AddOutValue not working if rule triggered by Dependency

AddOutValue not working if rule triggered by Dependency

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


Russ posted on Monday, October 03, 2011

The AddOutValue in the following rule correctly updates the TextProperty when the user changes the String1Property however it does not update the TextProperty when the user changes the String2Property or the String3Property. The Debug line does output the correct buf string with every user change.

 

#region Business Rules

 

protected override void AddBusinessRules()

{

  base.AddBusinessRules();

 

  BusinessRules.AddRule(new UpdateTextRule(String1Property, String2Property, String3Property) { Priority = 0 });

  BusinessRules.AddRule(new Csla.Rules.CommonRules.Dependency(String2Property, String1Property) { Priority = 0 });

  BusinessRules.AddRule(new Csla.Rules.CommonRules.Dependency(String3Property, String1Property) { Priority = 0 });

}

 

private class UpdateTextRule : Csla.Rules.BusinessRule

{

  IPropertyInfo SecondaryProperty { get; set; }

  IPropertyInfo TertiaryProperty { get; set; }

 

  public UpdateTextRule(IPropertyInfo String1Property, IPropertyInfo String2Property, IPropertyInfo String3Property)

    : base(String1Property)

  {

    PrimaryProperty = String1Property;

    SecondaryProperty = String2Property;

    TertiaryProperty = String3Property;

 

    InputProperties = new List<IPropertyInfo> { String1Property, String2Property, String3Property };

    AffectedProperties.Add(TextProperty);

  }

 

  protected override void Execute(RuleContext context)

  {

    var s1 = context.InputPropertyValues[PrimaryProperty].ToString();

    var s2 = context.InputPropertyValues[SecondaryProperty].ToString();

    var s3 = context.InputPropertyValues[TertiaryProperty].ToString();

 

    string buf = string.Format("{0} {1} {2}", s1, s2, s3);

    System.Diagnostics.Debug.WriteLine(buf);

    context.AddOutValue(TextProperty, buf);

  }

}

 

#endregion

 

Has anyone else ever seen this strange behavior?

 

Thanks

Russ

 

JonnyBee replied on Thursday, October 06, 2011

Hi,

Which version of CSLA are you using?

Priority = 0 may be left out, thıs ıs the default value.

My personal preference would be to have the TextProperty as primary property wıth a params array for input properties (any number of fields).

Create a sting array and use Join to create a comma separated list and add as output value.

JonnyBee replied on Thursday, October 06, 2011

I did this test with the latest CSLA 4.2 beta, without dependency rules, only change to the rule is this line:

System.Diagnostics.Debug.Print("rule:{0}", buf);

and this program on a root object with the 4 string properties:
      var root = Root.NewEditableRoot();
      root.String1 = "S1";
      Debug.Print("root:{0}", root.Text);
      root.String2 = "S2";
      Debug.Print("root:{0}", root.Text);
      root.String3 = "S3";
      Debug.Print("root:{0}", root.Text);

and got the following output on debug window:
rule:S1
root:S1
rule:S1 S2
root:S1 S2
rule:S1 S2 S3
root:S1 S2 S3

However, a fully general rule could be written like this:
    protected override void AddBusinessRules()     {       // call base class implementation to add data annotation rules to BusinessRules        base.AddBusinessRules();       BusinessRules.AddRule(new UpdateTextRule(TextProperty, String1Property, String2Property, String3Property));     }     public class UpdateTextRule : Csla.Rules.BusinessRule     {       public UpdateTextRule(IPropertyInfo primaryProperty, params IPropertyInfo[] valueProperties)         : base(primaryProperty)       {         InputProperties = new List<IPropertyInfo>(valueProperties);       }       protected override void Execute(RuleContext context)       {         string[] values = context.InputPropertyValues
.Where(p => p.Key != PrimaryProperty)
.Select(p => p.Value.ToString())
.ToArray();         var buf = String.Join(" ", values);         System.Diagnostics.Debug.Print("rule:{0}", buf);         context.AddOutValue(buf);       }     }

Russ replied on Thursday, October 06, 2011

Hi Jonny,

Your example is very elegant.  I have upgraded from 4.1 to 4.2 beta and it works great!  I also noticed my application seems to run a little bit faster!

Thanks again
Russ.

Copyright (c) Marimer LLC