Extending event raising collections - csf-dev/CSF.Collections.EventRaising GitHub Wiki

There are two abstract types which present the opportunity to extend this library to support additional collection types. These are:

  • CSF.Collections.EventRaising.EventRaisingCollectionBase<TItem>
  • CSF.Collections.EventRaising.EventRaisingCollectionWrapperBase<TCollection,TItem>

Implementing a new collection type

Your new collection type should inherit from the generic EventRaisingCollectionBase<TItem> type, and also implement the appropriate interface for the collection type. You must then provide public implementations of all of the methods on that collection type's interface (minus those already provided by ICollection<T>).

The base class provides the protected property SourceCollection, which provides access to the source collection instance. Methods which only read the state of the collection and make no changes can be redirected straight into that source collection instance.

Methods which alter the state of the collection should call the appropriate event triggering methods:

  • HandleBeforeAdd
  • HandleAfterAdd
  • HandleBeforeRemove
  • HandleAfterRemove

… and then redirect into the source collection. After calling the before event handlers, be sure to check the return value - if it is false then you should abort the modification.

Finally, you must implement two abstract methods: CreateBeforeActionEventArgs and CreateAfterActionEventArgs, and provide a constructor which takes an instance of the appropriate source collection type and passes it to the base method.

Implementing a new collection wrapper type

In order to implement a new collection wrapper type, you must first write the corresponding collection type (above). Once this is done then the wrapper type is very simple to write. Subclass the generic base type EventRaisingCollectionWrapperBase<TCollection,TItem> where TCollection is the interface for the collection type you wish to implement. Then, you need only:

  • Override the method CreateEventHandlingCollection to return an instance of your collection type
  • Provide parameterless constructor
  • Provide a constructor which takes an appropriate source collection instance and passes it to the base type.
⚠️ **GitHub.com Fallback** ⚠️