Business Properties with defaulting values from other properties

Business Properties with defaulting values from other properties

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


jhardman posted on Tuesday, June 28, 2011

I have a scenario where a property has a default value based on another property.  However, once the property has been set, the defaulting stops.  e.g.

      objPerson.FirstName = "M";
      Console.WriteLine(objPerson.KnownAs); //M
      objPerson.FirstName = "Michael";
      Console.WriteLine(objPerson.KnownAs); //Michael
      objPerson.LastName = "Jackson";
      Console.WriteLine(objPerson.KnownAs); //Michael Jackson
      objPerson.KnownAs = "Wacko"//defaulting for KnownAs now stops
      Console.WriteLine(objPerson.KnownAs); //Wacko
      objPerson.FirstName = "Mikey";
      Console.WriteLine(objPerson.KnownAs); //Wacko

How would I achieve this using the CSLA business properties?

JonnyBee replied on Wednesday, June 29, 2011

I'd probably do something like this:

    public static readonly PropertyInfo<string> FirstNameProperty = RegisterProperty<string>(c => c.FirstName);
    public string FirstName
    {
        get { return GetProperty(FirstNameProperty); }
        set { SetProperty(FirstNameProperty, value); }
    }
 
    public static readonly PropertyInfo<string> LastNameProperty = RegisterProperty<string>(c => c.LastName);
    public string LastName
    {
        get { return GetProperty(LastNameProperty); }
        set { SetProperty(LastNameProperty, value); }
    }
 
 
    public static readonly PropertyInfo<string> KnownAsProperty = RegisterProperty<string>(c => c.KnownAs);
    public string KnownAs
    {
        get {
            if (!FieldManager.FieldExists(KnownAsProperty))
                return string.Format("{0} {1}", FirstName, LastName).Trim();
            return GetProperty(KnownAsProperty); 
        }
        set { SetProperty(KnownAsProperty, value); }
    }

And add these rules to notify the UI of KnownAs has also changed:

      BusinessRules.AddRule(new Dependency(FirstNameProperty, KnownAsProperty));
      BusinessRules.AddRule(new Dependency(LastNameProperty, KnownAsProperty));

Once KnownAs is set the FieldManager.FieldExists will return true.

jhardman replied on Wednesday, June 29, 2011

Thanks mate, such a nice and elegant solution.  I was thinking along the lines of creating a business rule that modified the KnownAs property but was unsure how to turn off the defaulting.  With your solution, if there was a StringLength rule on the KnownAs property, would the rule be executed if it was using the default value?

JonnyBee replied on Wednesday, June 29, 2011

Yes, the Depency rule add properties to Affected properties of each PrimaryProperty (ie rule instance) and will make

So, yes you should be OK with adding a MaxLength rule to KnownAs property.

jhardman replied on Wednesday, June 29, 2011

Thanks mate.  Your solution is perfect.  I appreciate the time you spend on this forum.

Copyright (c) Marimer LLC