Clean Architecture Demo
Toggle main menu visibility
Loading...
Searching...
No Matches
InProcessDomainEventDispatcher.cs
Go to the documentation of this file.
1
// TaskManagement.Infrastructure/EventDispatching/InProcessDomainEventDispatcher.cs
2
using
Microsoft.Extensions.DependencyInjection;
3
using
TaskManagement.Application.Events
;
4
using
TaskManagement.Application.Ports
;
5
using
TaskManagement.Domain.Events
;
6
7
namespace
TaskManagement.Infrastructure.EventDispatching
;
8
26
public
sealed
class
InProcessDomainEventDispatcher
:
IDomainEventDispatcher
27
{
28
private
readonly IServiceProvider _serviceProvider;
29
30
public
InProcessDomainEventDispatcher
(IServiceProvider serviceProvider)
31
{
32
_serviceProvider = serviceProvider;
33
}
34
35
public
async Task
DispatchAsync
(
36
IReadOnlyCollection<DomainEvent> events,
37
CancellationToken cancellationToken =
default
)
38
{
39
ArgumentNullException.ThrowIfNull(events);
40
41
foreach
(var domainEvent
in
events)
42
{
43
// El tipo concreto del evento se conoce recién en tiempo de ejecución.
44
var handlerType = typeof(IDomainEventSubscriber<>).MakeGenericType(domainEvent.GetType());
45
var handleMethod = handlerType.GetMethod(nameof(IDomainEventSubscriber<DomainEvent>.HandleAsync))!;
46
47
foreach
(var handler
in
_serviceProvider.GetServices(handlerType))
48
{
49
await ((Task)handleMethod.Invoke(handler, [domainEvent, cancellationToken])!).ConfigureAwait(
false
);
50
}
51
}
52
}
53
}
TaskManagement.Infrastructure.EventDispatching.InProcessDomainEventDispatcher.DispatchAsync
async Task DispatchAsync(IReadOnlyCollection< DomainEvent > events, CancellationToken cancellationToken=default)
Definition
InProcessDomainEventDispatcher.cs:35
TaskManagement.Infrastructure.EventDispatching.InProcessDomainEventDispatcher.InProcessDomainEventDispatcher
InProcessDomainEventDispatcher(IServiceProvider serviceProvider)
Definition
InProcessDomainEventDispatcher.cs:30
TaskManagement.Application.Ports.IDomainEventDispatcher
IDomainEventDispatcher es la interfaz para publicación de eventos del dominio.
Definition
IDomainEventDispatcher.cs:46
TaskManagement.Application.Events
Definition
IDomainEventSubscriber.cs:3
TaskManagement.Application.Ports
Definition
IDomainEventDispatcher.cs:3
TaskManagement.Domain.Events
Definition
DomainEvent.cs:4
TaskManagement.Infrastructure.EventDispatching
Definition
InProcessDomainEventDispatcher.cs:7
src
TaskManagement.Infrastructure
EventDispatching
InProcessDomainEventDispatcher.cs
Generated by
1.18.0