Introduction
I recently started using Marc Clifton's excellent EventPool component to simplify the use of creating and using events. As Marc indicates in his writeup on the component, there are some drawbacks relating to the use of EventArgs
based on the limitations of .NET 1, .NET 1.1.
Background
With the advent of .NET 2 and the introduction of generics, it seemed the ideal opportunity to revisit the EventPool
and see what could be done to enhance it. Some of the new functionality is based on differences between .NET 1 and 2. Other enhancements have been included that have nothing to do with technology upgrades, but rather are down to features that I felt were missing in the original version.
Here are a list of features that have been added:
- The ability to use event arguments derived from
EventArgs
.
- The ability to use enumerations when managing events.
- The option to unsubscribe from events.
- Retrieve a list of supported events.
- Retrieve a list of events that have no subscriptions. This is useful for removing stale entries.
- Publish events using attributes.
Here we go through the different ways of using the EventPool
.
Derived Event Arguments
Suppose that you want to subscribe to an event handler which uses an event argument class called AddEventArgs
. All you need to do is this:
eventPoolHandler.EventPool.Subscribe
("Add", new EventHandler<AddEventArgs>(AddEventHandler));
Then, code up your event handler as you would normally:
private void AddEventHandler(object sender, AddEventArgs aea)
{
}
To actually raise the event, you call:
eventPool.Fire("Add", this, new AddEventArgs());
You now have typesafe event arguments.
Enumerated Events
One of the problems with using string
s to manage event names is that there is too much of a risk of names being typed in incorrectly. To get around this, you can add an enumeration that says what events you want to publish.
There are two ways to hook your enumerations up as events. One method allows you to pick a specific enumeration and publish that one only, the other allows you to publish all elements in the enumeration. When you subscribe to an enumeration or fire it, you must pick the specific enumeration.
Sample
public enum FileOperations
{
Opening,
Closing,
Saving
}
public class EventPublisher : IEventPool
{
private EventPool eventPool;
public EventPublisher()
{
this.eventPool = new EventPool();
this.eventPool.Publish(typeof(FileOperations));
}
public EventPool EventPool
{
get { return this.eventPool ; }
}
}
Unsubscribe From An Event
Sometimes you want to unsubscribe from event notifications. In this case, you simply call the Unsubscribe
method.
eventPoolHandler.EventPool.Unsubscribe
(FileOperations.Opening, new EventHandler<AddEventArgs>(AddEventHandler));
Supported Events and Unsubscribed Events
The Events
property returns a list of events that have been published on a particular event pool.
The UnsubscribedEvents
property returns a list of events that have no active subscriptions.
Publishing Events using Attributes
In the assembly, there is an EventPoolAttribute
class that you can use to mark your event publishing class with the list of events that you want to support. You can either supply string
s or enumeration.
public enum FileOperations
{
Opening,
Closing,
Saving,
}
[EventPool(typeof(FileOperations))]
public class EventPublisher : IEventPool
{
private EventPool eventPool = new EventPool();
public EventPublisher() {}
public EventPool EventPool
{
get { return eventPool ; }
}
}
Finally
I hope that this gives you some taste of what you can do with the EventPool
class and that it will prove useful to you.
History
- 2nd August, 2007: Article uploaded
- 3rd August, 2007: Article and code updated to include the ability to publish an enumeration in its entirety and to publish events in the
EventPoolAttribute
class
- 28th March, 2008: Article updated to correct an issue with the
Unsubscribe
method