Disabling rules on Object Creation

Disabling rules on Object Creation

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


veryblue posted on Saturday, January 07, 2012

Hi,

Is there a way to disable business rules on Object creation ? I have rules that hit the database and I dont want the rules to run when I first create an object. 

rxelizondo replied on Saturday, January 07, 2012

 

The default implementation of BusinessBase “DataPortal_Create()” and "Child_Create()" automatically call "BusinessRules.CheckRules()", for example, the code below is the default implementation of "DataPortal_Create()":

 

[RunLocal]

protected virtual void DataPortal_Create()

{

     BusinessRules.CheckRules();

}

 

So if you don’t want to run the rules then one option is to override “DataPortal_Create()” or "Child_Create()" and don’t call the base implementation as in:

 

[RunLocal]

protected override void DataPortal_Create()

{

}

 

If the problem is that you are loading property values during creation then you can use the "LoadProperty()" to bypass running rules or still use the "SetProperty()" method but use it between a using directive:

 

using (BypassPropertyChecks)

{

   SetProperty(MyProperty, SomeValue);

   -OR-

   this.MyProperty = SomeValue;

}

 

I believe that the newer version of the CSLA has a way to create rules that don’t run during object creation but I am not 100% sure about that.

 

 

 

JonnyBee replied on Sunday, January 08, 2012

Which version of CSLA do you use?

Assuming that you only want to prevent running rules that hit the database (all others should run on creation):

In CSLA 4.2 and when your rule inherits from PropertyRule or CommonRules.CommonBusinessRule you can set one of these flags in the constructor:

CanRunOnServer                    -  when false will deny the rule from running on "logical" server side.

CsnRunInCheckRules              - when  false will deny the rule from running in  CheckRules

CanRunAsAffectedProperty   - when false will deny cascading execution when this primaryProperty is an AffectedProperty from another rule.

When all 3 flags as false the rule will only run when the user edits a field (or your code sets the field with BypassProperyChecks).

 

veryblue replied on Sunday, January 08, 2012

CSLA 4

I am binding an empty object to a asp.net mvc view, so dont want any rules fired. I already am overriding the DataPortal_Create so not calling the checkrules there fixed my problem.

Its good to know the various options available though, thanks a lot!

Copyright (c) Marimer LLC