CSLA4: A suggestion for Csla.Rules.CommonRules.Lambda class

CSLA4: A suggestion for Csla.Rules.CommonRules.Lambda class

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


Cuong posted on Wednesday, July 28, 2010

Hi Rocky,


I just found an issue with the Lambda class. Please have a look on the code below:

protected override void AddBusinessRules()
{

          var rule1 = new Lambda(MyPropertyInfo, context => context.AddErrorResult("Error"));

          BusinessRules.AddRule(rule1);


          var rule2 = new Lambda(MyPropertyInfo, context => {});

         BusinessRules.AddRule(rule2);

}

By the code we can see BusinessRules.IsValid is always False when MyProperty changes. But in fact BusinessRules.IsValid is True. The reason is both rule1 and rule2 have the same RuleName. So when BusinessRules.CheckRules is called, the rule2's result (Success) overrides the rule1's result (Failed)

 

For resolving this issue, I suggest that you change the original code:

public Lambda(Action<RuleContext> rule)
{
      Rule = rule;
      base.RuleUri.AddQueryParameter("r", Rule.GetHashCode().ToString());
}

to

public Lambda(Action<RuleContext> rule)
{
      Rule = rule;
      base.RuleUri.AddQueryParameter("r", Rule.Method.GetHashCode().ToString());
}

or safer

public Lambda(Action<RuleContext> rule)
{
      Rule = rule;
      base.RuleUri.AddQueryParameter("r",

           Convert.ToBase64String(Encoding.Unicode.GetBytes(Rule.Method.ToString())));
}

 

 

RockfordLhotka replied on Wednesday, July 28, 2010

Thanks, that's clearly a bug, I'll add it to the list.

RockfordLhotka replied on Tuesday, August 03, 2010

The fix will be in 4.0.1.

Copyright (c) Marimer LLC