示例#1
0
        //If you are handling the domain events after committing the original transaction is because you do not want the side effects of those events to be included in the same transaction. e.g sending an email
        private async Task DispatchDomainEventsPostCommitAsync(
            Dictionary <object, List <IDomainEvent> > entityUpdatedEvents,
            Dictionary <object, List <IDomainEvent> > propertyUpdatedEvents,
            Dictionary <object, List <IDomainEvent> > entityDeletedEvents,
            Dictionary <object, List <IDomainEvent> > entityInsertedEvents,
            Dictionary <object, List <IDomainEvent> > entityDomainEvents)
        {
            var domainEvents = new List <IDomainEvent>();

            foreach (var kvp in entityUpdatedEvents)
            {
                foreach (var domainEvent in kvp.Value)
                {
                    try
                    {
                        domainEvents.Add(domainEvent);
                        //await _domainEvents.DispatchPostCommitAsync(domainEvent).ConfigureAwait(false);
                    }
                    catch
                    {
                    }
                }

                //Property Update Events
                if (propertyUpdatedEvents != null && propertyUpdatedEvents.ContainsKey(kvp.Key))
                {
                    foreach (var propertyUpdateEvent in propertyUpdatedEvents[kvp.Key])
                    {
                        try
                        {
                            domainEvents.Add(propertyUpdateEvent);
                            //await _domainEvents.DispatchPostCommitAsync(propertyUpdateEvent).ConfigureAwait(false);
                        }
                        catch
                        {
                        }
                    }
                }
            }

            foreach (var kvp in entityDeletedEvents)
            {
                foreach (var domainEvent in kvp.Value)
                {
                    try
                    {
                        domainEvents.Add(domainEvent);
                        //await _domainEvents.DispatchPostCommitAsync(domainEvent).ConfigureAwait(false);
                    }
                    catch
                    {
                    }
                }
            }

            foreach (var kvp in entityInsertedEvents)
            {
                foreach (var domainEvent in kvp.Value)
                {
                    try
                    {
                        domainEvents.Add(domainEvent);
                        //await _domainEvents.DispatchPostCommitAsync(domainEvent).ConfigureAwait(false);
                    }
                    catch
                    {
                    }
                }
            }

            foreach (var kvp in entityDomainEvents)
            {
                foreach (var domainEvent in kvp.Value)
                {
                    try
                    {
                        domainEvents.Add(domainEvent);
                        //await _domainEvents.DispatchPostCommitAsync(domainEvent).ConfigureAwait(false);
                    }
                    catch
                    {
                    }
                }
            }

            await _domainEvents.DispatchPostCommitBatchAsync(domainEvents).ConfigureAwait(false);
        }