views:

33

answers:

2

Implementing Document-View pattern I faced the problem: standard generic collections and dictionaries (List<T>, HashSet<T> for example) don't provide events for modification (OnBeforeAdd, OnAfterAdd, OnRemove, Freeze/Unfreeze methods... )

I suppose events were not implemented for optimization purposes but I have to use and listen for such events using Document class.

I searched Inet for a while and found some demo implementations for lists. Is there well-known production-proved library with complete set of eventable generic collections/dictionaries or shall I implement such collections by myself?

Thank you in advance!

+1  A: 

you can wrap container to your own class and add events to that class.

Arseny
ah yes, but I suppose this is so common task that these wrappers were implemented and tested many years ago...
Andrew Florko
finally I created implementation of IList<T> for my purposes as guys discussed here: http://weblogs.asp.net/rmclaws/archive/2005/11/14/430575.aspx
Andrew Florko
+1  A: 

Have you considered System.Collections.ObjectModel.ObservableCollection<T>?

From MSDN:

Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

I see this type of collection is used a lot in WPF and Silverlight for it's ability to raise events when data in the collection changes. This allows for rich databinding where the UI is updated based on the events raised by ObservableCollection<T>.

Ben McCormack
Thank you for the reply. It's a pity, but this collection doesn't have "free/unfreeze" methods. I mean if I have to add 1'000 elements and receive only one finalizing event (collectionChanged) instead of 1'000 events I have to implement it by myself.
Andrew Florko
@Andrew I'm confused by two things: 1) When you say "this collection," are you referring to `ObservableCollection` or something else? 2) What do you mean by "free/unfreeze" methods? I guess I'm wondering what's missing from `ObservableCollection` so that it doesn't fit your needs.
Ben McCormack