Class DomainEventDispatcher
Provides a default implementation of of the IDomainEventDispatcher interface.
public class DomainEventDispatcher : FireForgetDispatcher, IDomainEventDispatcher, IDispatcher
- Inheritance
-
DomainEventDispatcher
- Implements
- Inherited Members
- Extension Methods
Examples
This example shows how to route an in-process domain event to a registered handler through the default domain event dispatcher. The setup keeps the aggregate logic isolated while the handler records which account events were observed.
using System.Collections.Generic;
using Savvyio;
using Savvyio.Dispatchers;
using Savvyio.Domain;
using Savvyio.Handlers;
namespace ExampleApp;
public sealed class DomainEventDispatcherExample
{
public IReadOnlyCollection<string> Raise()
{
var handler = new AccountOpenedHandler();
var dispatcher = new DomainEventDispatcher(new ServiceLocator(serviceType => serviceType == typeof(IDomainEventHandler) ? new object[] { handler } : []));
dispatcher.Raise(new AccountOpenedEvent("ACC-42"));
return handler.ProcessedAccounts;
}
}
public sealed class AccountOpenedHandler : IDomainEventHandler
{
public List<string> ProcessedAccounts { get; } = new();
public IFireForgetActivator<IDomainEvent> Delegates => HandlerFactory.CreateFireForget<IDomainEvent>(registry => registry.Register<AccountOpenedEvent>(e => ProcessedAccounts.Add(e.AccountId)));
}
public sealed record AccountOpenedEvent(string AccountId) : Request, IDomainEvent;
Constructors
DomainEventDispatcher(IServiceLocator)
Initializes a new instance of the DomainEventDispatcher class.
public DomainEventDispatcher(IServiceLocator serviceLocator)
Parameters
serviceLocatorIServiceLocatorThe provider of service implementations.
Methods
Raise(IDomainEvent)
Raises the specified request using Fire-and-Forget/In-Only MEP.
public void Raise(IDomainEvent request)
Parameters
requestIDomainEventThe IDomainEvent to raise.
RaiseAsync(IDomainEvent, Action<AsyncOptions>)
Raises the specified request using Fire-and-Forget/In-Only MEP.
public Task RaiseAsync(IDomainEvent request, Action<AsyncOptions> setup = null)
Parameters
requestIDomainEventThe IDomainEvent to raise.
setupAction<AsyncOptions>The AsyncOptions which may be configured.