Hi CSLA friends,
I am wondering if somebody had encountered the same issue. I have implemented BusinessRules in 2 RuleSets but I get different results in 4.1 than in 4.2.x and even in 4.3.x.
The business rules are declared as follows;
Protected Overrides Sub AddBusinessRules()
' Adds the rules from the DataAnnotation
MyBase.AddBusinessRules()
' Some Rule Set other than the default
BusinessRules.RuleSet = "someruleset"
BusinessRules.AddRule(New somerule(Of UserItem))
' Set active Rule Set to the default
BusinessRules.RuleSet = "default"
End Sub
Protected Overrides Sub AddBusinessRules()
' Add rules from the DataAnnotations
MyBase.AddBusinessRules()
' some ruleset other than the default
BusinessRules.RuleSet = "someruleset"
BusinessRules.AddRule(New SomeRule(Of MyClass))
' Set active ruleset to default
BusinessRules.RuleSet = "default"
End Sub
"someruleset" gets trigerred somewhere by:
BusinessRules.RuleSet = "someruleset"
BusinessRules.CheckRules()
BusinessRules.RuleSet = "default"
It works in 4.1, making the instance invalid (IsValid=False) as expected but it does not work in 4.2.x and 4.3.x. In 4.2.x and 4.3.x the BrokenRules collection seem to be cleared making the instance valid even though "SomeRule" was violated.
Anyway, I took a peek at the source code for 4.2.2 and found that within the BusinessRules class a few lines were added to clear the BusinessRules collection when the RuleSet property is set.
private string _ruleSet = null;
/// <summary>
/// Gets or sets the rule set to use for this
/// business object instance.
/// </summary>
public string RuleSet
{
get { return string.IsNullOrEmpty(_ruleSet) ? ApplicationContext.DefaultRuleSet : _ruleSet; }
set
{
_typeRules = null;
_typeAuthRules = null;
_ruleSet = value == ApplicationContext.DefaultRuleSet ? null : value;
if (BrokenRules.Count > 0)
{
BrokenRules.ClearRules();
}
}
}
If I comment it out, 4.2.2 behaves just like 4.1.
I am probably just missing something or failed to read the change log pertaining to 4.2 and above releases.
Any help will be greatly appreciated.
Thanks!
Got the answer from an earlier post:
Copyright (c) Marimer LLC