Class Mediator
- Namespace
- Caesar
- Assembly
- Caesar.dll
Default IMediator implementation. Resolves handlers and behaviors from an IServiceProvider at call time, so it can be registered with any lifetime. Handler wrappers are stateless and cached per request type for the lifetime of the process, except for request types from a collectible assembly, whose entries go away when that assembly is unloaded.
public class Mediator : IMediator, ISender, IPublisher
- Inheritance
-
Mediator
- Implements
- Inherited Members
Constructors
Mediator(IServiceProvider)
Creates a mediator that publishes notifications sequentially (ForeachAwaitPublisher).
public Mediator(IServiceProvider serviceProvider)
Parameters
serviceProviderIServiceProviderProvider used to resolve handlers and behaviors.
Mediator(IServiceProvider, INotificationPublisher)
Creates a mediator with a custom notification publishing strategy.
public Mediator(IServiceProvider serviceProvider, INotificationPublisher publisher)
Parameters
serviceProviderIServiceProviderProvider used to resolve handlers and behaviors.
publisherINotificationPublisherStrategy used to invoke notification handlers.
Methods
CreateStream(object, CancellationToken)
Creates a stream for a streaming request whose type is only known at runtime. The object must implement IStreamRequest<TResponse>.
public IAsyncEnumerable<object?> CreateStream(object request, CancellationToken cancellationToken = default)
Parameters
requestobjectThe request.
cancellationTokenCancellationTokenCancellation token.
Returns
Remarks
Nothing is resolved or run until the stream is enumerated. An object that does not declare exactly one IStreamRequest<TResponse> is rejected with ArgumentException straight away.
CreateStream<TResponse>(IStreamRequest<TResponse>, CancellationToken)
Creates a stream for a streaming request, running the stream pipeline.
public IAsyncEnumerable<TResponse> CreateStream<TResponse>(IStreamRequest<TResponse> request, CancellationToken cancellationToken = default)
Parameters
requestIStreamRequest<TResponse>The request.
cancellationTokenCancellationTokenCancellation token.
Returns
- IAsyncEnumerable<TResponse>
Type Parameters
TResponseThe item type.
Remarks
Nothing is resolved or run until the stream is enumerated, so failures surface from the enumerator.
Publish(object, CancellationToken)
Publishes a notification whose type is only known at runtime. The object must implement INotification.
public Task Publish(object notification, CancellationToken cancellationToken = default)
Parameters
notificationobjectThe notification.
cancellationTokenCancellationTokenCancellation token.
Returns
PublishCore(IEnumerable<NotificationHandlerExecutor>, INotification, CancellationToken)
Publishes a notification through the configured INotificationPublisher. Override to intercept every publish (for example to add logging or tracing).
protected virtual Task PublishCore(IEnumerable<NotificationHandlerExecutor> handlerExecutors, INotification notification, CancellationToken cancellationToken)
Parameters
handlerExecutorsIEnumerable<NotificationHandlerExecutor>The resolved handler executors.
notificationINotificationThe notification.
cancellationTokenCancellationTokenCancellation token.
Returns
Publish<TNotification>(TNotification, CancellationToken)
Publishes a notification to all of its handlers using the configured INotificationPublisher.
public Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification
Parameters
notificationTNotificationThe notification.
cancellationTokenCancellationTokenCancellation token.
Returns
Type Parameters
TNotificationThe notification type.
Send(object, CancellationToken)
Sends a request whose type is only known at runtime. The object must implement IRequest<TResponse>; the response is returned as object.
public Task<object?> Send(object request, CancellationToken cancellationToken = default)
Parameters
requestobjectThe request.
cancellationTokenCancellationTokenCancellation token.
Returns
Remarks
Failures are reported through the returned task, never thrown from this call, except ArgumentNullException for a null request and ArgumentException for an object that does not declare exactly one IRequest<TResponse>.
Send<TResponse>(IRequest<TResponse>, CancellationToken)
Sends a request and returns its response after running the pipeline.
public Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default)
Parameters
requestIRequest<TResponse>The request.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<TResponse>
Type Parameters
TResponseThe response type.
Remarks
Failures are reported through the returned task, never thrown from this call, except ArgumentNullException and ArgumentException for a request Caesar cannot dispatch.
Send<TRequest>(TRequest, CancellationToken)
Sends a command (a request without a response) after running the pipeline.
public Task Send<TRequest>(TRequest request, CancellationToken cancellationToken = default) where TRequest : IRequest
Parameters
requestTRequestThe request.
cancellationTokenCancellationTokenCancellation token.
Returns
Type Parameters
TRequestThe request type.
Remarks
Failures are reported through the returned task, never thrown from this call, except ArgumentNullException for a null request.