Just checking: BindingList<T> is the only generic list supportingchange notifications?

S

Steve K

I've just recently added support for IChangeTracking to my business
objects and came up against a bit of a snag. I have many List<T> fields
in my objects that are exposed as properties. My application can get a
reference to the lists and do just about anything with them.... all of
this happening without my "Dirty" monitoring logic being the wiser.

I have implemented my IChangeTracking stuff the way many do, by rolling
it together with INotifyPropertyChanged logic and adding an
UpdateField() method to my base class. Basically all my setters call
this method and if the new value is different than the current value I
raise the changed event and set the dirty flag dirty.

Simple.

Back to my lists; I can think of two ways to hook into changes to them
in order to have them effect the "dirtyness" of my objects"
1) Hide the List<> methods behind methods of my objects, IE:
MyObject.AddItemToList(object item)
{
_list.Add(item);
UpdateDirtFlag();
}

2) Switch from List<T> to a collection that supports change
notifications and add handlers to these events in my objects
constructor, IE:
class MyObject
{
private IBindingList<int> _someInts;

public MyObject()
{
_someInts.ListChanged += delegate(object sender,
ListChangedEventArgs args)
{
UpdateDirtyFlag();
};
}
}

#1 is going to add a lot of bloat (and safety!) to my objects and I can
imagine I will be writing the same code over and over and over.

#2 is the most attractive to me although I'm not *really* sure why, I've
fallen in love with events lately and I think that might have something
to do with it ;0)


I've become quite fond of List<T> and the change to BindingList<T>
doesn't appear to be a simple one.

I'm wondering if there is another list that I haven't found yet, maybe
something a little obscure that has the same characteristics of List<T>
with change notification or eventing.

A third option I've thought of is to make my own List derived from
List<T> and implement the IRaiseItemChangedEvents interface.


Have any of you had a similar requirement as mine? Knowing when the
state of your objects lists change so you can handle things internally?

Any suggestions or thoughts welcome.
 
M

Marc Gravell

Out of the 2.0 box, yes. There may be some 3.0 collections that support
it, but for you purposes BindingList<T> is the simplest (it is
pretty-much a bare-bones list with change support, and [virtual]
extension points if you want sorting etc).

If needs be, you can also do this yourself... implement IBindingList and
provide sensible implementations the event and property (the rest you
need to fail-safe, which can be a pain).

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top