Search in BusinessListBase for a member

Search in BusinessListBase for a member

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


shaih posted on Monday, December 17, 2007

Hi,

I was wondering why there is no efficient search built into the framework for the BusinessListBase class?

Is the proper way to have in addition a NameValueListBase collection and performs the searches on this collection - in my scenario I have several lists that in order to update (update and/or remove) items from the list I do not want to go over the entire list searching for the item.

Is the way around this is to hold a NameValueListBase<Guid, MyObj>?

 

Thanks

Shai 

RockfordLhotka replied on Monday, December 17, 2007

BLB is a collection. Collections (and lists) are just linear lists of data.

Adding indexing costs performance and memory. Most applications would never recoup that cost, because they wouldn't do enough lookups against the list to make the cost of building/maintaining an index cheaper than a linear scan.

NVLB is effectively a dictionary. Dictionaries (and hashtables) are keyed lists of data, where the key is stored in a hash table for quick lookup. Hashtables cost memory, but have little performance cost, so they are often a nice compromise. Unfortunately they are not bindable using data binding - you can't bind a hashtable or dictionary to a grid for example.

In CSLA .NET 3.5 we're implementing indexing in BLB, so LINQ queries can use an index. This is an optional feature, designed to have no impact (or as little as possible) if you don't opt-in to using the feature. But if you know you will be doing a lot of equality-based queries against a specific property of the child objects in a BLB then the indexing is well worth it.

So the way around the issue in your case may be to maintain an index. In a specific case this is easy. Specific meaning that you can code a specific collection to index a specific property of the child objects.

All you need to do is override the insert/remove/set methods from the base class so you can update your index as items are inserted, removed and changed in the collection. Oh, and you need to handle PropertyChanged from all children and update your index then too.

Your index could be a simple dictionary - keyed by your property value, with each item's value being the corresponding child object.

Then you can implement a Find() method on your collection that does a lookup into the dictionary based on the key value.

Copyright (c) Marimer LLC