Table of Contents

Class Mediator

Namespace
Savvyio.Extensions
Assembly
Savvyio.Extensions.Dispatchers.dll

Provides a default implementation of the IMediator interface.

public class Mediator : IMediator, ICommandDispatcher, IDomainEventDispatcher, IIntegrationEventDispatcher, IQueryDispatcher, IDispatcher
Inheritance
Mediator
Implements
Extension Methods

Examples

Mediator is the single-entry-point dispatcher that routes commands, queries, and events to their handlers without requiring separate dispatcher injections. Create it with a ServiceLocator that resolves handlers by service type.

using System;
using System.Collections.Generic;
using Savvyio;
using Savvyio.Commands;
using Savvyio.Dispatchers;
using Savvyio.Extensions;
using Savvyio.Handlers;

namespace ExampleApp;

public sealed class MediatorExample
{
    public void Dispatch()
    {
        var handler = new CreateOrderHandler();
        var locator = new ServiceLocator(serviceType =>
            serviceType == typeof(ICommandHandler) ? new object[] { handler } : Array.Empty<object>());
        var mediator = new Mediator(locator);

        mediator.Commit(new CreateOrderCommand("SO-42"));
        Console.WriteLine($"Processed orders: {handler.ProcessedOrders.Count}");
    }
}

public sealed class CreateOrderHandler : ICommandHandler
{
    public List<string> ProcessedOrders { get; } = new();

    public IFireForgetActivator<ICommand> Delegates =>
        HandlerFactory.CreateFireForget<ICommand>(r =>
            r.Register<CreateOrderCommand>(cmd => ProcessedOrders.Add(cmd.OrderId)));
}

public sealed record CreateOrderCommand(string OrderId) : Request, ICommand;

Constructors

Mediator(IServiceLocator)

Initializes a new instance of the Mediator class.

public Mediator(IServiceLocator serviceLocator)

Parameters

serviceLocator IServiceLocator

The provider of service implementations.

Methods

Commit(ICommand)

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

public void Commit(ICommand request)

Parameters

request ICommand

The ICommand to commit.

CommitAsync(ICommand, Action<AsyncOptions>)

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

public Task CommitAsync(ICommand request, Action<AsyncOptions> setup = null)

Parameters

request ICommand

The ICommand to commit.

setup Action<AsyncOptions>

The AsyncOptions which may be configured.

Returns

Task

A Task that represents the asynchronous operation.

Publish(IIntegrationEvent)

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

public void Publish(IIntegrationEvent request)

Parameters

request IIntegrationEvent

The IIntegrationEvent to publish.

PublishAsync(IIntegrationEvent, Action<AsyncOptions>)

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

public Task PublishAsync(IIntegrationEvent request, Action<AsyncOptions> setup = null)

Parameters

request IIntegrationEvent

The IIntegrationEvent to publish.

setup Action<AsyncOptions>

The AsyncOptions which may be configured.

Returns

Task

A Task that represents the asynchronous operation.

QueryAsync<TResult>(IQuery<TResult>, Action<AsyncOptions>)

Queries the specified request asynchronous using Request-Reply/In-Out MEP.

public Task<TResult> QueryAsync<TResult>(IQuery<TResult> request, Action<AsyncOptions> setup = null)

Parameters

request IQuery<TResult>

The IQuery<TResult> to request.

setup Action<AsyncOptions>

The AsyncOptions which may be configured.

Returns

Task<TResult>

A Task<TResult> that represents the asynchronous operation. The task result contains the outcome of the query operation.

Type Parameters

TResult

The type of the result to return.

Query<TResult>(IQuery<TResult>)

Queries the specified request using Request-Reply/In-Out MEP.

public TResult Query<TResult>(IQuery<TResult> request)

Parameters

request IQuery<TResult>

The IQuery<TResult> to request.

Returns

TResult

The outcome of the query operation.

Type Parameters

TResult

The type of the result to return.

Raise(IDomainEvent)

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

public void Raise(IDomainEvent request)

Parameters

request IDomainEvent

The 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

request IDomainEvent

The IDomainEvent to raise.

setup Action<AsyncOptions>

The AsyncOptions which may be configured.

Returns

Task

A Task that represents the asynchronous operation.

See Also