Table of Contents

DomainEventDispatcher Class

Definition

Namespace
Savvyio.Domain
Assemblies
Savvyio.Domain.dll
Source
src/Savvyio.Domain/DomainEventDispatcher.cs

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

Name Description
DomainEventDispatcher(IServiceLocator)

Initializes a new instance of the DomainEventDispatcher class.

Methods

Name Description
Raise(IDomainEvent)

Raises the specified request using Fire-and-Forget/In-Only MEP.

RaiseAsync(IDomainEvent, Action<AsyncOptions>)

Raises the specified request using Fire-and-Forget/In-Only MEP.

See Also