Class Mediator
A default implementation of the IMediator interface.
public sealed class Mediator : IMediator
- Inheritance
-
Mediator
- Implements
- Inherited Members
Constructors
Mediator(IServiceProvider, IMediatorDiagnostic?)
A default implementation of the IMediator interface.
public Mediator(IServiceProvider serviceProvider, IMediatorDiagnostic? diagnostic = null)
Parameters
serviceProvider
IServiceProviderService provider to resolve handlers and behaviors.
diagnostic
IMediatorDiagnosticAn optional diagnostic service for logging activities and metrics.
Exceptions
- ArgumentNullException
Thrown when
serviceProvider
is null
Methods
Publish<TNotification>(TNotification, CancellationToken)
Sends a notification to multiple handlers.
public 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.
public 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.
public 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.
public 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.