Created
March 21, 2016 17:48
-
-
Save tarasn/c4a33e6d86fd953346e5 to your computer and use it in GitHub Desktop.
Simple event broker that can be easily to be used in an IOC (using auto registration and handling more than one message by the same handler)
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
| using System; | |
| using System.Collections.Generic; | |
| using Castle.MicroKernel.Registration; | |
| using Castle.Windsor; | |
| namespace ConsoleApplication6 | |
| { | |
| public class Message | |
| { | |
| } | |
| public class Message1 : Message | |
| { | |
| } | |
| public class Message2 : Message | |
| { | |
| } | |
| public interface IHandler | |
| { | |
| } | |
| public class Handler1 : IHandler | |
| { | |
| private IEventBroker _eb; | |
| public Handler1(IEventBroker eb) | |
| { | |
| _eb = eb; | |
| _eb.Register<Message1>(Handle); | |
| } | |
| public void Handle(Message1 message) | |
| { | |
| } | |
| } | |
| public class Handler2 : IHandler | |
| { | |
| private IEventBroker _eb; | |
| public Handler2(IEventBroker eb) | |
| { | |
| _eb = eb; | |
| _eb.Register<Message2>(Handle); | |
| _eb.Register<Message1>(Handle); | |
| } | |
| public void Handle(Message2 message) | |
| { | |
| } | |
| public void Handle(Message1 message) | |
| { | |
| } | |
| } | |
| public class Handler22 : IHandler | |
| { | |
| private IEventBroker _eb; | |
| public Handler22(IEventBroker eb) | |
| { | |
| _eb = eb; | |
| _eb.Register<Message1>(Handle); | |
| } | |
| public void Handle(Message1 message) | |
| { | |
| } | |
| } | |
| public interface IEventBroker | |
| { | |
| void Register<T>(Action<T> handle) where T : Message; | |
| void Publish<T>(T message) where T : Message; | |
| } | |
| public class EventBroker : IEventBroker | |
| { | |
| readonly IDictionary<Type,List<dynamic>> _dic= new Dictionary<Type, List<dynamic>>(); | |
| public void Register<T>(Action<T> handle) where T : Message | |
| { | |
| if (!_dic.ContainsKey(typeof (T))) | |
| { | |
| _dic[typeof (T)] = new List<dynamic>(); | |
| } | |
| _dic[typeof (T)].Add(handle); | |
| } | |
| public void Publish<T>(T message) where T : Message | |
| { | |
| foreach (var handle in _dic[typeof(T)]) | |
| { | |
| handle(message); | |
| } | |
| } | |
| } | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var cnt = new WindsorContainer(); | |
| cnt.Register(Classes.FromThisAssembly() | |
| .BasedOn<IHandler>() | |
| .WithService.Base() | |
| ); | |
| cnt.Register(Component.For<IEventBroker, EventBroker>()); | |
| var handlers = cnt.ResolveAll<IHandler>(); | |
| //var h = cnt.Resolve<IHandler>(); | |
| //cnt.Resolve<IEventBroker>().Publish(new Message2()); | |
| cnt.Resolve<IEventBroker>().Publish(new Message1()); | |
| Console.ReadKey(true); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment