collectionbase

rmatthew

Centurion
Joined
Dec 30, 2002
Messages
115
Location
Texas
I am working on a strongly typed collection using collectionbase

When I change a property of an item in the collection (such as item.name) I want to validate it against the collection so as to prevent duplicate property values.

Similar to the way when you change the name of a datatable that belongs to a datatablecollection, a DuplicateNameException is trown.

Is there a recommended/standard way to accomplish this?
 
I'm not sure about standard ways of handling this but a few options that come to mind are...

1. raising an event when an object is modified and having the collection listen for the 'Object Modified' event. The id of the object or something similar could be an argument in the event. Once the event is received, the collection knows something has changed and needs to check whether an exception needs to be thrown. Of course, now you've got to consider transactions and how you intend to get the collection back to a consistent state (one without an error condition) if something fails and the exception is thrown.

2. In the collection's add method "Register" a delegate held by the object with the collection. When the object is modified, call the delegate and the collection will get the word. One problem I foresee with this method is if the object is held in multiple collections.

3. Force all object creation to go through the collection rather than creating objects separately. The add method would check error conditions and create the object. You would provide modification methods such as ChangeName(index, newName) so that you have explicit control over every change made to the objects through the collection. Of course this is tricky because you would want your object to be modifiable by the collection but read-only outside of the collection.

I'd be curious to hear if there is an standard solution to this as well.
 
It might be worth having a look with ildasm.exe and see how the DataSet / DataTable system actually works.

From what I remember (not got .Net on this PC yet) a Datatable maintains a reference to it's parent dataset and vice-versa, I suppose on certain property changes the Datatable will query the associated dataset and throw the relevant exceptions.

If you are considering this it might be worth defining a standard interface that all items that can be added to your collection can implement - this way at least you can define your own mechanism for this kind of behaviour.
 
Back
Top