CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Custom Business Rules

rated by 0 users
Answered (Verified) This post has 1 verified answer | 4 Replies | 1 Follower

Top 500 Contributor
31 Posts
Mr X posted on Thu, Jun 7 2012 11:16 AM

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");

    }

}

 

Answered (Verified) Verified Answer

Top 10 Contributor
1,813 Posts
Verified by Mr X

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 .

 

 

Jonny Bekkum, Norway CslaContrib Coordinator

All Replies

Top 500 Contributor
31 Posts
Mr X replied on Thu, Jun 7 2012 11:21 AM

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");

}

}

 

 

Top 10 Contributor
9,281 Posts

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.

Rocky

Top 10 Contributor
1,813 Posts
Verified by Mr X

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 .

 

 

Jonny Bekkum, Norway CslaContrib Coordinator

Top 500 Contributor
31 Posts
Mr X replied on Fri, Jun 8 2012 8:20 PM

Thank you both very much, it now works!

Page 1 of 1 (5 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems