Created
October 29, 2009 21:42
-
-
Save joliver/221878 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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