Class SimpleMediator
- Namespace
- SimpleMediator
- Assembly
- SimpleMediator.dll
Default IMediator implementation using Microsoft.Extensions.DependencyInjection.
public sealed class SimpleMediator : IMediator
- Inheritance
-
SimpleMediator
- Implements
- Inherited Members
Remarks
Creates a scope per request, resolves handlers, behaviors, pre/post processors and publishes notifications. Includes instrumentation via SimpleMediator.MediatorDiagnostics.
Constructors
SimpleMediator(IServiceScopeFactory, ILogger<SimpleMediator>?, IOptions<NotificationDispatchOptions>?)
Default IMediator implementation using Microsoft.Extensions.DependencyInjection.
public SimpleMediator(IServiceScopeFactory scopeFactory, ILogger<SimpleMediator>? logger = null, IOptions<NotificationDispatchOptions>? notificationOptions = null)
Parameters
scopeFactoryIServiceScopeFactoryFactory used to create scopes per operation.
loggerILogger<SimpleMediator>Optional logger for tracing and diagnostics.
notificationOptionsIOptions<NotificationDispatchOptions>Optional notification dispatch options.
Remarks
Creates a scope per request, resolves handlers, behaviors, pre/post processors and publishes notifications. Includes instrumentation via SimpleMediator.MediatorDiagnostics.
Methods
Publish<TNotification>(TNotification, CancellationToken)
Publishes a notification that may be handled by zero or more handlers.
public ValueTask<Either<MediatorError, Unit>> Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification
Parameters
notificationTNotificationInstance to propagate.
cancellationTokenCancellationTokenOptional token to cancel the dispatch.
Returns
- ValueTask<Either<MediatorError, Unit>>
Type Parameters
TNotificationNotification type being distributed.
Send<TResponse>(IRequest<TResponse>, CancellationToken)
Sends a request that expects a TResponse response.
public ValueTask<Either<MediatorError, TResponse>> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default)
Parameters
requestIRequest<TResponse>Request to process.
cancellationTokenCancellationTokenOptional token to cancel the operation.
Returns
- ValueTask<Either<MediatorError, TResponse>>
Response produced by the handler after flowing through the pipeline.
Type Parameters
TResponseResponse type returned by the handler.
Stream<TItem>(IStreamRequest<TItem>, CancellationToken)
Sends a streaming request that produces a sequence of items asynchronously.
public IAsyncEnumerable<Either<MediatorError, TItem>> Stream<TItem>(IStreamRequest<TItem> request, CancellationToken cancellationToken = default)
Parameters
requestIStreamRequest<TItem>Stream request to process.
cancellationTokenCancellationTokenOptional 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
TItemType 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.