Skip to content

Instantly share code, notes, and snippets.

@joliver
Created October 29, 2009 21:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joliver/221878 to your computer and use it in GitHub Desktop.
Save joliver/221878 to your computer and use it in GitHub Desktop.
public class EventDispatcher : IDispatchEvents
{
private readonly IDictionary<Type, Action<IDomainEvent>> handlers = new Dictionary<Type, Action<IDomainEvent>>();
public virtual void Register<TEvent>(Action<TEvent> handler)
where TEvent : class, IDomainEvent
{
// re-wrap delegate
this.handlers[typeof(TEvent)] = @event => handler(@event as TEvent);
}
public virtual void Dispatch(IDomainEvent message)
{
Action<IDomainEvent> handler;
if (!this.handlers.TryGetValue(message.GetType(), out handler))
throw new EventNotRegisteredException(message.GetType());
handler(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment