Running Total in Parent object

Running Total in Parent object

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


RangerGuy posted on Thursday, August 24, 2006

I want to keep a running total in my order object so that every time a orderdetail is updated,added or deleted it fires an event that changes the "sub total" of an order. So that I don't have to loop thru the order everytime I want the sub - total.

So I am wondering would it be ok to create 2 events AddToSubTotal and MinusFromSubTotal then fire these events in the add & remove methods of the child collection (orderdetails) which updates the SubTotal property of the parent (Object) order.

 

SonOfPirate replied on Thursday, August 24, 2006

A more general way of accomplishing this is to add a PriceChanged event to your OrderDetail object and have the OrderDetailCollection register a handler each time an item is added.  Then you can have a PriceChangedEventArgs expose properties such as: OldPrice, NewPrice, ChangeInPrice, that can be used to adjust the subtotal.

You can issue another PriceChanged event from the OrderDetailCollection to the parent Order object which has registered a handler for the collection's event that would update the Order's SubTotal property...

or,

Make the SubTotal a property of the OrderDetailsCollection and change the Order object to delegate its SubTotal property to return the collection's property.  This would eliminate the need for cascading the event up to the Order object.

The first approach (maybe both) would be consistent with the automation model-type approach where objects expose changed events for each property and simplifies things because you don't have to include logic to determine if the change is an addition or subtraction as it is handled through the sign of the ChangeInPrice property (or by NewPrice - OldPrice, which is all ChangeInPrice would be anyway).

Hope that helps.

Q Johnson replied on Thursday, August 24, 2006

Or you could just put the SubTotal property in the collection and let it increment/decrement it in its own Add/Remove methods.

 

SonOfPirate replied on Thursday, August 24, 2006

You'll want to use an event-driven approach if your OrderDetail objects are editable.  You can't capture changes to the item with Add/Remove in the collection.

RangerGuy replied on Thursday, August 24, 2006

Don't know why I couldn't see how to do this... not enough coffee maybe LOL!

I thought I'd post this to share how i figured it out :) I started to think creating events and handlers seemed overly complex.

class Order : BusinessBase<Order>
{

    private OrderDetails _OrderDetails = OrderDetails.NewOrderDetails();

    public double SubTotal
    {
        get{ return _OrderDetails.SubTotal; }
    }
 
}

class OrderDetails : BusinessListBase<OrderDetails ,OrderDetail>
{

    private double _SubTotal = 0.0;

    public double SubTotal 
    { 
        get 
        { 
            return _SubTotal; 
         } 
    }

 public void AssignProduct(ProductInfo product, int qty)
 {
     foreach (OrderDetail od in this)
     {
         if (!Contains(product, qty))
         {
             OrderDetail newOrderDetail = OrderDetail.NewOrderDetail(product, qty);

             //Update Running Sub Total
             _SubTotal = _SubTotal + (product.Cost * qty);
             this.Add(newOrderDetail)
         }
     }
 }

 public void RemoveProduct(ProductInfo product, int qty)
 {
     foreach (OrderDetail od in this)
     {
         if (od.product.equals(product) && od.qty = = qty)
         {
             //Update Running Sub Total
             _SubTotal = _SubTotal - (product.Cost * qty);
             remove(od);   }
           }
       }
 }

SonOfPirate replied on Thursday, August 24, 2006

Keep in mind that this assumes your OrderDetail objects are read-only.  If you allow the user to makes changes, such as to the Quantity property, you will need some way to capture the change made at the item level in your collection.  That is why the event approach is more commonly used.

But, if it is read-only, then this will work just fine.

RangerGuy replied on Thursday, August 24, 2006

RIGHT! Totally wasn't thinking of the properties for order details DOH!

Thanks!

I think I'm going to have to start paying you SonOfPirate LOL!

Copyright (c) Marimer LLC