Quantcast
Channel: CSLA .NET
Viewing all articles
Browse latest Browse all 764

Calculating Rules for two Dependend Properties

$
0
0

CSLA Version 4.1

Hello !

I have two Properties: one called DeliveryWeek (LieferKW) and one called DeliveryDate (Liefertermin).
Further i have two Rules which calculate a DeliveryDate (Friday of Week) from DeliveryWeek
and one Rule that calculate a DeliveryWeek vom DeliveryDate.
The User must can change both (vice versa) in the WPF UI and the other Field must be updated.
The Problem is, that when the user enter a new DeliveryDate the CalculateLieferterminRule runs first
and reset the DeliveryDate.
What is the right Way to setup the Rules ?

My Code:
        protected override void AddBusinessRules()
        {
            base.AddBusinessRules();

            //Some Other Rules for Required Fields
           
            //BusinessRules.AddRule(new Csla.Rules.CommonRules.Dependency(LieferKWProperty, LieferterminProperty));
            //BusinessRules.AddRule(new Csla.Rules.CommonRules.Dependency(LieferterminProperty, LieferKWProperty));
            BusinessRules.AddRule(new CalculateLieferterminRule(LieferterminProperty, LieferKWProperty){Priority = 1});
            BusinessRules.AddRule(new CalculateLieferKWRule(LieferKWProperty, LieferterminProperty) { Priority = 1 });

        }
       
        private class CalculateLieferterminRule : BusinessRule
        {
            private IPropertyInfo LieferKWProperty;
           
            public CalculateLieferterminRule(IPropertyInfo primaryProperty, IPropertyInfo lieferKWProperty) : base(primaryProperty)
            {
                if (InputProperties == null)
                {
                    InputProperties = new List<IPropertyInfo>() {primaryProperty, lieferKWProperty};
                }

                PrimaryProperty = primaryProperty;
                LieferKWProperty = lieferKWProperty;
                AffectedProperties.Add(PrimaryProperty);

            }

            protected override void Execute(RuleContext context)
            {
                var lieferKw = Convert.ToInt32(context.InputPropertyValues[LieferKWProperty]);
                DateTime? liefertermin = DateTimeExtension.GetLastBusinessDateOfWeek(lieferKw);

                context.AddOutValue(PrimaryProperty, liefertermin);
            }

        }

        private class CalculateLieferKWRule : BusinessRule
        {
            private IPropertyInfo LieferterminProperty;
           
            public CalculateLieferKWRule(IPropertyInfo primaryProperty, IPropertyInfo lieferterminProperty) : base(primaryProperty)
            {
                if (InputProperties == null)
                {
                    InputProperties = new List<IPropertyInfo>() {primaryProperty, lieferterminProperty};
                }

                PrimaryProperty = primaryProperty;
                LieferterminProperty = lieferterminProperty;
                AffectedProperties.Add(PrimaryProperty);
            }

            protected override void Execute(RuleContext context)
            {
                var liefertermin = Convert.ToDateTime(context.InputPropertyValues[LieferterminProperty]);
                int lieferKW = DateTimeExtension.GetCalendarWeekWithYear(liefertermin);

                context.AddOutValue(PrimaryProperty, lieferKW);
            }
        }

 

 


Viewing all articles
Browse latest Browse all 764

Trending Articles