18 December 2007

The Event Aggregator

by mo

I’d like to talk to you for a moment about my man The Event Aggregator. (Homeboy’s been tossin’ events for years!) Martin Fowler defines the Event Aggregator as:

“Channel events from multiple objects into a single object to simplify registration for clients.” - Martin Fowler

But he’s so much more then that! This pattern comes in handy in state-ful environments, so.. hello WinForms/WPF. Sorry ASP.NET!

What this guy does is creates a single channel to find out what’s going on and to raise events. Through the event aggregator you can subscribe to known events in the system as well as raise known events in the system. Some examples for system wide events are Save, Loading, Shutdown. You can also create concepts for layer specific event aggregators that target certain layers of a system. For example UI only.

public class ApplicationEvents {
  private static readonly IEventAggregator aggregator;
  public static readonly IEvent Save = new EventRaiser( "Save" );
  public static readonly IEvent Loading = new EventRaiser( "Loading" );

  static ApplicationEvents( ) {
    aggregator = new EventAggregator( );
    aggregator.Register( Save );
    aggregator.Register( Loading );

  public static void Raise( IEvent eventToRaise ) {
    aggregator.RaiseEvent( eventToRaise );

  public static void SubscribeTo( IEvent eventToSubscribeTo, EventHandler handler ) {
    aggregator.AddHandler( eventToSubscribeTo.Name, handler );

The ApplicationEvents becomes the central point for raising and subscribing to known application wide events. Type can be coupled to the aggregator but not to each other. Let’s start with the concept of a named event. An event is really nothing more then a delegate. In .NET there are 2 delegate signatures that are used for all events. The generic and non generic version of the EventHandler delegate. (Read More… ) A named event is an event with a name. He might look something like this guy?

internal class EventRaiser : IEvent {
  public EventRaiser( string name ) {
    _name = name;
    _handlers = new List< EventHandler >( );

  public string Name {
    get { return _name; }

  public void Add( EventHandler handler ) {
    _handlers.Add( handler );

  public void Raise( ) {
    Raise( null, null );

  public void Raise( object sender, EventArgs data ) {
    foreach ( EventHandler handler in _handlers ) {
      if ( handler != null ) {
        handler( sender, data );

  private readonly string _name;
  private readonly IList< EventHandler > _handlers;

Underneath the hood it’s the aggregator that’s maintaining references to all the types that have subscribed to events.

    public class EventAggregator : IEventAggregator {
        public EventAggregator( ) {
            _events = new Dictionary< string, IEvent >( );

        public void Register( string eventName ) {
            Register( new EventRaiser( eventName ) );

        public void Register( IEvent eventToAdd ) {
            EnsureEventHasntBeenRegistered( eventToAdd );
            _events.Add( eventToAdd.Name, eventToAdd );

        public void AddHandler( string eventName, EventHandler handler ) {
            RetrieveEvent( eventName ).Add( handler );

        public void RaiseEvent( string withEventName ) {
            RaiseEvent< EventArgs >( withEventName, null, EventArgs.Empty );

        public void RaiseEvent( IEvent eventToRaise ) {
            RaiseEvent< EventArgs >( eventToRaise.Name, null, EventArgs.Empty );

        public void RaiseEvent< T >( string withEventName, object sender, T data ) where T : EventArgs {
            RetrieveEvent( withEventName ).Raise( sender, data );

        private IEvent RetrieveEvent( string eventName ) {
            if ( _events.ContainsKey( eventName ) ) {
                return _events[ eventName ];
            throw new ArgumentNullException( eventName, "The Event Has Not Been Registered." );

        private void EnsureEventHasntBeenRegistered( IEvent eventToAdd ) {
            if ( _events.ContainsKey( eventToAdd.Name ) ) {
                throw new ArgumentException( "Event name has already been registered", eventToAdd.Name );

        private readonly IDictionary< string, IEvent > _events;

The concept is similar to Juval Lowy’s EventHelper class defined in…

Programming .NET Components, 2nd Edition by Juval Lowy Read more about this title…

books csharp designpatterns