Table of Contents

Interface IMediator

Namespace
SimpleMediator
Assembly
SimpleMediator.dll

Central coordinator used to send commands/queries and publish notifications.

public interface IMediator

Examples

var services = new ServiceCollection();
services.AddSimpleMediator(typeof(CreateReservation).Assembly);
var mediator = services.BuildServiceProvider().GetRequiredService<IMediator>();

var result = await mediator.Send(new CreateReservation(/* ... */), cancellationToken);

await result.Match(
    Left: error =>
    {
        Console.WriteLine($"Reservation failed: {error.GetMediatorCode()} - {error.Message}");
        return Task.CompletedTask;
    },
    Right: reservation => mediator.Publish(new ReservationCreatedNotification(reservation), cancellationToken));

Remarks

The default implementation (SimpleMediator) creates a DI scope per operation, runs behaviors in cascade, and delegates to the registered handlers.

Methods

Publish<TNotification>(TNotification, CancellationToken)

Publishes a notification that may be handled by zero or more handlers.

ValueTask<Either<MediatorError, Unit>> Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification

Parameters

notification TNotification

Instance to propagate.

cancellationToken CancellationToken

Optional token to cancel the dispatch.

Returns

ValueTask<Either<MediatorError, Unit>>

Type Parameters

TNotification

Notification type being distributed.

Send<TResponse>(IRequest<TResponse>, CancellationToken)

Sends a request that expects a TResponse response.

ValueTask<Either<MediatorError, TResponse>> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default)

Parameters

request IRequest<TResponse>

Request to process.

cancellationToken CancellationToken

Optional token to cancel the operation.

Returns

ValueTask<Either<MediatorError, TResponse>>

Response produced by the handler after flowing through the pipeline.

Type Parameters

TResponse

Response type returned by the handler.

Stream<TItem>(IStreamRequest<TItem>, CancellationToken)

Sends a streaming request that produces a sequence of items asynchronously.

IAsyncEnumerable<Either<MediatorError, TItem>> Stream<TItem>(IStreamRequest<TItem> request, CancellationToken cancellationToken = default)

Parameters

request IStreamRequest<TItem>

Stream request to process.

cancellationToken CancellationToken

Optional token to cancel the stream iteration.

Returns

IAsyncEnumerable<Either<MediatorError, TItem>>

Async enumerable of Either<MediatorError, TItem>, where each element represents either an error (Left) or a successful item (Right).

Type Parameters

TItem

Type of each item yielded by the stream.

Examples

await foreach (var result in mediator.Stream(new StreamProductsQuery(), cancellationToken))
{
    result.Match(
        Left: error => _logger.LogError("Failed to fetch product: {Error}", error.Message),
        Right: product => Console.WriteLine($"Product: {product.Name}"));
}

Remarks

Stream requests enable efficient processing of large datasets, real-time feeds, and batch operations without loading all data into memory at once.

The returned stream flows through all registered IStreamPipelineBehavior<TRequest, TItem> instances before reaching the handler. Each behavior can transform, filter, or enrich items.

Use await foreach to consume the stream. Dispose or break early to trigger cancellation.