コード例 #1
0
        public void Subscribe_NullObject_ShouldThrowException()
        {
            // Arrange
            IMessageBus <DummyMessage> messageBus = new MemoryMessageBus <DummyMessage>();

            // Act, Assert
            Assert.Throws <ArgumentNullException>(() => messageBus.Subscribe <DummyMessage>(null));
        }
コード例 #2
0
        public void Publish_WithHandlerAndNullObject_ShouldThrowException()
        {
            // Arrange
            IMessageBus <DummyMessage> messageBus         = new MemoryMessageBus <DummyMessage>();
            IHandler <DummyMessage>    fakeMessageHandler = Mock.Of <IHandler <DummyMessage> >();

            // Act
            messageBus.Subscribe(fakeMessageHandler);

            // Assert
            Assert.Throws <ArgumentNullException>(() => messageBus.Publish <DummyMessage>(null));
        }
コード例 #3
0
        public void Subscribe_WhenUsingDisposable_ShouldUnsubscribe()
        {
            // Arrange
            IMessageBus <DummyMessage>      messageBus         = new MemoryMessageBus <DummyMessage>();
            Mock <IHandler <DummyMessage> > handlerMock        = new Mock <IHandler <DummyMessage> >();
            IHandler <DummyMessage>         fakeMessageHandler = handlerMock.Object;

            // Act
            using (IDisposable unsubscribable = messageBus.Subscribe(fakeMessageHandler));
            messageBus.Publish(new DummyMessage());

            // Assert
            handlerMock.Verify(m => m.Handle(It.IsNotNull <DummyMessage>()), Times.Never);
        }
コード例 #4
0
        public void Subscribe_OneHandler_ShouldContainOneHandler()
        {
            // Arrange
            IMessageBus <DummyMessage>      messageBus         = new MemoryMessageBus <DummyMessage>();
            Mock <IHandler <DummyMessage> > handlerMock        = new Mock <IHandler <DummyMessage> >();
            IHandler <DummyMessage>         fakeMessageHandler = handlerMock.Object;

            // Act
            messageBus.Subscribe(fakeMessageHandler);
            messageBus.Publish(new DummyMessage());

            // Assert
            handlerMock.Verify(m => m.Handle(It.IsNotNull <DummyMessage>()), Times.Once);
        }
コード例 #5
0
        public void Publish_FakeMessage_ShouldInvokeOneHandler()
        {
            // Arrange
            IMessageBus <DummyMessage>      messageBus = new MemoryMessageBus <DummyMessage>();
            Mock <IHandler <DummyMessage> > mock       = new Mock <IHandler <DummyMessage> >();

            mock.Setup(x => x.Handle(It.IsNotNull <DummyMessage>()));
            IHandler <DummyMessage> fakeMessageHandler = mock.Object;
            DummyMessage            dummyMessage       = new DummyMessage();

            // Act
            messageBus.Subscribe(fakeMessageHandler);
            messageBus.Publish(dummyMessage);

            // Assert
            mock.Verify(m => m.Handle(It.IsNotNull <DummyMessage>()), Times.Once);
        }
コード例 #6
0
        private static void Main()
        {
            // units of work
            IComparer <Event <uint> >         comparer   = CreateEventComparer();
            IEventStore <uint, Event <uint> > eventStore = new MemoryEventStore <uint, Event <uint> >(comparer);
            IUnitOfWork aggregatesUnitOfWork             = new AggregateUnitOfWorkFactory().Create(eventStore);
            IUnitOfWork domainUnitOfWork = new DomainUnitOfWorkFactory().Create();

            // messaging
            IMessageBus <object> eventBus = new MemoryMessageBus <object>();

            OrderCommandsHandler orderCommandsHandler = new OrderCommandsHandler(aggregatesUnitOfWork);

            eventBus.Subscribe <CreateOrderCommand>(orderCommandsHandler);
            eventBus.Subscribe <ChangeOrderDateCommand>(orderCommandsHandler);
            eventBus.Subscribe <DeleteOrderCommand>(orderCommandsHandler);

            OrderEventsHandler orderEventsHandler = new OrderEventsHandler(domainUnitOfWork, eventBus);

            eventBus.Subscribe <OrderCreatedEvent>(orderEventsHandler);
            eventBus.Subscribe <OrderDateChangedEvent>(orderEventsHandler);
            eventBus.Subscribe <OrderDeletedEvent>(orderEventsHandler);

            // commands simulation
            uint iterations = 3;

            for (uint i = 0; i < iterations; i++)
            {
                eventBus.Publish(new CreateOrderCommand(i));
                eventBus.Publish(new ChangeOrderDateCommand(i, DateTime.Now.AddDays(1)));
                eventBus.Publish(new DeleteOrderCommand(i));
            }

            for (uint i = 0; i < iterations; i++)
            {
                OrderDto found = domainUnitOfWork.GetRepository <OrderDto>().GetById(i);
                Console.WriteLine($"{nameof(OrderDto.Id)}: {found.Id}");
                Console.WriteLine($"{nameof(OrderDto.Date)}: {found.Date}");
            }

            Console.ReadKey();
        }