Created
July 18, 2013 11:57
-
-
Save rcknight/6028733 to your computer and use it in GitHub Desktop.
A very basic event dispatcher
This file contains 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 | |
{ | |
private EventHandlerRegistry _registry; | |
public EventDispatcher(EventHandlerRegistry reg) | |
{ | |
_registry = reg; | |
} | |
public void Dispatch(object e) | |
{ | |
var handlers = _registry.HandlersForMessageType(e.GetType()); | |
foreach(var handler in handlers) | |
((dynamic)handler).Handle((dynamic) e); | |
} | |
} |
This file contains 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 EventHandlerRegistry | |
{ | |
private readonly Dictionary<Type, List<object>> _eventHandlers; | |
public EventHandlerRegistry() | |
{ | |
_eventHandlers = new Dictionary<Type, List<object>>(); | |
} | |
public void RegisterByConvention(object handler) | |
{ | |
var handlerType = handler.GetType(); | |
var handlerInterfaces = handlerType.GetInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IConsume<>)).ToArray(); | |
foreach (var messageType in handlerInterfaces.Select(i => i.GetGenericArguments()[0])) | |
{ | |
AddHandlerToRegistry(messageType, handler); | |
} | |
} | |
public void AddHandlerToRegistry(Type messageType, object handler) | |
{ | |
List<Object> handlerList; | |
var hasHandlerList = _eventHandlers.TryGetValue(messageType, out handlerList); | |
if (!hasHandlerList) | |
{ | |
handlerList = new List<Object>(); | |
_eventHandlers.Add(messageType, handlerList); | |
} | |
handlerList.Add(handler); | |
} | |
public IEnumerable<object> HandlersForMessageType(Type messageType) | |
{ | |
List<object> handlerList; | |
var hasHandlerList = _eventHandlers.TryGetValue(messageType, out handlerList); | |
return hasHandlerList ? handlerList : new List<object>(); | |
} | |
} |
This file contains 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 interface IConsume<in TMessage> | |
{ | |
void Handle(TMessage message); | |
} |
This file contains 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
void Main() | |
{ | |
var reg = new EventHandlerRegistry(); | |
var dispatcher = new EventDispatcher(reg); | |
var handler = new SomeEventHandler(); | |
//reg.AddHandlerToRegistry(typeof(SomeEvent), handler); | |
reg.RegisterByConvention(handler); | |
var e = new SomeEvent() { EventText = "Woftam" }; | |
dispatcher.Dispatch(e); | |
Console.ReadLine(); | |
} | |
public class SomeEvent | |
{ | |
public string EventText {get;set;} | |
} | |
public class SomeEventHandler : IConsume<SomeEvent> | |
{ | |
public void Handle(SomeEvent e) | |
{ | |
Console.WriteLine (e.EventText); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment