Пример #1
0
        /// <inheritdoc />
        public void DispatchEvents()
        {
            var events = DomainEventRepository.FindAll();

            foreach (var domainEventRecord in events)
            {
                var subscriptions = _subscriptionManager.GetHandlersForEvent(domainEventRecord.Type.Name);

                foreach (var subscription in subscriptions)
                {
                    var handler = Singleton.Container.GetInstance(subscription.HandlerType);
                    if (handler == null)
                    {
                        continue;
                    }
                    var eventType    = _subscriptionManager.GetEventTypeByName(domainEventRecord.Type.Name);
                    var concreteType = typeof(IHandler <>).MakeGenericType(eventType);
                    concreteType.GetMethod("Handle").Invoke(handler, new[] { domainEventRecord.Event });
                }

                if (!string.IsNullOrEmpty(domainEventRecord.Message))
                {
                    Console.WriteLine(domainEventRecord.Message);
                }
            }
            DomainEventRepository.ClearEvents();
        }
        public void AddCreateCampaignEvent()
        {
            //Arrange
            var mock        = new Mock <IRepository <Campaign> >();
            var saleMock    = new Mock <IRepository <Sale> >();
            var uowMock     = new Mock <IUnitOfWork>();
            var consoleMock = new Mock <IConsoleWriter>();

            mock.Setup(x => x.Add(It.IsAny <Campaign>()))
            .Returns(new Campaign {
                ProductCode = "P1", Name = "C1", Status = Status.Active, Duration = 5
            }).Verifiable();
            var campaignService = new CampaignServices(mock.Object, saleMock.Object, uowMock.Object, consoleMock.Object);

            //Act
            campaignService.CreateCampaign("C1", "P1", 10, 20, 100);
            var events = DomainEventRepository.FindAll();

            //Assert
            Assert.Contains(events, x => x.Type == typeof(AddNewCampaignEvent));
        }
Пример #3
0
        public void AddNewProductOrderCreatedEvent(Product product, Order order, Campaign campaign)
        {
            //Arrange
            var campaignMock = new Mock <IRepository <Campaign> >();
            var productMock  = new Mock <IRepository <Product> >();
            var orderMock    = new Mock <IRepository <Order> >();
            var uowMock      = new Mock <IUnitOfWork>();

            productMock.Setup(x => x.Get(It.IsAny <Expression <Func <Product, bool> > >()))
            .Returns(product);
            orderMock.Setup(x => x.Get(It.IsAny <Expression <Func <Order, bool> > >()))
            .Returns(order);

            var orderService = new OrderServices(orderMock.Object, productMock.Object, campaignMock.Object, uowMock.Object);

            //Act
            orderService.CreateOrder(product.ProductCode, order.Quantity);
            var events = DomainEventRepository.FindAll();

            //Assert
            Assert.Contains(events, x => x.Type == typeof(NewProductOrderCreatedEvent));
        }
        public void AddCreateCampaignEventShouldNotEmptyMessage()
        {
            //Arrange
            var mock        = new Mock <IRepository <Campaign> >();
            var saleMock    = new Mock <IRepository <Sale> >();
            var uowMock     = new Mock <IUnitOfWork>();
            var consoleMock = new Mock <IConsoleWriter>();

            mock.Setup(x => x.Add(It.IsAny <Campaign>()))
            .Returns(new Campaign {
                ProductCode = "P1", Name = "C1", Status = Status.Active, Duration = 5
            }).Verifiable();
            var campaignService = new CampaignServices(mock.Object, saleMock.Object, uowMock.Object, consoleMock.Object);

            //Act
            campaignService.CreateCampaign("C1", "P1", 10, 20, 100);
            var events      = DomainEventRepository.FindAll();
            var domainEvent = events.FirstOrDefault(x => x.Type == typeof(AddNewCampaignEvent)).Event as AddNewCampaignEvent;

            //Assert
            Assert.False(string.IsNullOrEmpty(domainEvent.Message));
        }