Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / C#
Article

The EventPool Revisited

Rate me:
Please Sign up or sign in to vote.
4.81/5 (15 votes)
28 Mar 2008CPOL3 min read 61.2K   304   52   20
Easily manage .NET events using attributes, enumerations and generics

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:

  1. The ability to use event arguments derived from EventArgs.
  2. The ability to use enumerations when managing events.
  3. The option to unsubscribe from events.
  4. Retrieve a list of supported events.
  5. Retrieve a list of events that have no subscriptions. This is useful for removing stale entries.
  6. 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:

C#
eventPoolHandler.EventPool.Subscribe
    ("Add", new EventHandler<AddEventArgs>(AddEventHandler));

Then, code up your event handler as you would normally:

C#
private void AddEventHandler(object sender, AddEventArgs aea)
{
  // Do something.
} 

To actually raise the event, you call:

C#
eventPool.Fire("Add", this, new AddEventArgs());

You now have typesafe event arguments.

Enumerated Events

One of the problems with using strings 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

C#
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.

C#
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 strings or enumeration.

C#
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
United Kingdom United Kingdom
A developer for over 30 years, I've been lucky enough to write articles and applications for Code Project as well as the Intel Ultimate Coder - Going Perceptual challenge. I live in the North East of England with 2 wonderful daughters and a wonderful wife.

I am not the Stig, but I do wish I had Lotus Tuned Suspension.

Comments and Discussions

 
GeneralUnsubscribe still doesn't work Pin
siroman9-Jul-08 11:27
siroman9-Jul-08 11:27 
GeneralUnsubscribe seems broken Pin
dirk lewis28-Mar-08 8:17
dirk lewis28-Mar-08 8:17 
GeneralRe: Unsubscribe seems broken Pin
Pete O'Hanlon28-Mar-08 10:09
subeditorPete O'Hanlon28-Mar-08 10:09 
GeneralRe: Unsubscribe seems broken Pin
dirk lewis3-Apr-08 2:30
dirk lewis3-Apr-08 2:30 
GeneralRe: Unsubscribe seems broken Pin
Pete O'Hanlon6-Apr-08 8:52
subeditorPete O'Hanlon6-Apr-08 8:52 
GeneralRe: Unsubscribe seems broken Pin
l0t3k5-May-08 7:49
l0t3k5-May-08 7:49 
GeneralRe: Unsubscribe seems broken Pin
Pete O'Hanlon7-May-08 21:57
subeditorPete O'Hanlon7-May-08 21:57 
General[Message Deleted] Pin
dirk lewis31-Mar-08 1:40
dirk lewis31-Mar-08 1:40 
GeneralAbout the enums Pin
philippe dykmans25-Oct-07 4:46
philippe dykmans25-Oct-07 4:46 
My first thought when reading Marc's original article was: "why not use enum instead of string?". I thought it would be so much easier. So i recoded Marc's EventPool with enum and threw out the string... Turned out i was wrong.

Using enum is indeed easier for small projects. But for growing projects it becomes a burden. Because the enum introduces a contract between all parties that use the eventpool. That is, the enum has to be visible to ALL of them. And so, there goes your 'loose coupling'.

Not to say that the enum are a bad idea. Just to say: "whatever you do, DON'T THROW OUT THE STRINGS!"

Philippe Dykmans
Software developpement
University of Antwerp

GeneralRe: About the enums Pin
Pete O'Hanlon5-Nov-07 2:21
subeditorPete O'Hanlon5-Nov-07 2:21 
QuestionIs this correct? Pin
Obiwan Jacobi2-Aug-07 19:29
Obiwan Jacobi2-Aug-07 19:29 
AnswerRe: Is this correct? Pin
Pete O'Hanlon2-Aug-07 23:00
subeditorPete O'Hanlon2-Aug-07 23:00 
GeneralVery nice Pin
Sacha Barber2-Aug-07 7:59
Sacha Barber2-Aug-07 7:59 
GeneralRe: Very nice Pin
Pete O'Hanlon2-Aug-07 9:30
subeditorPete O'Hanlon2-Aug-07 9:30 
GeneralRe: Very nice Pin
Sacha Barber2-Aug-07 21:19
Sacha Barber2-Aug-07 21:19 
GeneralRe: Very nice Pin
Pete O'Hanlon2-Aug-07 21:47
subeditorPete O'Hanlon2-Aug-07 21:47 
GeneralRe: Very nice Pin
Sacha Barber3-Aug-07 2:27
Sacha Barber3-Aug-07 2:27 
GeneralNice! Pin
Marc Clifton2-Aug-07 3:05
mvaMarc Clifton2-Aug-07 3:05 
GeneralRe: Nice! Pin
Pete O'Hanlon2-Aug-07 3:18
subeditorPete O'Hanlon2-Aug-07 3:18 
GeneralRe: Nice! Pin
Pete O'Hanlon2-Aug-07 23:01
subeditorPete O'Hanlon2-Aug-07 23:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.