Validation for series of numbers

Validation for series of numbers

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


Holger posted on Thursday, November 04, 2010

Hi all,

I want to validate a series of numbers (e.g. money). I have a class with three Money-Properties "Money1", "Money2" and "Money3" where the input rule is "Money1 less than Money2 less than Money3". The input values should always be in ascending order and the error provider should show both errors "less than" and "greater than". Our money class is tested and all operators are working well.

My first idea was to create two rules "MoneyLessThanOtherMoney" and "MoneyGreaterThanOtherMoney"

Sample code of "MoneyLessThanOtherMoney" ("MoneyGreaterThanOtherMoney" is the same):

public class MoneyLessThanOtherMoney : CommonBusinessRule
{
    public IPropertyInfo MoneySecondProperty { get; private set; }
 
    public MoneyLessThanOtherMoney(Csla.Core.IPropertyInfo moneyFirst, Csla.Core.IPropertyInfo moneySecond)
        : base(moneyFirst)
    {
        InputProperties = new List<Csla.Core.IPropertyInfo> { moneyFirst, moneySecond };
            
        MoneySecondProperty = moneySecond;            
        AffectedProperties.Add(moneySecond);
    }
 
    protected override void Execute(Csla.Rules.RuleContext context)
    {
        Money moneyFirst = context.InputPropertyValues[PrimaryProperty] as Money;
        Money moneySecond = context.InputPropertyValues[MoneySecondProperty] as Money;
 
        if (moneyFirst != null && moneySecond != null)
        {
            if (moneyFirst >= moneySecond)
            {
                string message = string.Format(Localization.MoneyRules.MoneyLessThanOtherMoney, PrimaryProperty.FriendlyName, MoneySecondProperty.FriendlyName);
                context.Results.Add(new RuleResult(RuleName, PrimaryProperty, message) { Severity = Severity });
            }
        }
    }
}

And the AddBusinessRules method is:

protected override void AddBusinessRules()
{
    BusinessRules.AddRule(new MoneyLessThanOtherMoney(Money1Property, Money2Property) { Severity = Csla.Rules.RuleSeverity.Error });
    BusinessRules.AddRule(new MoneyGreaterThanOtherMoney(Money2Property, Money1Property) { Severity = Csla.Rules.RuleSeverity.Error });            

    BusinessRules.AddRule(new MoneyLessThanOtherMoney(Money2Property, Money3Property) { Severity = Csla.Rules.RuleSeverity.Error });
    BusinessRules.AddRule(new MoneyGreaterThanOtherMoney(Money3Property, Money2Property) { Severity = Csla.Rules.RuleSeverity.Error });

    BusinessRules.AddRule(new MoneyLessThanOtherMoney(Money1Property, Money3Property) { Severity = Csla.Rules.RuleSeverity.Error });
    BusinessRules.AddRule(new MoneyGreaterThanOtherMoney(Money3Property, Money1Property) { Severity = Csla.Rules.RuleSeverity.Error });
}

I would expected that I get two broken rules, when the users enters the following data
    
    10 USD, 5 USD, 20 USD
    
broken rule messages should be

    Money1 -> must be less than Money2
    Money2 -> must be greater than Money1

but I get only the message

        Money2 -> must be greater than Money1
        
When I debug into my rule classes BOTH "context.Results.Add" got fired, but in the BrokenRulesCollection of my business class is only ONE broken rule. I tried different scenarios 2 - 4 depending money properties, only with 2 money properties I got the expected results. I'm new to the new business rule subsystem, maybe it is a bug in the new system or my rules are not correct.

Does someone have a hint for me?

Copyright (c) Marimer LLC