CSLA performance (CheckRules)

CSLA performance (CheckRules)

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


CaymanIslandsCarpediem posted on Wednesday, March 21, 2007

In my last post about CSLA performance (http://forums.lhotka.net/forums/thread/13330.aspx), I found that calling ValidationRules.CheckRules() on each item after they are fetched resulted in more than doubling the time required to return those objects even for a small number of very simple rules. 

I realize in many cases calling ValidationRules.CheckRules() on a previously saved item may not make sense as if it was previously saved all rules must have been passed.  However, in a number of my cases items could have some of thier rules broken by changes to other objects, etc, etc and I have been calling ValidationRules.CheckRules() during each fetch "just to be safe".  After seeing the performance impact of this, I'm considering moving away from this practice in most cases, however I still want to ensure EVERY rule is checked before I would actually edit any of those items.  Below I'll outline my thoughts on what I'm thinking of doing and would appreciate any thoughts.  Maybe this could even make it into the default CSLA framework?  Not sure that this functionality is needed in enough cases to make that worth while, just thought I'd mention it ;-)

My goal is to avoid calling ValidationRules.CheckRules() on each item fetch for the performance hit that it can cause.  However, if an item is going to be edited I do want all rules checked even if its a rule for a property that isn't being edited.  I seems I can accomplish this with a VERY simple change to two CSLA classes.

1) In Csla.Validation.ValidationRules I'll create a properly like IsAllRulesChecked.  Then in the public void CheckRules() method (just that one which calls all rules) set IsAllRulesChecked to true.

2) In Csla.Core.BusinessBase add this to public void BeginEdit():

if (!this.ValidationRules.IsAllRulesChecked) ValidationRules.CheckRules();

So for each item it will track if the CheckRules method which checks all rules has been called and if not it will call it on BeginEdit().  I could also add additional logic to all CheckRules methods to see if all rules have been checked individually, etc but I cannot imagine the gain of possibly avoiding one extra CheckRules() call would justify the overhead of checking for this.

Any thoughts appreciated!

joshpainter replied on Thursday, March 22, 2007

I don't have the CSLA framework source in front of me, but you could subclass BusinessBase with a class that overrides PropertyHasChanged.  In your method, you would check a local bool field similar to your "isAllRulesChecked".  If it is false, call ValidationRules.CheckRules() which will force all rules to be checked, then set the local bool field to true.  If true, just call base.PropertyHasChanged(propertyName).  This way, the first time a property is changed on a freshly fetched object, all rules are checked for all properties instead of just that one.

This is mainly from memory, so I may not have the override signatures correct...but it should work if you can override that method.  Anything I'm not thinking of?

 

Copyright (c) Marimer LLC