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.
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()":
protected virtual void DataPortal_Create()
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:
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:
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.
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).
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