Recommendation on Readonly Property dependant of managed Property

Recommendation on Readonly Property dependant of managed Property

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


alainrodriguez posted on Wednesday, October 24, 2012

 

I have the following case, on which a calculated property, WeekdaysToExpectedStartDate depends on
StartDateProperty and current date.

WeekdaysToExpectedStartDate must be binded on the UI as readonly, meaning an change on the StartDateProperty 
should refresh the WeekdaysToExpectedStartDate readonly property.
See the code below: public static readonly PropertyInfo<SmartDate> StartDateProperty = RegisterProperty<SmartDate>(c =>
c.StartDate, "Start Date", RelationshipTypes.PrivateField); [Trim] public string StartDate { get { return GetPropertyConvert<SmartDate, string>(StartDateProperty, _StartDate); } set { SetPropertyConvert<SmartDate, string>(StartDateProperty, ref _StartDate, value); } } public int WeekdaysToExpectedStartDate { get { return GetWeekdaysInBetween(ReadProperty(StartDateProperty).Date, DateTime.Today); } } I understand that I could do the above or create a member variable for the
WeekdaysToExpectedStartDate, and create a business rule to set it with the calculated value,
but this seems rather extensive for a simple calculation.
..
What is the recommended way in this case?

Thanks!

 

skagen00 replied on Wednesday, October 24, 2012

For something like this I've typically just captured the property change of StartDate in PropertyHasChanged of the BO and just invoke OnPropertyChanged("WeekdaysToExpectedStartDate") in that case to notify UI to refresh its databinding.

JonnyBee replied on Thursday, October 25, 2012

Skagen's solution is what I have used from time to time too!!

You can also do this within the rule engine if you add a PropertyInfo for the WeekdaysToExpectedStartDateProperty and a rule like this:

  public class RaisePropertyChanged : Csla.Rules.BusinessRule

  {

    public RaisePropertyChanged(IPropertyInfo primaryProperty, params IPropertyInfo[] raiseChangedProperties)

      : base(primaryProperty)

    {

      AffectedProperties.AddRange(raiseChangedProperties);

    }

  }

 

Usage:

 

  BusinessRules.AddRule(new RaisePropertyChanged(StartDateProperty, WeekdaysToExpectedStartDateProperty));

 

So it's down to your own coding preferences to select one of the solutions.

alainrodriguez replied on Thursday, October 25, 2012

Thank you both... it helped a lot.. I am sticking with JB solution... Thanks!

alainrodriguez replied on Thursday, October 25, 2012

JonnyBee, when you mean add a PropertyInfo, just as:

 

 public static readonly PropertyInfo<intWeekdaysToExpectedStartDateProperty = RegisterProperty<int>(c => c.WeekdaysToExpectedStartDate
    "Weekdays To Expected Start Date"RelationshipTypes.PrivateField);
  
  public int WeekdaysToExpectedStartDate
    {
      get { return GetWeekdaysInBetween(DateTime.Today, _startDate); }
    }




JonnyBee replied on Thursday, October 25, 2012

Yes, that is what I mean.

Copyright (c) Marimer LLC