Problem with BusinessRules which affect two Properties

Problem with BusinessRules which affect two Properties

Old forum URL:

t.kehl posted on Saturday, October 30, 2010


I have the fallowing problem:
My BusinessObject has two Properties, which should be checked by a businessRule:

DefaultInchargeModeProperty - this is of type byte and has 0, 1 or 2 as value.
DefaultInchargeUserProperty - this is a ReadOnlyObject.

Now, when DefaultInchargeMode has the value 2, DefaultInchargeUserProperty must has a not null value.

I have now created the fallowing businessrule:

        private class CheckDefaultInchargeRule : BusinessRule {
            public CheckDefaultInchargeRule(IPropertyInfo primaryProperty)
                : base(primaryProperty) {
                    InputProperties = new List<IPropertyInfo> { primaryProperty, DefaultInchargeUserProperty };

            protected override void Execute(RuleContext context) {
                if ((byte)context.InputPropertyValues[PrimaryProperty] == (byte)DefaultInchargeModes.GivenUser) {
                    if(context.InputPropertyValues[DefaultInchargeUserProperty] == null) {
                        context.AddErrorResult(DefaultInchargeUserProperty, string.Format(Properties.Resources.RequiredRule, DefaultInchargeUserProperty.FriendlyName));

I create the rule like this:

this.BusinessRules.AddRule(new CheckDefaultInchargeRule(DefaultInchargeModeProperty));


The Rule does not working.

but when I change

context.AddErrorResult(DefaultInchargeUserProperty, string.Format(Properties.Resources.RequiredRule, DefaultInchargeUserProperty.FriendlyName));


context.AddErrorResult(string.Format(Properties.Resources.RequiredRule, DefaultInchargeUserProperty.FriendlyName));

It works. why this? - I need in the Result the DefaultInchargeUserProperty for coloring the UI. I check, if there is a Binding from the view to the Property in the ErrorResult.

I hope someone can help me.



JonnyBee replied on Monday, November 01, 2010

Hi Thomas,

I'll try to give me views to your rule here:

1. You need a business rule to check the property DefaultInchargeUserProperty

when DefaultInchargeMode has the value 2, DefaultInchargeUserProperty is required.

2. You have a dependency on the value of another property DefaultInchargeModeProperty

To solve this I would create the following rule for validatin DefaultInchargeUserProperty:

    public class CheckInCharge : Csla.Rules.CommonRules.Required
        private IPropertyInfo _chargeModeProperty;
        public CheckInCharge(IPropertyInfo primaryProperty, IPropertyInfo chargeModeProperty) : base (primaryProperty)
            _chargeModeProperty = chargeModeProperty;

            if (InputProperties == null) InputProperties = new List<IPropertyInfo>();

        public void Execute(RuleContext context)
            if ((byte)context.InputPropertyValues[_chargeModeProperty] == (byte)DefaultInchargeModes.GivenUser)

And in AddBusinessRules:

      this.BusinessRules.AddRule(new CheckInCharge(DefaultInchargeUserProperty));
      this.BusinessRules.AddRule(new Dependency(DefaultInchargeModeProperty, DefaultInchargeUserProperty));

 to make sure that if DeaultInchargeMode is changed then rules for DefaultInchargeUser field is rerun.

t.kehl replied on Monday, November 01, 2010

Hi JonnyBee

Thank you. This is now working fine.

Best Regards, Thomas

rfcdejong replied on Tuesday, November 02, 2010

This would perhaps work with my ConditionProperty

// When InChargeMode = GivenUser then DefaultInchargeUserProperty is required
BusinessRules.AddRule(new ConditionRule<Required, DefaultInchargeModes>(
                            DefaultInchargeModeProperty, (p) => p ==
new Required(DefaultInchargeUserProperty)));

Copyright (c) Marimer LLC