Exemplo n.º 1
0
        public async Task DispatchEventsAsync()
        {
            var domainEvents = _domainEventsProvider.GetAllDomainEvents();


            var domainEventNotifications = new List <IDomainEventNotification <IDomainEvent> >();

            foreach (var domainEvent in domainEvents)
            {
                Type domainEvenNotificationType        = typeof(IDomainEventNotification <>);
                var  domainNotificationWithGenericType = domainEvenNotificationType.MakeGenericType(domainEvent.GetType());
                var  domainNotification = _scope.ResolveOptional(domainNotificationWithGenericType, new List <Parameter>
                {
                    new NamedParameter("domainEvent", domainEvent),
                    new NamedParameter("id", Guid.NewGuid())
                });

                if (domainNotification != null)
                {
                    domainEventNotifications.Add(domainNotification as IDomainEventNotification <IDomainEvent>);
                }
            }

            _domainEventsProvider.ClearAllDomainEvents();

            IEnumerable <Task> tasks = domainEvents
                                       .Select(async domainEvent =>
            {
                await _mediator.Publish(domainEvent);
            });

            tasks = tasks.Concat(domainEventNotifications.Select(async notification =>
            {
                await _bus.Publish(notification);
            }));

            await Task.WhenAll(tasks);

            /*
             * foreach (var domainEventNotification in domainEventNotifications)
             * {
             *  string type = domainEventNotification.GetType().FullName;
             *  var data = JsonConvert.SerializeObject(domainEventNotification, new JsonSerializerSettings
             *  {
             *      ContractResolver = new AllPropertiesContractResolver()
             *  });
             *  OutboxMessage outboxMessage = new OutboxMessage(
             *      domainEventNotification.Id,
             *      domainEventNotification.DomainEvent.OccurredOn,
             *      type,
             *      data);
             *  _outbox.Add(outboxMessage);
             * }
             */
        }
        public async Task DispatchEventsAsync()
        {
            var domainEvents = _domainEventsProvider.GetAllDomainEvents();


            var domainEventNotifications = new List <IDomainEventNotification <IDomainEvent> >();

            foreach (var domainEvent in domainEvents)
            {
                Type domainEvenNotificationType        = typeof(IDomainEventNotification <>);
                var  domainNotificationWithGenericType = domainEvenNotificationType.MakeGenericType(domainEvent.GetType());
                var  domainNotification = _scope.ResolveOptional(domainNotificationWithGenericType, new List <Parameter>
                {
                    new NamedParameter("domainEvent", domainEvent)
                });

                if (domainNotification != null)
                {
                    domainEventNotifications.Add(domainNotification as IDomainEventNotification <IDomainEvent>);
                }
            }

            _domainEventsProvider.ClearAllDomainEvents();

            var tasks = domainEvents
                        .Select(async(domainEvent) =>
            {
                await _mediator.Publish(domainEvent);
            });

            await Task.WhenAll(tasks);

            foreach (var domainEventNotification in domainEventNotifications)
            {
                string type = domainEventNotification.GetType().FullName;
                var    data = JsonConvert.SerializeObject(domainEventNotification, new JsonSerializerSettings
                {
                    ContractResolver = new AllPropertiesContractResolver()
                });
                OutboxMessage outboxMessage = new OutboxMessage(
                    domainEventNotification.Id,
                    domainEventNotification.DomainEvent.OccurredOn,
                    type,
                    data);
                _outbox.Add(outboxMessage);
            }
        }