Parent object's IsDirty Property

Parent object's IsDirty Property

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


mushroom posted on Wednesday, October 15, 2008

Hi All,

I looked and was not able to find an answer to my question. Kindly link me to the answer if there is already one.

So, I have a parent object with a collection of child objects whose structure is similar to the ProjectTracker sample. These are what my objects basically look like:

Parent Object

public class ParentObject : BusinessBase<ParentObject>

{

private ChildDetails _childDetails = ChildDetails.NewChildDetails();

public ChildDetails ChildList

{

get { return _childDetails ; }

}

}

Child Object

[Serializable()]

public class ChildDetails : BusinessListBase<ChildDetails , ChildDetail>

{

public void Assign(int id, string desc)

{

if (!Contains(id))

{

ChildDetailitem = ChildDetail.NewChildDetail(id, desc);

this.Add(item);

}

else

throw new InvalidOperationException(someMessage);

}

The NewChildDetail method:

private ChildDetail(int id, string desc)

{

MarkAsChild();

_id = id;

_desc = dessc;

}

When adding a ChildDetail object I have this code:

ParentObject parent = ParentObject.GetParentObject(idParent);

parent.ChildList.Assign(numX, strX); //where parent is a valid instance of ParentObject. The data was populated by a similar method

after that line, checking parent.IsDirty shows false. My problem here is that the child objects are not persisted because the parent.IsDirty is false. That leads to BusinessBase.Save not calling the DataPortal.Update.

 

sergeyb replied on Wednesday, October 15, 2008

If you are not using 3.5/3.6 syntax for property declarations, you will need to override dirty and valid in your parent to account for child list.

Dirty

Return MyBase.IsDirty OrElese _childDetails.IsDirty

 

Valid

Return MyBase.IsValid AndAlso _childDetails.IsValid

 

Or you can switch to 3.5 syntax and let CSLA manage those for you.

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: mushroom [mailto:cslanet@lhotka.net]
Sent: Wednesday, October 15, 2008 1:00 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Parent object's IsDirty Property

 

Hi All,

I looked and was not able to find an answer to my question. Kindly link me to the answer if there is already one.

So, I have a parent object with a collection of child objects whose structure is similar to the ProjectTracker sample. These are what my objects basically look like:

Parent Object

public class ParentObject : BusinessBase<ParentObject>

{

private ChildDetails _childDetails = ChildDetails.NewChildDetails();

public ChildDetails ChildList

{

get { return _childDetails ; }

}

}

Child Object

[Serializable()]

public class ChildDetails : BusinessListBase<ChildDetails , ChildDetail>

{

public void Assign(int id, string desc)

{

if (!Contains(id))

{

ChildDetailitem = ChildDetail.NewChildDetail(id, desc);

this.Add(item);

}

else

throw new InvalidOperationException(someMessage);

}

The NewChildDetail method:

private ChildDetail(int id, string desc)

{

MarkAsChild();

_id = id;

_desc = dessc;

}

When adding a ChildDetail object I have this code:

ParentObject parent = ParentObject.GetParentObject(idParent);

parent.ChildList.Assign(numX, strX); //where parent is a valid instance of ParentObject. The data was populated by a similar method

after that line, checking parent.IsDirty shows false. My problem here is that the child objects are not persisted because the parent.IsDirty is false. That leads to BusinessBase.Save not calling the DataPortal.Update.

 



mushroom replied on Wednesday, October 15, 2008

How careless of me. I have that test on my other Parent objects. I forgot to include the test for the child objects on this class. Thanks a lot!

eulerthegrape replied on Wednesday, October 15, 2008

In your parent object override the IsDirty (and probably the IsValid) methods to include the validity of your child collection.

ChildDetails _children = ChildDetails.NewChildDetails();

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

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

Copyright (c) Marimer LLC