Problem with BusinessRules which affect two Properties

Problem with BusinessRules which affect two Properties

Old forum URL: forums.lhotka.net/forums/t/9691.aspx


t.kehl posted on Saturday, October 30, 2010

Hi.

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 };
                    AffectedProperties.Add(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));

to

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.

Thanks.

Thomas

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>();
            InputProperties.Add(chargeModeProperty);
        }

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

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

http://forums.lhotka.net/forums/t/9699.aspx

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

Copyright (c) Marimer LLC