I've got a static class (DataFormSubject) that holds a generic List object, as follows:
private static List<DataForm> dataForms = new List<DataForm>();
Other classes that rely on this list need to be told when the list is updated, so I created a custom event, and associated methods that could trigger when an item is added or removed, as follows:
public delegate void DataFormsUpdatedHandler(object sender);
public static event DataFormsUpdatedHandler DataFormsUpdatedEvent;
public static void AddDataForm(DataForm df)
{
dataForms.Add(df);
if (DataFormsUpdatedEvent != null)
DataFormsUpdatedEvent(df);
}
public static void RemoveDataForm(DataForm df)
{
dataForms.Remove(df);
if (DataFormsUpdatedEvent != null)
DataFormsUpdatedEvent(df);
}
The List is available from the static class via a property, as follows:
public static List<DataForm> DataForms
{
get { return dataForms; }
//set { dataForms = value; }
}
But the problem here is that the client can now bypass the update event by accessing the property and doing a direct add or remove on the class! E.g.
DataFormSubject.DataForms.Add(new DataForm);
How can I prevent this, or is there a better way of acheiving what I want? Ideally what I would like is an update event on the List class that observers can subscribe to!
-
Consider using
BindingList<T>orObservableCollection<T>; both of these do what you want in standard ways.You cannot do anything interesting by subclassing
List<T>- the methods aren't virtual.Collection<T>is designed to be more extensible, but in this case everything you need is already provided (byBindingList<T>).In particular, this provides:
- IBindingList (used by many UI binding components, such as grids)
- ListChanged - that your code can subscribe to for detailed notification
Calanus : BindingList did the trick! -
You want to look into using an
ObservableCollectionfor such a purpose. See this MSDN article to get started. -
Don't show an IList from the static class, only show an Enumerable:
public static IEnumerable<DataForm> DataForms { get { return dataForms; } //set { dataForms = value; } }This way all changes must be done through your class
Marc Gravell : That doesn't help calling code get notification...gcores : The static class has the notification event.
0 comments:
Post a Comment