I've read the eBook, but I'm not totally clear on when a property will be considered dependent. The way its worded makes it sound like if a rule calls AddOutResult then any propery it changes will be considered affected, but what about just using a property as an input?
The scenario I have is this. Two properties, A and B. The validation rule is that both must be empty or both must contain a value. I've created a rule which does this, but is the fact that both A and B are input properties enough to ensure that when B changes A is checked as well? Or do I need to explicitly setup the dependency?
Pre Csla 4.2 you must add explicit Dependency rules for InputProperties.
This is changed for Csla 4.2 to consider InputProperties as automatic Dependency so you will not need to add Dependency rules.
And beware that the actual Dependency is the rule.AffectedProperties, ie: the depencies is all the properties in AffectProperties list whether you call AddOutValue or not. The property must however be in AffectedProperties or you will get an exception when calling AddOutValue.
So if you want an error icon on both field you could create a rule like this:
using System.Collections.Generic; using Csla.Core; using Csla.Rules; using System.Linq; namespace MyBusiness.Rules { public class RequiredWhenAnyHasValue : Csla.Rules.CommonRules.Required { public RequiredWhenAnyHasValue(IPropertyInfo primaryProperty, params IPropertyInfo[] additionalRequired) : base(primaryProperty) { if (InputProperties == null) InputProperties = new List<IPropertyInfo>(); InputProperties.AddRange(additionalRequired); } protected override void Execute(RuleContext context) { var anyHasValue = context.InputPropertyValues.Any(p => !string.IsNullOrEmpty(p.Value.ToString())); if (anyHasValue) { base.Execute(context); } } } }
usage:
protected override void AddBusinessRules() { // call base class implementation to add data annotation rules to BusinessRules base.AddBusinessRules(); BusinessRules.AddRule(new RequiredWhenAnyHasValue(String1Property, String2Property)); BusinessRules.AddRule(new RequiredWhenAnyHasValue(String2Property, String1Property)); BusinessRules.AddRule(new Dependency(String1Property, String2Property)); BusinessRules.AddRule(new Dependency(String2Property, String1Property)); }
Thanks, I ended up figuring this out on my own. So I'm using the Using Csla 4 ebooks to guide me; which version of Csla are they talking about?
Copyright (c) Marimer LLC