Persisting Child Objects

Persisting Child Objects

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


OzMaqsood posted on Thursday, June 07, 2007

Hi all,

I'm new to the world of CSLA but a quick question to ask.

Basically I have a Root object which contains a single Child object.

One of the properties on the Child object is modified and the object is marked as Dirty.

The UI calls Root.Save to save the changes to the database. Within the Root.DataPortal_Update method, the Child changes are saved via a call to the Child.Update method.

However the Child object does not get saved. This is because the Root object has not changed and IsDirty returns false. It seems like the Child object will only be saved if the Root object has changed.

A simple solution is to call Child.Save within the Root.Save method. Is this the right thing to do?

Thanks

Oz

ajj3085 replied on Thursday, June 07, 2007

No, you need to override the IsDirty and IsValid properties on the parent to include the state of the child.. like this:

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


public override bool IsValid {
  get { return base.IsValid && myChild.IsValid ; }
}

OzMaqsood replied on Thursday, June 07, 2007

Thanks a lot!

Copyright (c) Marimer LLC