kgrzybek / modular-monolith-with-ddd

Full Modular Monolith application with Domain-Driven Design approach.
MIT License
10.89k stars 1.71k forks source link

Issue with 2 or more DomainNotification<> with thesame DomainEvent #243

Open arvylagunamayor opened 2 years ago

arvylagunamayor commented 2 years ago

Im trying to handle one DomainEvent with multiple DomainNotificationHandler that doing different task. One for emitting event for subscribe module, and one for sending automated email. Based on the current implementation of "DomainEventsDispatcher.cs" file (code below). The autofac only resolving one of my DomainNotification. I can do workaround like triggering ICommandScheduler, but im not sure its the best approach.

` 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", domainEvent.Id)
            });

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