Saving child objects, getting the saved instance?

Saving child objects, getting the saved instance?

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


roboto posted on Monday, January 14, 2008

Hi!


I'm just starting to get a grip on the usage of CSLA and so I'm getting a bit stuck.

Okay, what I've got:

ParentObject
    -> Childs : ChildList
       -> Child

My ChildList has a 'AddNew' function that basicly Creates (through the DataPorta) a new instance and adds it to the list.

Now I've the following code:
ParentObject instanceOfParent = ParentObject.LoadById(55);
Child newChild = instanceOfParent.Childs.AddNew();
newChild.Description = "Hello forum";

instanceOfParent = instanceOfParent.Save();

Assert.IsFalse(instanceOfParent.IsDirty);
Assert.IsFalse(newChild.IsDirty, "this will fail!");

Now, the parent, the list and the newChild is saved successfully to the database, but since cslsa creates a copy of newChild child, my initial object isn't updated.  So the second assert will fail.

Is there a chance to work around this? I'm mainly using by BOs from code, not bound to the GUI.

Thank you
Dominik
   

mr_lasseter replied on Monday, January 14, 2008

When you save the object you get a new instance of the obect (and all child objects).  What you are going to have to do is find the child item in the newly saved list.  Try something along the lines of:

ParentObject instanceOfParent = ParentObject.LoadById(55);
Child newChild = instanceOfParent.Childs.AddNew();
newChild.Description = "Hello forum";

instanceOfParent = instanceOfParent.Save();
newChild = instanceOfParent.Childs[0];

Assert.IsFalse(instanceOfParent.IsDirty);
Assert.IsFalse(newChild.IsDirty, "this will not fail!");


Copyright (c) Marimer LLC