Child Collection lazy init and parent IsDirty property

Child Collection lazy init and parent IsDirty property

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


ErikJL posted on Wednesday, December 30, 2009

Hi! I have a CSLA Busniness object (FuseLocation) with a child collection (FuseList). Since the child collection is not always necessary, and there will be times when there will be a large collection of the type of parent object, I want the child collection to lazy initialize (hydrate only when accessed the first time). This introduces a problem with the parent's IsDirty property; when the child collection is acccessed and hydrated for the first time, the parent's IsDirty property is set to true even though no data has changed. Realizing this was a core function of the framework for detecting a property change I tried to reset IsDirty to false using MarkClean() if IsDirty was false to begin with, but for I cannot figure out why it does not work. Here's the child collection propery code:

        public FuseList List
        {
            get
            {
                Boolean wasDirty = this.IsDirty;

                if (this.fuseList == null)                 
                    this.fuseList = FuseXList.GetChildList(this);

                if(!wasDirty)
                    this.MarkClean();

                return this.fuseList;
            }
        }

IsDirty is the only  thing I noticed, but i realize there may be more going on behind the scenes in the framework. How can I implement a lazy initialized child collection correctly?

ajj3085 replied on Wednesday, December 30, 2009

Looks like you're using private backing fields.  In that case, there's nothing in the code you posted that would make the parent dirty. 

I suspect two possible things: 1)  your GetChildList is changing a property on the parent, thus causing your problems.  2)  The child objects in the list are coming back as dirty for some reason.  You don't mention what Csla version you're using, but there was a time where you had to call MarkOld manually.  Perhaps that's the case here?

HTH

Andy

ErikJL replied on Wednesday, December 30, 2009

Hi Andy,

 You are correct! The child list (rather, each child object) is coming back dirty.  I'm not sure what version CSLA we have as my partner was the one who put it in source control, and I don't see any versioning, but I do know we aren't using the latest version. Calling "MarkOld()" within each child object did the trick. Thank you for the help!

Erik

ajj3085 replied on Thursday, December 31, 2009

Erik,

Glad you were able to get it working.  Check the Csla AssemblyInfo.cs file.. unless your partner changed it, that should tell you what version you have.  Its probably one of the older ones, because newer versions have ways where Csla will take care of the MarkOld call for you.

Andy

ErikJL replied on Thursday, January 07, 2010

Ahh, there it is - 2.1.4 is the version. Thanks again.

Copyright (c) Marimer LLC