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 StartDatePropertyshould 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!
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.
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.
Thank you both... it helped a lot.. I am sticking with JB solution... Thanks!
JonnyBee, when you mean add a PropertyInfo, just as:
public static readonly PropertyInfo<int> WeekdaysToExpectedStartDateProperty = RegisterProperty<int>(c => c.WeekdaysToExpectedStartDate,
"Weekdays To Expected Start Date", RelationshipTypes.PrivateField);
public int WeekdaysToExpectedStartDate { get { return GetWeekdaysInBetween(DateTime.Today, _startDate); } }
Yes, that is what I mean.
Copyright (c) Marimer LLC