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
{ { context.AddErrorResult(CityProperty, "City is required"); } }
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");
}
}
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 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 { get; private 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));
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 .
Thank you both very much, it now works!
Copyright (c) Marimer LLC