Table of Contents

Class CommandDispatcher

Namespace
Savvyio.Commands
Assembly
Savvyio.Commands.dll

Provides a default implementation of of the ICommandDispatcher interface.

public class CommandDispatcher : FireForgetDispatcher, ICommandDispatcher, IDispatcher
Inheritance
CommandDispatcher
Implements
Inherited Members

Examples

This example shows how to wire the built-in command dispatcher to a concrete command handler through ServiceLocator. The setup mirrors a mediator pipeline where the handler tracks processed order identifiers and the dispatcher invokes it with a fire-and-forget command.

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

namespace ExampleApp;

public sealed class CommandDispatcherExample
{
    public IReadOnlyCollection<string> Commit()
    {
        var handler = new CreateOrderHandler();
        var dispatcher = new CommandDispatcher(new ServiceLocator(serviceType => serviceType == typeof(ICommandHandler) ? new object[] { handler } : []));
        dispatcher.Commit(new CreateOrderCommand("ORD-42"));
        return handler.ProcessedOrders;
    }
}

public sealed class CreateOrderHandler : ICommandHandler
{
    public List<string> ProcessedOrders { get; } = new();
    public IFireForgetActivator<ICommand> Delegates => HandlerFactory.CreateFireForget<ICommand>(registry => registry.Register<CreateOrderCommand>(command => ProcessedOrders.Add(command.OrderId)));
}

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

Constructors

CommandDispatcher(IServiceLocator)

Initializes a new instance of the CommandDispatcher class.

public CommandDispatcher(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.

See Also