References question

References question

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


boo posted on Sunday, September 24, 2006

Been consulting the book trying to find the answer but I'm missing it or it's not there.  I realize when you have a BusinessBase object and you save it you need to update your references for reasons outlined in the book:


myBizObj = myBizObj.Save();


But when you have an EditableRootCollection - what is the behavior of the following:


myBizObj = BizObject.NewBizObject();
//Set some properties
//....
//Add to collection
myBizObjCollection.Add(myBizObj);
//Save the collection
myBizObjCollection = myBizObjCollection.Save();


First of all, do I need to reassign the collection.  If I read the book right, yes.

This would mean that the myBizObj object reference is no longer valid, right, because the myBizObjCollection objects internal collection is now updated with new references, so I need to find the original object I was working with (myBizObj) in the returned list and reassign my reference right?  I haven't found an easy way to do this except to create a 'Find' method in the EditableCollection object myself (same problem with deleting objects RemoveAt is protected)...seems like there'd be a built in method similiar to how List<T> has Find and FindAll and such using the Predicate<T> delegate.

Anyway...am I overthinking this or am I correct about all of this?  I'm also still pretty young in the CSLA world so I might not be aware of a method (for finding in a collection and deleting a child object in a collection).

Thanks.

RockfordLhotka replied on Wednesday, September 27, 2006

You are basically correct. When a BusinessListBase-derived collection is saved, the Save() method returns a new instance of the collection, and you need to re-bind to that new collection.

The child items in the collection are also new, so any bindings to child objects must also be refreshed. This probably does mean doing some sort of find operation to locate the appropriate child in the new collection.

Copyright (c) Marimer LLC