Exemplo n.º 1
0
        public async Task When_3_Handlers_AreSubscribed_And_Each_PublishedOnce_Then_Publish_ShouldFire_Exactly_3_Times()
        {
            _eventBus.Subscribe(new FirstTestEventHandler());
            _eventBus.Subscribe(new SecondTestEventHandler());
            _eventBus.Subscribe(new ThirdTestEventHandler());

            int firedCount = 0;

            _eventBus.OnEventPublished += (sender, args) => ++ firedCount;

            await _eventBus.Publish(new FirstTestEvent(Guid.NewGuid(), DateTimeOffset.UtcNow));

            await _eventBus.Publish(new SecondTestEvent(Guid.NewGuid(), DateTimeOffset.UtcNow));

            await _eventBus.Publish(new ThirdTestEvent(Guid.NewGuid(), DateTimeOffset.UtcNow));

            Assert.AreEqual(3, firedCount);
        }
        private async Task ExecuteSubscribersAsync(DomainEventPrimitive domainEventPrimitive, CancellationToken cancellationToken)
        {
            var domainEventType = DomainEventsInformation.ForName(domainEventPrimitive.Name);

            var instance = (DomainEvent)Activator.CreateInstance(domainEventType);

            var result = (DomainEvent)domainEventType
                         .GetTypeInfo()
                         .GetDeclaredMethod(nameof(DomainEvent.FromPrimitives))
                         ?.Invoke(instance, new object[]
            {
                domainEventPrimitive.AggregateId,
                domainEventPrimitive.Body,
                domainEventPrimitive.Id,
                domainEventPrimitive.OccurredOn
            });

            await _bus.Publish(new List <DomainEvent> {
                result
            }, cancellationToken);

            _context.Set <DomainEventPrimitive>().Remove(domainEventPrimitive);
        }