Collection questions

Collection questions

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


ajj3085 posted on Monday, September 25, 2006

Hi,

I have a ProductBundle class, which allows the user to specify if the price is calculated (by adding the prices of its components) or if the price is fixed (specified by the user).

The system allows any number of price lists as well.  So when editing a product bundle, the user can edit prices for ALL of the lists at once.  The price list and associated unit price are edited through a grid.

Now for the fun.  The Prices collection will always contain a Price object for each PriceList, even if no price is defined for the list.  When the bundle's price type is set to Calculated, the Prices list is cleared.  This works fine.  What I'd like to do is restore the list to the state its before being cleared when PriceType is set back to Fixed. 

My first idea is to simply add an internal method to the collection which will move everything in the DeletedList back to the main list.  I think this should work, since Adding and Removing objects to and from the collection is not allowed by public members. 

Thoughts or other suggestions?
Andy

skagen00 replied on Monday, September 25, 2006

Just a suggestion, as I didn't fully understand everything you were saying:

In your collection get you could also check to see if pricetype=calculated, and if so the get on the collection could just return an empty collection - if it's not calculated, it returns the actual collection. That way you wouldn't need to tinker with the collection itself.

 

ajj3085 replied on Monday, September 25, 2006

That would cause the collection class to be tightly coupled to the parent.. which won't work because the collection is also used by the Product class which doesn't have a PriceType property.

jkellywilkerson replied on Monday, September 25, 2006

Could you have the object .Clone() itself and implement some sort of Backup/Restore feature within the BO itself?  Not sure what that would look like or if it would even work.  Just a thought from "left field".

Kelly.

ajj3085 replied on Tuesday, September 26, 2006

Kelly,

That's what I thought too.  I think I came up with a workable solution that offers better performance.

Basically, the client code can never add or remove items from the collection.  So what I did is add some internal methods, ClearPrices and UnclearPrices. 

Basically what happens is this; when the bundle's price is set to calculated, it calls clear, which simply moves everything to DeletedList.  When set to unclear, all items in DeletedList are moved back to the 'real' list. 

The tricky part was figuring out the proper behavior when the list is saved.  When I save either everything is in DeletedList in which case the proper row is removed and the object is set to New, or its in the main list and items are inserted / updated as needed. 

I never clear DeletedList though; the reason is that after a save, if the user sets the price back to Fixed, I need to have those objects.. so I just move them out of the deleted list.

So far it works pretty well, and gives the user a better experience than having to remember 'add' a new price list to the Prices and set its price.  The price list is just always there ready to be filled out.

jmbowles replied on Tuesday, September 26, 2006

This is my first post, so, take it easy on me ...

These are all good suggestions.  However, I look at the problem in a different way.  If I understand correctly, the ProductBundle's calculation behavior can change at runtime. I would configure a ProductBundle with a strategy (i.e. IProductCalculationStrategy) that knows how to calculate the costs.  Each strategy can then determine what values or lack thereof are present and calculate accordingly.

ajj3085 replied on Tuesday, September 26, 2006

Welcome to the board!

That's a good idea when I need to calculate the price of the bundle or even non-bundle, which I will have to do in other use cases. 

At this point when editing the bundle, the price isn't shown if Calculated is selected; its just a flag for this particular use case.

Copyright (c) Marimer LLC