Click here to Skip to main content
Licence CPOL
First Posted 2 Aug 2007
Views 32,385
Downloads 187
Bookmarked 49 times

The EventPool Revisited

By | 28 Mar 2008 | Article
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:

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)
{
  // Do something.
} 

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

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 strings 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

License

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

About the Author

Pete O'Hanlon

CEO

United Kingdom United Kingdom

Member

Follow on Twitter Follow on Twitter
Google+
A developer for more years than I care to remember. 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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUnsubscribe still doesn't work Pinmembersiroman11:27 9 Jul '08  
GeneralUnsubscribe seems broken Pinmemberdirk lewis8:17 28 Mar '08  
GeneralRe: Unsubscribe seems broken PinmvpPete O'Hanlon10:09 28 Mar '08  
GeneralRe: Unsubscribe seems broken Pinmemberdirk lewis2:30 3 Apr '08  
GeneralRe: Unsubscribe seems broken PinmvpPete O'Hanlon8:52 6 Apr '08  
GeneralRe: Unsubscribe seems broken Pinmemberl0t3k7:49 5 May '08  
GeneralRe: Unsubscribe seems broken PinmvpPete O'Hanlon21:57 7 May '08  
General[Message Deleted] Pinmemberdirk lewis1:40 31 Mar '08  
GeneralAbout the enums Pinmemberphilippe dykmans4:46 25 Oct '07  
GeneralRe: About the enums PinmemberPete O'Hanlon2:21 5 Nov '07  
QuestionIs this correct? PinmemberObiwan Jacobi19:29 2 Aug '07  
AnswerRe: Is this correct? PinmemberPete O'Hanlon23:00 2 Aug '07  
GeneralVery nice PinmemberSacha Barber7:59 2 Aug '07  
GeneralRe: Very nice PinmemberPete O'Hanlon9:30 2 Aug '07  
GeneralRe: Very nice PinmemberSacha Barber21:19 2 Aug '07  
GeneralRe: Very nice PinmemberPete O'Hanlon21:47 2 Aug '07  
GeneralRe: Very nice PinmemberSacha Barber2:27 3 Aug '07  
Pete O`Hanlon wrote:
I think you do yourself a great disservice though. You have an approachable, friendly writing style and your articles are always interesting. This is why people have such praise for you

 
Mmmm, I still find this amusing.
 

Pete O`Hanlon wrote:
I think you really do belong up in the pantheon of CP greats

 
Thats pretty cool.
 

Pete O`Hanlon wrote:
Marc, Nish, Christian, Chris and Josh. It says something that you can refer to these people by name and people instantly know who they are.

 
Yeah I knew instantly who you meant. Now thats cool.
 
Im glad you like what ive written, im happy with that. Im going to have to find a job to utilise all this .NET 3.0 stuff soon. Not being to stretched at work at the moment. ASP .NET (yuck, not my bag, if I do web stuff, I would prefer flash, CSS is not cool. ASP .NET I kind of 1/2 like) which is something my skills are lacking. And its my 1st job after graduation so its cool for 6 months of so. Then onwards I hope.
 
I thinkI will soon be using sliverlight and WCF/WPF and CS3 (adobe) on a freinds project, which would be cool. Ill write about that if I do it.
 
Till then. Bye chap. And thanks.
 

 


 
Sacha Barber
A Modern Geek - I cook, I clean, I drink, I Program. Modern or what?
 
My Blog : sachabarber.net

GeneralNice! PinprotectorMarc Clifton3:05 2 Aug '07  
GeneralRe: Nice! PinmemberPete O'Hanlon3:18 2 Aug '07  
GeneralRe: Nice! PinmemberPete O'Hanlon23:01 2 Aug '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 28 Mar 2008
Article Copyright 2007 by Pete O'Hanlon
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid