MarkAsNew() taking a long time when done for each item in a BusinessBaseLIst

MarkAsNew() taking a long time when done for each item in a BusinessBaseLIst

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


snakebyteme posted on Tuesday, August 31, 2010

He is the scenario:

We are using CSLA 3.8.4. We execute a stored procedure to generate invoice detail rows. We load them into a BBList , so they are marked as clean since they were fetched. We use "lazy instantiation" in our child object properties (if Me.IsNew and FieldManager not exists then obj.CreateChild). We run some post processing routines that evaluate the invoice detail rows. Before we call the parent's save method, we do a "for each detail in BBLIst {detail.MarkAsNew()}" which will give use the desired object state to force insertion into the database.

If I mark the objects as new before I add them to the BBList, there is no delay but I run into lazy instantiation problems since the parent is new.

If I mark each item in the BBList as new right before the save, the lazy instantiation problems are solved since all evaluation was done on clean objects, but it can take 45 minutes to mark 5000 objects as new.

For now, I have added a boolean property in the BB subclass called DisableLazyInstatiation and have added it to our lazy instantiation code. I mark the object as new before it is added to the BBList and set the flag to block lazy instantiation.  This is not an ideal solution for me.

It seems odd that it takes so long for a boolean flag (IsNew) to be set for each item in a collection. Obviously more is being done than just setting the boolean value.

Does anyone have a suggested way to mark an entire BBList's contents as new quickly?

JonnyBee replied on Tuesday, August 31, 2010

Hi,

Why do you need to iterate through the list to alter the state in the first place?
You should be able to use DataPortal.Create<> to lazy load (and initialize using a SP) a new item that is correctly marked as new?

Have you tried to measure the time for just iterating through the collection without calling MarkAsNew?
If just iterating the list takes time then it is possible the ListItemChanged event that takes time.

MarkNew also calls MarkDirty which raises the OnUnknownPropertyChanged event so if DataBinding is active when you itereate through the list it sure will take extra time updating the UI.

So try the following:
1. See if you can instantiate new objects with correct state (preferred solution) - if not then following:
2. Make sure DataBinding is not active when you iterate the list.
3. If just iterating the list takes long time then try to set RaiseListChangedEvents = false before iterating through the collection and reset to true afterwards.

snakebyteme replied on Tuesday, August 31, 2010

"RaiseListChangedEvents = False" eliminated the problem.

    Public Sub MarkAsNew()
        RaiseListChangedEvents = False
        For Each Item As InvoiceDetail In Me
            Item.MarkAsNew()
        Next
        RaiseListChangedEvents = True
    End Sub

Thank you.

Copyright (c) Marimer LLC