How to Set the Parent objects property when a child object collection was modified?

How to Set the Parent objects property when a child object collection was modified?

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


msrs_it posted on Saturday, September 25, 2010

Hi,

I'm using CSLA.NET 3.8.4. In my Business Library I have an object Quotation contains collection of items. Now, I want to update the Property 'GrandTotal' of Quotation object  whenever collection object modifies.

 

Thanks & Regards

msrs_it

JonnyBee replied on Saturday, September 25, 2010

Best way is to hook into the ChildChanged event in your Quotation object where you run a linq query on the collection to calculate GrandTotal and update the GrandTotal field.

 

msrs_it replied on Saturday, September 25, 2010

I have created an event in my BusinessBase class 'Quotation'

But the event I wrote is not firing. Below is the code from my object

 #region Factory Methods

        public static Quotation NewQuotation()
        {
            return DataPortal.Create<Quotation>();
        }

        public static Quotation NewQuotation(SalesRequest salesRequest)
        {
            return DataPortal.Create<Quotation>(salesRequest);
        }

        public static Quotation GetQuotation(int id)
        {
            return DataPortal.Fetch<Quotation>(
              new SingleCriteria<Quotation, int>(id));
        }

        public static void Delete(int id)
        {
            DataPortal.Delete(new SingleCriteria<Quotation, int>(id));
        }

        private Quotation()
        { /* Require use of factory methods */ }

        #endregion

#region Events

        private void Child_Changed(object sender, ChildChangedEventArgs e)
        {
            if (e.PropertyChangedArgs.PropertyName == "TotalPrice")
            {
                Total = 0m;
                foreach (QuotationItem quotationItem in Items)
                {
                    Total += quotationItem.TotalPrice;
                }
            }
        }

        #endregion

Thanks & Regards

msrs_it

JonnyBee replied on Sunday, September 26, 2010

Try and change to:

        #region Events

        protected override void OnChildChanged(object source,   PropertyChangedEventArgs propertyArgs,  ListChangedEventArgs listArgs)
        {

            base.OnChildChanged(source, propertyArgs, listArgs);

            if (propertyArgs != null)
            {
              if (propertyArgs.PropertyChangedArgs.PropertyName == "TotalPrice")
              {

                  Total = Items.Sum(p => p.TotalPrice);
              }
           }
        }

        #endregion

If you decide to use the Events as in your original code you must also hook into the event when your object is contructed or deserialized.

msrs_it replied on Monday, September 27, 2010

Thanks JonnyBee.

It's Working with small corrections as I wrote the code like below. [For your info: I'm using CSLA.NET 3.8.4]

        protected override void OnChildChanged(ChildChangedEventArgs e)
        {
            base.OnChildChanged(e);
            if (e.PropertyChangedArgs != null)
            {
                if (e.PropertyChangedArgs.PropertyName == "TotalPrice")
                {
                    Total = Items.Sum(p => p.TotalPrice);
                }
            }
        }

The override method OnChildChanged only accepts one argument that is ChildChangedEventArgs.

Anyway thanks a lot for your help. But I want to raise the Event 'Child_Changed' rather than overriding. As per your suggestion I'll try and get back to you.

Regards

msrs

Copyright (c) Marimer LLC