[Dot Net Core](Graphic series ) 15. MediatR — IRequest

Czxdas
3 min readDec 1, 2022

--

Examples of using MediatR can be found here.

For .Net Core, this package will be registered first:

First, an instance of MediatRServiceConfiguration will be created, and then ServiceRegistrar.AddRequiredServices will be called,
Register MediaR related services such as IMediator, ISender, IPublisher, ServiceFactory and IPipelineBehavior.

When calling ServiceRegistrar.AddMediatRClasses , the application component will register the service with the associated interface class that implements MediaR.

These interfaces include IRequestHandler<,>, INotificationHandler<>, IRequestPreProcessor<>, IRequestPostProcessor<,> and IRequestExceptionAction<,> and so on.
It should be noted that the interface will be registered as a different service depending on the GenericTypeArguments.

The difference in GenericTypeArguments also means that the developer decides which categories should use those MediaR-related interfaces.

[IRequest]

MediatR IRequest

In ConfigureServices, execute Service.AddMediateR(typeof(Startup)).

Service.AddMediatR(typeof(Startup))

Suppose we execute
var result = await _mediator. Send(new GetCustomersQuery());
as an example.

The Class inherits the interface IRequest, and Customer passes the data medium for the data model in it:

public class GetCustomersQuery : IRequest<List<Customer>>
{
}

Executed Handler Class:

public class GetCustomersQueryHandler : IRequestHandler<GetCustomersQuery, List<Customer>>
{
public async Task<List<Customer>> Handle(GetCustomersQuery request, CancellationToken cancellationToken)
{
return _repository.GetAll<Customer>().ToList();
}
}

The operation process is as follows:

Through the DI mechanism of .net Core, the Mediator and its field ServiceFactory can be obtained from the controller constructor.

Afterwards, multiple times will be used to process the entities corresponding to Type and Instance.

Then there are several steps in the figure:

First, directly add the instance RequestHandlerWrapperImpl<TRequest, TResponse> , the example here is TRequest is IRequestHandler<GetCustomersQuery, List>, and TResponse is List.
Second, delegate ServiceFactory to generate instances via registered services via IServiceProvider.GetService in the Dot Net Core framework.

Third, implement the Handle of RequestHandlerWrapperImpl .

Fourth, it will call GetHandle of RequestHandlerBase, the top-level parent class of RequestHandlerWrapperImpl, and use ServiceFactory to obtain the registered service
The IRequestHandler<TRequest, TResponse> implementation entity is also a category created by the developer.

Fifth, use ServiceFactory to implement entities from the registered service IPipelineBehavior<TRequest, TResponse>, and combine IRequestHandler<GetCustomersQuery, List> entities to execute the Handle functions of these entities in sequence.

The order of execution is RequestPreProcessorBehavior.Handle -> GetCustomersQueryHandler.Handle -> RequestExceptionProcessorBehavior.Handle -> RequestExceptionActionProcessorBehavior.Handle -> RequestPostProcessorBehavior.Handle

--

--

Czxdas
Czxdas

Written by Czxdas

Keep looking for Prajna wisdom

No responses yet