My two properties are a bool? and a string.
The Rule is that if bool = true then the string must not be empty.
I can use a single BusinessRule class for both Properties by using two Constructor overloads.
But the PropertyStatus control can only reflect the state of one property. SL's built-in mechanism has the same issue.
I was thinking of creating a single property in the ViewModel that could reflect the state of the two Model properies, and then attach the PropertyStatus control to it. But perhaps there is a simpler solution. I have a number of these kind of couplets.
Jav
Create your own BusinessRule that inherits from Required rule and add a Dependency rule from BoolProperty to SomeProperty - something like this:
public class RequiredWhenOtherIsTrue : Csla.Rules.BusinessRule
{
private IPropertyInfo _boolProperty;
public RequiredWhenOtherIsTrue(IPropertyInfo primaryProperty, IPropertyInfo boolProperty) : base(primaryProperty)
{
_boolProperty = boolProperty,
if (InputProperties == null) InputProperties = new System.Collections.Generic.List<IPropertyInfo>();
InputProperties.Add(boolProperty);
}
protected override void Execute(Csla.Rules.RuleContext context)
{
var booltest = (bool) context.InputPropertyValues[_boolProperty];
if (booltest)
base.Execute(context);
}
}
protected override void AddBusinessRules()
{
// call base class implementation to add data annotation rules to BusinessRules
base.AddBusinessRules();
BusinessRules.AddRule(new RequiredWhenOtherIsTrue(SomeProperty, BoolProperty);
BusinessRules.AddRule(new Dependency(BoolProperty, SomeProperty);
}
Johny,
Thank you. I think I get it. Rocky does mention the Dependency rule in his Objects eBook. Should have looked in there first.
Jav
Copyright (c) Marimer LLC