Grandchild doesn’t get saved

Grandchild doesn’t get saved

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


albruan posted on Monday, September 18, 2006

In my application, I have a Project object that contains a collection of Department objects.  I don’t encounter any problems saving any additions or modifications to a Department object as long as I don’t add/modify a Department’s children. 

 

Each Department object contains a collection of Drawing objects.  If I add a new Drawing object, it shows up in the Drawings collection for the Department in which the Drawing object is being added; however, the new Drawing object doesn’t get persisted to the database in this case because the IsDirty property for the Department isn’t recognized as being true.

 

I’ve tried instantiating a new Drawing object two ways, but it doesn’t have any effect.  In the first case, I instantiated it with the following code:

            Library.Drawing aDwg = aProj.Department[1].Drawings.AddNew();

In the second case, I instantiated it by first instantiating a Department object and then performing an AddNew as follows:

            Library.Department aDept = aProj.Department[1];

            Library.Drawing aDwg = aDept.Drawings.AddNew();

In both cases, I instantiate the proj object as follows:

            Library.Project aProj = Library.Project.GetProject{  Guid for the project  };

 

After adding the data for the new Drawing object, I attempt to save the whole thing by calling aProj.Save(), but it won’t save the changes since, as I mentioned, the aProj and aDept objects aren't recognized as being dirty.

 

My question is why doesn’t the application recognize the Department object as being dirty when a child object is added to a collection contained within the Department object?

RockfordLhotka replied on Monday, September 18, 2006

Did you override the IsDirty property in your parent object as described in Chapters 7 and 8? That is a requirement for any parent object.

albruan replied on Monday, September 18, 2006

I overrode it in the Project object thusly:

public override bool IsDirty
{
   get { return base.IsDirty || _department.IsDirty; }
}

and overrode it in the Department object thusly:

public override bool IsDirty
{
   get { return base.IsDirty || _drawing.IsDirty; }
}

While iterating through the child objects in the BusinessListBase class, it returns true for IsDirty when it gets to the new Drawing object, but it returns false for IsDirty the last time through and that's what gets passed back to the overridden IsDirty property in the parent Project object.

RockfordLhotka replied on Monday, September 18, 2006

I don't have a good picture of your object relationships. But BLB returns IsDirty=true if any of its child objects are dirty. A parent should use a BLB to manage its children, so the parent should ask the collection if any of the children are dirty.

In other words, your code is "right", except it doesn't look like you are asking the child collection if it is dirty - you are asking some singular object - and that seems like a bug.

Copyright (c) Marimer LLC