Doing Calculations in FilteredBindingLists

Doing Calculations in FilteredBindingLists

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


pirithoos posted on Tuesday, June 26, 2007

Hi all,

I do have a question about design.

My application has a ReadOnlyList called ReadOnlyOrderList containing OrderInfos.
This list has some shared methods to e.g. sum the turnover off all contained OrderItems or another
one which counts the total number of Orders already shipped.

Know I started to use a FilteredBindingList as view to extract Orders by certain criteria such
e.g. only Orders with a turnover > 10.000 EUR.

My question is what is the best practise to allow using the sum/count methods over the OrderInfoBO's within those Views?

Shall I inherit a subclass from FilteredBindingList implemeting those shared methods
or create a new class consuming either ReadOnlyOrderLists or FilteredOrderBindingLists
doing the sums/calculation and return them via a function/property? Or any different approach?

What is the recommended implementation in this scenario?

Thanks for your feedback,

Frank



RockfordLhotka replied on Tuesday, June 26, 2007

You want to make sure to normalize your behaviors. Something like Count is already normalized by .NET - it is a property on every collection. But Sum is not, so you need to write it.

In .NET 3.5 you'll be able to extend IBindingList with a Sum method, but today you can't do that, and instead must write a normal (typically static/Shared) method instead.

Your only question then, is whether to add a Sum method to your business lists and have that method delegate to your shared Sum implementation, or to have your code directly use that shared method.

sum = OrderInfos.Sum()
sum = CustomFiltered.Sum()

or

sum = OrderInfos.Sum(list)

where "list" is the list containing the data to sum - filtered or not.

This second approach, btw, lends itself more to the extender method in .NET 3.5 - it'll be easier to port/convert.

pirithoos replied on Wednesday, June 27, 2007

Dear Rocky,

thanks for your reply.

In fact count is normalized in .Net and I use this property to get the total number of OrdersInfos
in my collection. But sometimes I need the count of only those OrderInfos whicht are
shipped (though all either shipped or not shipped OrderInfos are in the same collection),
therefore I need to implent an additional Count for ShippedOrders...

I followed up your second approach and spend my ReadOnlyListBase 'OrderInfos' some  shared functions like

GetSumTurnover( ByVal IList (of OrderInfo) as Double
GetSumOfShippedOrder (ByVal IList (Of OrderInfo) as Integer

those functiona accepts either OrderInfos or the FilterBindingList Views of OrderInfos.

This works fine. However one more thing needing a solution:

There are currently approx 5 different kinds of sums / counts and more to come...
though it is no problem writing simmilar GetSumXYZ/GetCountXYZ methods like the above, this would implicit that we loop 5 or more times through the same collection to build those sums/counts.

Before using the FilteredBindingList exposed all sums/count by ReadOnlyProperties
of the OrderInfos collection and when after within the Factorymethod returning
this OrderInfoList I temporarily stored the list and called a sub looping through all
OrderInfos and build the individual sums/count within a select clause later accessed
through those Properties.

Now where my OrderInfos collection has two factorymethods one returning a 'ReadOnlyBindingList'
one returning a 'FilteredBindingList' this is no longer possible at least as long as I do
not implement the same sub in both (ReadOnlyBinding and FilteredBindingList)

Any further suggestions for a elegant solution?

Best Regards

Frank

Copyright (c) Marimer LLC