public void Test() { bool unknownMessage = false; var factory = new SimpleEventHandlerFactory(); var defaultHandler = factory.CreateEventHandler( (IMessage cmd, long sequence, bool endOfBatch) => { unknownMessage = true; }); var collection = new SimpleMessageHandlerCollection(defaultHandler); bool eventHandled = false; int result = 0; collection.AddHandler(typeof(MyTestCommand), factory.CreateEventHandler( (IMyTestCommand cmd, long sequence, bool endOfBatch) => { eventHandled = true; result += cmd.data; })); var msg = new MyTestCommand { data = 100 }; // It has to search for the handler the first time dynamic handler = collection.GetHandler(msg); handler.OnNext((dynamic)msg, 0, false); Assert.That(eventHandled, Is.True); Assert.That(result, Is.EqualTo(100)); var unknown = new UnknownCommand(); handler = collection.GetHandler(unknown); handler.OnNext(unknown, 2, false); Assert.That(unknownMessage, Is.True); }
public void TestBasicHandlerFactory() { bool handled = false; var factory = new SimpleEventHandlerFactory(); var handler = factory.CreateEventHandler( (MyTestCommand cmd, long seq, bool endOfBatch) => { handled = true; }); handler.OnNext(new MyTestCommand(), 0, false); Assert.That(handled, Is.True); }
private static void RegisterCommandHandlers(IConcertService concertService, CommandHandlerCollection commandHandlers) { var factory = new SimpleEventHandlerFactory(); commandHandlers.AddHandler(typeof (PurchaseTicketCommand), factory.CreateEventHandler(concertService, (IConcertService service, IPurchaseTicketCommand cmd, long seq, bool endOfBatch) => { concertService.PurchaseTicket(/*IPurchaseTicketCommand data*/); })); commandHandlers.AddHandler(typeof(CancelTicketCommand), factory.CreateEventHandler(concertService, (IConcertService service, ICancelTicketCommand cmd, long seq, bool endOfBatch) => { concertService.CancelTicket(/*ICancelTicketCommand data*/); })); commandHandlers.AddHandler(typeof(CreateConcertCommand), factory.CreateEventHandler(concertService, (IConcertService service, ICreateConcertCommand cmd, long seq, bool endOfBatch) => { concertService.CreateConcert(/*ICreateConcertCommand data*/); })); }
public void TestServiceHandlerFactory() { bool handled = false; DateTimeOffset eventTime = DateTimeOffset.UtcNow; var clockMock = new Mock<IUtcClockService>(); clockMock .SetupGet(o => o.UtcNow) .Returns(new DateTimeOffset(new DateTime(2016, 04, 01))); var factory = new SimpleEventHandlerFactory(); var handler = factory.CreateEventHandler(clockMock.Object, (IUtcClockService businessLogic, MyTestCommand cmd, long seq, bool endOfBatch) => { eventTime = businessLogic.UtcNow; handled = true; }); handler.OnNext(new MyTestCommand(), 0, false); Assert.That(handled, Is.True); Assert.That(eventTime, Is.EqualTo(new DateTimeOffset(new DateTime(2016, 04, 01)))); }