about SharedValidationRulesManager

about SharedValidationRulesManager

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

xc_lw posted on Monday, December 19, 2011

How to use ValidationRulesManager?

How to use SharedValidationRulesManager?

Are there some samples?

BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(UserNameProperty, "User Name can not be empty"));

Is this rule a shared rule?

How to add an object levle rule and how to add a type level rule?

And another question:I just want to check rules before save an object. I see there is only one method called CheckObjectRules for the object instance. Is it equals to BusinessRules.CheckRules() or BusinessRules.CheckObjectRules? What's the difference between these three methods?


instance.BusinessRules.CheckRules()  --Only can be called inner the class

instance.BusinessRules.CheckObjectRules() --Only can be called inner the class

I'm confused totally!

JonnyBee replied on Monday, December 19, 2011

CSLA 4.2 does not have ValidationRulesManager nor SharedValidationRules. These were part of the validation rules in Csla 3.8.x.

CSLA 4.2 have a new rule engine with only per-type rules. (No more per-instance rules). So when compared to the old rule engine all rules in CSLA 4.2 is "shared" rules for all instances of a given type.

Object level rule: PrimaryProperty = null
Property rule:       PrimaryProperty = an instance of IPropertyInfo (a registered property).

Both instance level authorization rules, object level rules and property rules are added by BusinessRule.AddRule in AddBusinessRules.

For an object instance that are active in databinding (typically rich UI) there is only 2 methods that will notify UI of changes:


For objects that are NOT active in databinding you can call

<bo>.BusinessRules.CheckRules ()                       - check all object rules and property rules
<bo>.BusinessRules.CheckRules(propertyInfo)   - check rules for a given property
<bo>.BusinessRules.CheckObjectRules               -  check all object level rules

If you are creating a web application (and typically only want to check rules on postback) the look at 

Csla.Web.Mvc.CslaModelBinder  and the Csla.Core.ICheckRules.

In a web app you will typically suppress rules when updating fields in the postback and the run all rules afterwards. The ICheckRules interface provides this for your code.

In the samples download you can also look at Samples\NET\cs\RuleTutorial  and Samples\NET\cs\BusinessRuleDemo

I also suggest that you purchase the Using CSLA 4 Ebook series from Rocky, especially "Creating Business Objects" available from http://download.lhotka.net/Default.aspx?t=UsingCsla4 and http://store.lhotka.net/Default.aspx?tabid=1560&List=1&CategoryID=4&Level=1&SortField=DateCreated%20DESC,ProductName

xc_lw replied on Monday, December 19, 2011

I want to check the validation of a <BO>.  The <BO> is created by DataPortal.BeginCreate() and bind to a form. When the user not modify any property in the UI. The BrokenRuleCollection is empty.

So I must check the validation before the user push the [Save] button.

I have a MyBusinessBase class which drived from BusinessObject, and I write a public CheckObjectRules object in which call base.CheckObjectRules . Like bellow:

        public void CheckObjectRules()

but the base.CheckObjectRules() method only check these rules :

                   where r.PrimaryProperty == null
                    && CanRunRule(r, executionContext)

when I track to this statement, all the rules' PrimaryProperty is not null,so no rules be checked, It seems that all rules I set are property level. So I call


It's worked now, the <BO>.IsValid is false, but in the UI Ican not see any "Red" error information.

If the mouse enter the control which the invalid property binding to and there will be a "Red" error information, If I just create a new <BO> and then press the "Save" button, although I called the CheckRules(), there will be not a "Red" infomation.

What's the right method to achive my requirement?



JonnyBee replied on Monday, December 19, 2011

CSLA - default behavior - is to call:

  1. BusinessRules.CheckRules in DataPortal_Create or <objectfactory>.Create so that the BO will show broken rules in the UI at first.
  2. BusinessRules are run for each time the user edits a property
  3. This allows the UI to keep track of object status in the viewModel (IsDirty, IsValid etc)

Which explains why there are only 2 methods in the BO that notifies the UI of changes as I described in the first post.

You can however implement this yourself like this in your BO:

    public void CheckAllRules()
        List<string> list = this.BusinessRules.CheckRules();
        if (ApplicationContext.PropertyChangedMode == ApplicationContext.PropertyChangedModes.Windows)
            foreach (string propertyName in list)
in order to run all rules and notify the UI. 

Copyright (c) Marimer LLC