Save and reread Child-Objects

Save and reread Child-Objects

Old forum URL: forums.lhotka.net/forums/t/8866.aspx


t.kehl posted on Sunday, May 02, 2010

Hi.

I have troubles with Save and Reread Child-Objects.

I have the fallowing Objects:

Activity
ActivityDocument (wich is a child of Activity)
Checkout (which is a child of ActivityDocument).

Now, I do the fallowing:

  var activity = Activity.GetActivity(new Guid("..."));
  var doc = ActivityDocument.GetActivityDocument(activity.ActivityDocumentList[0].Id);
  var n = doc.CheckoutList.Count;

  var c = doc.CheckoutList.AddNew();
  // ... setting some properties
  doc = doc.Save();

  n = doc.CheckoutList.Count;

at the end, n has the correct count - after the first execution - 1. Now, when I retry this, n is after getting doc 0 and at the end one more time 1. When I restart the Service with the WCF-Dataportal, the count will be correct. It seems, that there is a problem about the caching. Do I making something wrong with saving my objects?

I use CSLA 3.8.2.

Thanks for your help!

Best Regards, Thomas

JoeFallon1 replied on Sunday, May 02, 2010

You can't save a child object directly.

So doc=doc.Save should not work.

You need to save the root which saves its children:

activity = activity.Save()

Joe

 

t.kehl replied on Monday, May 03, 2010

Hi Joe

When I do

var doc = ActivityDocument.GetActivityDocument(activity.ActivityDocumentList[0].Id);

doc is now an EditableRoot.

I have the same problem on one layer higher:

Activity is an EditableRoot
ActivityDocumentList is a ChildCollection of Activity

public void CreateNewActivityDocument() {
  var activity = Activity.GetActivity(new Guid("58823196-3597-4549-ADD2-E32432F4B3F5"));
  var ad = activity.ActivityDocumentList.AddNew();
  var x = activity.ActivityDocumentList.Count   
  // ... setting some properties to ad
  activity = activity.Save();
  var y = activity.ActivityDocumentList.Count   
}

When I do CreateNewActivityDocument() the first time x and y are:
x = 0
y = 1

The problem is now, when I do CreateNewActivityDocument() one more time I have the same results for x and y
x = 0
y = 1

I think, there will be a caching of the EditableRoot and the ChildCollectionList. But I expect, that this cache should be updated when I call Save() of the EditableRoot.

Best Regards, Thomas

RockfordLhotka replied on Monday, May 03, 2010

Read (or re-read) Expert 2008 Business Objects chapters 1-5, with a focus on chapter 4. It is important to understand how CSLA expects you to construct your object relationships.

An object graph can have exactly one root object. Only the root object can be directly retrieved or saved. All other objects in the graph are child objects, and are retrieved/saved as part of the root object.

The one exception to this is lazy loading, in which case child objects are retrieved independently from the root, but are still saved as part of the root.

I suppose EditableRootListBase (a dynamic list) is an exception too - it is a specialized collection that contains root objects.

Copyright (c) Marimer LLC