CSLA load on demand

CSLA load on demand

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


Slayer posted on Monday, April 20, 2009

Hi everybody,

I have a Customer(parent) object which contains a collection of Orders(child) objects. I would like to load the Order collection on demand. So the Orders collection is loaded only when Customer.Orders property is invoked. My IsDirty code would look something like this:

public override bool IsDirty
{
bool isOrdersDirty = false;

if(_Orders != null)
{
isOrdersDirty = _Orders.IsDirty;
}

return base.isDirty || isOrdersDirty;
}

I am having problems with my Customer.BeginEdit() however.
When the BeginEdit method is invoked before the Orders collection is loaded, this could result in a mess.

Does anybody have any ideas on this matter.

Thanks

ajj3085 replied on Monday, April 20, 2009

Search the forum for lazy loading. Csla supports lazy loading, you just have to follow the proper pattern (and since I don't use lazy loading, I can't tell you exactly :-) )

Paul Czywczynski replied on Monday, April 20, 2009

Here is the pattern we're using on CSLA 3.0.x. In this example we are doing three things. First we declare a private property to cache the branch list so we don't have to fetch it again. Second we mark the private property as NonSerialized so it isn't shuttled across the wire back to the dataportal in 3 tier mode on insert, update, delete operations. Learned that one the hard way. Third we lazy load the branch list by calling GetBranchCollection only when the BranchList getter is called and our _branchList cached property is null.

        [NonSerialized()]
        private BranchCollection _branchlist;
        public BranchCollection BranchList
        {
            get
            {
                CanReadProperty("BranchList");
                if (_branchlist == null)
                    _branchlist = BranchCollection.GetBranchCollection();
                return _branchlist;
            }
        }


RockfordLhotka replied on Monday, April 20, 2009

That isn't sufficient in 3.0.x though, if the parent object is at EditLevel > 0.

I've discussed this on other threads as well, but what you need to do (prior to 3.5 with the field manager) is manually ensure that the new child's edit level is the same as the parent before you return it. This is a couple while loops like

while (this.EditLevel > newChild.EditLevel) newChild.BeginEdit();
while (this.EditLevel < newChild.EditLevel) newChild.ApplyEdit();

 

Copyright (c) Marimer LLC