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
serviceLocatorIServiceLocatorThe provider of service implementations.
Methods
Commit(ICommand)
Commits the specified request using Fire-and-Forget/In-Only MEP.
public void Commit(ICommand request)
Parameters
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
requestICommandThe ICommand to commit.
setupAction<AsyncOptions>The AsyncOptions which may be configured.
Returns
Publish(IIntegrationEvent)
Publishes the specified request using Fire-and-Forget/In-Only MEP.
public void Publish(IIntegrationEvent request)
Parameters
requestIIntegrationEventThe 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
requestIIntegrationEventThe IIntegrationEvent to publish.
setupAction<AsyncOptions>The AsyncOptions which may be configured.
Returns
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
requestIQuery<TResult>The IQuery<TResult> to request.
setupAction<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
TResultThe 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
requestIQuery<TResult>The IQuery<TResult> to request.
Returns
- TResult
The outcome of the query operation.
Type Parameters
TResultThe 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
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.