CSLA 4 Preview - Design and validation question

CSLA 4 Preview - Design and validation question

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

Curelom posted on Friday, April 23, 2010

I have a businesslist called ForecastList containning Forecast business objects with a WeekStartDateProperty and a decimal Data property.  My customer wants a differential percentage warning if the data from one week is changed too much the next week.  I have done this using the BusinessRule below.  It works great, however, I don't know how to take care of the dependency that if the current week was changed to much and fires the warning, if the user goes and changes the previous week to comply, the business rule isn't fired again and the warning still shows unless the current week itself is changed. 

I hope that all made sense.  Anyway how do I handle the dependency issue or do I need some major design changes?

    public class MyRule : Csla.Rules.BusinessRule {
        public MyRule(Csla.Core.IPropertyInfo primaryProperty)
            : base(primaryProperty) {
            InputProperties = new List<Csla.Core.IPropertyInfo> { primaryProperty };
        protected override void Execute(RuleContext context) {           
            var target = (Forecast)context.Target;
            if (target.Parent != null) {
                ForecastList fl = (ForecastList)target.Parent;

                var q = (from f in fl
                         orderby f.WeekStartDate
                         where f.WeekStartDate.Date == target.WeekStartDate.Date.AddDays(-7)
                         select new { WeekStartDate = f.WeekStartDate, Data = f.Data }).FirstOrDefault();
                //right now just asking if this week is greater than last week for testing
                //to be replaced with the percentage differential later
                if (q != null && q.Data < target.Data) {
                    context.AddWarningResult("Percentage off Warning");

Curelom replied on Wednesday, April 28, 2010

I have modified this to not use a business list, but rather properties for each of the forecast objects.  i.e.  The SkuForecast object has Forecast properties of ForecastWeek1, ForecastWeek2, ForecastWeek3, etc.  I am then invoking businessrules to these

BusinessRules.AddRule(new PercentageDerivativeRule(ForecastWeek2Property, ForecastWeek1Property));
BusinessRules.AddRule(new PercentageDerivativeRule(ForecastWeek3Property, ForecastWeek2Property));
BusinessRules.AddRule(new PercentageDerivativeRule(ForecastWeek4Property, ForecastWeek3Property));


The problem I have now is that the business rules aren't firing.  The forecast objects aren't changing themselves, but rather a Data property on the forecast object.  Is there a way to get this to "bubble up" to trigger validation on the SkuForecast object?

RockfordLhotka replied on Wednesday, April 28, 2010

You'll probably need to handle the ChildChanged event and call BusinessRules.CheckRules(ForecastWeek1Property) and so forth as the child objects change.

Curelom replied on Wednesday, April 28, 2010

Thank you.  This is working.  I wasn't able to infer which property was called as the Property stated in the ChildChangedEventArgs show "Data" which is the Childs property that was changed, not the master object's property.  So I run BusinessRules.CheckRules() to run them all.  Not as efficient, but it works.

RockfordLhotka replied on Wednesday, April 28, 2010

The original PropertyChangedEventArgs should be part of the child changed event args you get, and that should contain the original property name from the child.

Copyright (c) Marimer LLC