Custom Business Rules

Custom Business Rules

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


Mr X posted on Thursday, June 07, 2012

I am having trouble implementing two very simple business rules, one updater and one validator. The first rule, verifies if a bool property "IsSelected" is true. If so, it sets the value of a second (a string) property to "". The second rule creates a broken rule if the second property remains "". The problem is when I test it I always get a broken rule different than the one I want:

"null:Specified argument was out of the range of valid values."  

 I looked at the CSLA 4 book and a number of posts but can't seem to put my finger on the problem.

Any help would be appreciated.

 

My code has 2 business rules:

I set the CityProperty as a dependent property of IsSelected as followed:

BusinessRules.AddRule(

 

new Dependency(IsSelectedProperty, CityProperty));

 

and I set the rules priorities as followed:

BusinessRules.AddRule(

 

new UpdateCityRule() { Priority = 0 });

BusinessRules.AddRule(new CityMandatoryRule() { Priority = 1 });

 

 

FIRST RULE:

 

 

 

 

 

 

public

 

 

class UpdateCityRule : Csla.Rules.BusinessRule

{

 

 

 

    protected override void Execute(Csla.Rules.RuleContext context)

    {

 

 

 

        var target = (MyObject)context.Target;

 

 

 

        if (target.IsSelected)

            context.AddOutValue(CityProperty,

 

"");

    }

}

 

 SECOND RULE:

 

 

 

public class CityMandatoryRule : Csla.Rules.BusinessRule

 

 

 

{

 

 

 

    protected override void Execute(Csla.Rules.RuleContext context)

    {

 

 

 

        var target = (MyBO)context.Target;

 

 

 

        if ((target.IsSelected) && (target.City == ""))

            context.AddErrorResult(CityProperty,

 

"City is required");

    }

}

 

Mr X replied on Thursday, June 07, 2012

I realize the code did not formatted the way I wanted. Here is the same code:

public class UpdateCityRule : Csla.Rules.BusinessRule

{

protected override void Execute(Csla.Rules.RuleContext context)

{

var target = (MyObject)context.Target;

 

if (target.IsSelected)

context.AddOutValue(CityProperty, "");

LoadProperty(target, MyObject.CityProperty, "");

}

}

  

public class CityMandatoryRule : Csla.Rules.BusinessRule

{

protected override void Execute(Csla.Rules.RuleContext context)

{

var target = (MyObject)context.Target;

 

if (!(target.IsSelected) && (target.City == ""))

context.AddErrorResult(CityProperty, "City is required");

}

}

 

 

RockfordLhotka replied on Thursday, June 07, 2012

There's probably an exception in one of the rules. If an exception occurs in a rule, the exception message is returned as the broken rule text, and it looks like that's what you are seeing.

Stepping through the rules in the debugger is probably the fastest way to find out what's going on.

JonnyBee replied on Thursday, June 07, 2012

Rocky is correct that you get an exception. The reason is that in order to use AddOutValue the propertyinfo must exist in AffecdtedProperties dictionary. 

So the first rule - as to my coding preferences would be like this:

    public class UpdateCityRule : Csla.Rules.PropertyRule
    {
        public IPropertyInfo<string> CityProperty { getprivate set;}
        public UpdateCityRule(PropertyInfo<bool> selectedProperty, IPropertyInfo<string> cityProperty)
              : base(selectedProperty)
        {
            CityProperty = cityProperty;
            AffectedProperties.Add(cityProperty);
        }
        
        protected override void Execute(Csla.Rules.RuleContext context)
        {
            var selected =  (bool) context.InputPropertyValues[PrimaryProperty];
            if (selected)
                context.AddOutValue(CityProperty, "");
        }
    }


usage in AddBusinessRules: 
    BusinessRules.AddRule(new UpdateCityRule(IsSelectedProperty, CityProperty));

 

  1. This role will automatically be checked by the rule engine when the IsSelectProperty is changed (as it is the PrimaryProperty).
    IE: Property rules should always have a PrimaryProperty
  2. The rule will be executed in BusinessRules.CheckRules().
  3. The City property will be cleared when fr property is selected.

Notice how the Rule has NO knowledge of the actual BusinessObject.  It only needs 2 PropertyInfo's to use as keys for either reading value from InputProperties og set the OutValue .

 

 

Mr X replied on Friday, June 08, 2012

Thank you both very much, it now works!

Copyright (c) Marimer LLC