Table of Contents

Class DomainEventDispatchServiceCollectionExtensions

Namespace
Trellis.Mediator
Assembly
Trellis.Mediator.dll

Extension methods for registering Trellis.Mediator domain-event dispatch.

public static class DomainEventDispatchServiceCollectionExtensions
Inheritance
DomainEventDispatchServiceCollectionExtensions
Inherited Members

Methods

AddDomainEventDispatch(IServiceCollection)

Registers DomainEventDispatchBehavior<TMessage, TResponse> as an open-generic pipeline behavior together with the default IDomainEventPublisher implementation.

public static IServiceCollection AddDomainEventDispatch(this IServiceCollection services)

Parameters

services IServiceCollection

The service collection.

Returns

IServiceCollection

The service collection for chaining.

Remarks

This is the AOT/trim-friendly registration entry point; pair it with AddDomainEventHandler<TEvent, THandler>(IServiceCollection) calls for each concrete handler. For assembly scanning, use AddDomainEventDispatch(IServiceCollection, params Assembly[]).

The behavior is inserted as the innermost of the always-on Trellis behaviors, running after ValidationBehavior (Exception → Tracing → Logging → AuthorizationContext → static/direct/via authorization → Validation → DomainEventDispatch). If TransactionalCommandBehavior (from Trellis.Mediator) is already registered, this method temporarily yanks it, ensures the always-on Trellis behaviors are present, appends dispatch, and re-appends the transactional behavior as innermost. The result is order-independent: events fire after the transaction commits regardless of whether AddTrellisUnitOfWork<TContext>() was called before or after this method.

Idempotent: calling this method more than once registers the behavior and publisher exactly once.

AddDomainEventDispatch(IServiceCollection, params Assembly[])

Scans the specified assemblies for concrete IDomainEventHandler<TEvent> implementations and registers each as a scoped service. Also registers the dispatch behavior and default publisher.

[RequiresUnreferencedCode("Assembly scanning requires unreferenced types. Use AddDomainEventHandler<TEvent, THandler> for AOT/trim scenarios.")]
[RequiresDynamicCode("Constructs closed generic IDomainEventHandler<TEvent> at runtime.")]
public static IServiceCollection AddDomainEventDispatch(this IServiceCollection services, params Assembly[] assemblies)

Parameters

services IServiceCollection

The service collection.

assemblies Assembly[]

Assemblies to scan for handler implementations.

Returns

IServiceCollection

The service collection for chaining.

Remarks

A single concrete type may implement IDomainEventHandler<TEvent> for multiple event types; each interface implementation is registered separately so the type is invoked for each event it declares.

Registration uses TryAddEnumerable(IServiceCollection, ServiceDescriptor) so calling this method repeatedly does not produce duplicate handler registrations.

Exceptions

ArgumentException

Thrown when assemblies is empty or contains a null element.

AddDomainEventHandler<TEvent, THandler>(IServiceCollection)

Registers a single IDomainEventHandler<TEvent> and ensures the dispatch behavior is wired up. Use this for AOT/trim scenarios where assembly scanning is not available.

public static IServiceCollection AddDomainEventHandler<TEvent, THandler>(this IServiceCollection services) where TEvent : IDomainEvent where THandler : class, IDomainEventHandler<TEvent>

Parameters

services IServiceCollection

The service collection.

Returns

IServiceCollection

The service collection for chaining.

Type Parameters

TEvent

The domain event type the handler responds to.

THandler

The handler implementation type.