Interface IMediator
An interface defining a mediator to encapsulate request/response and publishing patterns.
public interface IMediator
Examples
Example usage of IMediator:
// Define a request and response
public record HelloRequest(string Name) : IRequest<string>;
public class HelloRequestHandler : IRequestHandler<HelloRequest, string>
{
public ValueTask<string> Handle(HelloRequest request, CancellationToken cancellationToken)
{
return ValueTask.FromResult($"Hello, {request.Name}!");
}
}
// Define a notification
public record HelloNotification(string Message) : INotification;
public class HelloNotificationHandler : INotificationHandler<HelloNotification>
{
public ValueTask Handle(HelloNotification notification, CancellationToken cancellationToken)
{
Console.WriteLine($"Notification received: {notification.Message}");
return ValueTask.CompletedTask;
}
}
// Usage
var services = new ServiceCollection();
// Register Mediator services
services.AddMediator();
// Register Handlers
services.AddScoped<IRequestHandler<HelloRequest, string>, HelloRequestHandler>();
services.AddScoped<INotificationHandler<HelloNotification>, HelloNotificationHandler>();
var provider = services.BuildServiceProvider();
var mediator = provider.GetRequiredService<IMediator>();
// Sending a request
var response = await mediator.Send(new HelloRequest("World"));
Console.WriteLine(response); // Output: Hello, World!
// Publishing a notification
await mediator.Publish(new HelloNotification("This is a test notification"));
Methods
Publish<TNotification>(TNotification, CancellationToken)
Sends a notification to multiple handlers.
ValueTask Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification
Parameters
notification
TNotificationThe notification to send.
cancellationToken
CancellationTokenCancellation token.
Returns
- ValueTask
Awaitable task for the notification operation.
Type Parameters
TNotification
The type of notification being sent.
Exceptions
- ArgumentNullException
Thrown when
notification
is null.
Send(object, CancellationToken)
Sends a request to the appropriate handler and returns the response.
ValueTask<object?> Send(object request, CancellationToken cancellationToken = default)
Parameters
request
objectThe request to send to the handler. The request object must implement IRequest<TResponse> interface.
cancellationToken
CancellationTokenCancellation token.
Returns
Exceptions
- ArgumentNullException
Thrown when
request
is null.- InvalidOperationException
Thrown when
request
does not implement IRequest<TResponse> interface.
Send<TResponse>(IRequest<TResponse>, CancellationToken)
Sends a request to the appropriate handler and returns the response.
ValueTask<TResponse?> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default)
Parameters
request
IRequest<TResponse>The request to send to the handler.
cancellationToken
CancellationTokenCancellation token.
Returns
- ValueTask<TResponse>
Awaitable task returning the
TResponse
.
Type Parameters
TResponse
The type of response from the handler.
Exceptions
- ArgumentNullException
Thrown when
request
is null.
Send<TRequest, TResponse>(TRequest, CancellationToken)
Sends a request to the appropriate handler and returns the response.
ValueTask<TResponse?> Send<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken = default) where TRequest : IRequest<TResponse>
Parameters
request
TRequestThe request to send to the handler.
cancellationToken
CancellationTokenCancellation token.
Returns
- ValueTask<TResponse>
Awaitable task returning the
TResponse
.
Type Parameters
TRequest
The type of request being sent.
TResponse
The type of response from the handler.
Exceptions
- ArgumentNullException
Thrown when
request
is null.