CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

AddNew performance question

rated by 0 users
Not Answered This post has 0 verified answers | 7 Replies | 1 Follower

Top 500 Contributor
17 Posts
bartol posted on Tue, Jan 18 2011 9:03 AM

Hi,

I am using CSLA 4.0 and WPF and I have this BO that has a child collection (based on BusinessListBase) where the results of a calculation are stored. The child that is stored in the collection only has two properties: a Guid and a double.

The problem I have is when I use AddNew to create over 10,000 items (the results of my calculation) it takes a long time to create them. I am overriding AddNewCore as such:

protected override MyResult AddNewCore() 
{
    Add(MyResult.New());
    return Items[Items.Count - 1];
}

The code that fills the collection is something like this:

foreach(var item in CalculationResults)
{
    var c = AddNew();
    c.Value = item.value;
    c.ReferenceId = item.Id;
}

I wonder if there is a better and faster way to do this? I stepped through the CSLA code and there are lot of IsValid checks during the process which maybe could be postponed until the end of my bulk operation.

Thanks,
B

All Replies

Top 10 Contributor
1,770 Posts

If you are loading that amount of data then you should make sure to set:

RaiseListChangedEvents = false;

before you add items and

RaiseListChangedEvents = true;

when you are done.

I presume this is done in your data access layer and not with active databinding? 

Anyway -  the ListChangedEvents may have serious affect of performance and should be off while you load data.

 

Jonny Bekkum, Norway CslaContrib Coordinator

Top 500 Contributor
17 Posts
bartol replied on Tue, Jan 18 2011 12:59 PM

Hi,

I have tried adding the RaiseListChangedEvents but it does not make a measurable difference since I am not subscribing to any event in this specific case. I am wondering if I should do a fetch on the list and load it through the DataPortal and resetting it but I am afraid that is going to cause havoc with the parent/child relationships of the objects.

The only way I managed to speedup the additions to nearly instant was by hacking the CSLA code by setting a static flag which returns true for IsValid in BusinessBase (terrible I know...).

Thanks,

Top 50 Contributor
161 Posts

Since you've already checked RaiseListChangedEvents, I make the following two suggestions.

 

  • Use either BypassPropertyChecks() when loading the values inside the for loop or use LoadProperty; this will then prevent many events firing. This can add up with many items.
  • This is a personal preference, but I would prefer to create an instance of the object in the AddNewCore, add that to the list and then return to the object, instead of accessing the list count and returning based on index. Probably doesn't make much difference for this simple example, but have had examples where changing this can make a big difference (mostly in loops).

 

 

Don't forget to verify the post(s) that best answers your question!

.NET Developer @ http://www.sybiz.com.au

Top 500 Contributor
17 Posts
bartol replied on Wed, Jan 19 2011 5:13 AM

Hi,

Thanks for that. I just tried BypassPropertyChecks now but still no difference. Also the problem appears in all types of list manipulation, for example, calling MyList.Clear() where MyList contains 1000 items similar to what I described above takes a couple of seconds to process unless I "suspend" the IsValid processing on BusinessBase. Very odd.

Cheers,

Top 10 Contributor
9,270 Posts

What do you mean by "IsValid checks"?

The IsValid implementation in BusinessBase is passive. In other words it doesn't run unless some code reads the property value. That implies that somewhere in the application there's some code that's re-reading IsValid as each child is added? What is that code?

Rocky

Top 500 Contributor
17 Posts
bartol replied on Thu, Jan 20 2011 3:57 AM

Hi,

The IsValid checks are triggered by a PropertyStatus in the UI. I can email you a stack trace if you want but basically what happens is that the AddNewCore indirectly triggers a Child_PropertyChanged which later on causes a ObjectStatus.Refresh() which calls IsSavable thus IsValid is called. The code I posted above does not show this because I only found it later. This exact same things happens when I call Clear in the collection. In this particular screen a call to Clear of a collection with 8 items causes over 50 calls to IsValid (that is as fas as I counted).

B

Top 10 Contributor
9,270 Posts

In this case you might try unbinding the object from the UI before doing mass operations.

I think someone already suggested setting RLCE to false? Unfortunately the .NET ObservableCollection base class doesn't have that concept, so there's no way to entirely disable collection changed events, and that's what most UI components use to know they should refresh...

Rocky

Page 1 of 1 (8 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems