Mediator.Lite - Lightweight implementation of mediator for .NET
You need to install Nuget package: link
You can do this with the following commands:
Install-Package Mediator.Lite
Or with .NET CLI:
dotnet add package Mediator.Lite
To use this library you need to create an handlers by realizing IRequestHandler
and register them in DI-container.
To create a handler for the specified command, you need to create the following classes:
public record YourCommand() : IRequest;
public class YourCommandHandler : IRequestHandler<YourCommand>
{
public Task Handle(YourCommand request, CancellationToken cancellationToken)
{
// Implementation...
}
}
Or if you need to return any response from handlers, you need to specify the response like this:
public record YourCommandResponse();
public record YourCommand() : IRequest<YourCommandResponse>;
public class YourCommandHandler : IRequestHandler<YourCommand, YourCommandResponse>
{
public Task<YourCommandResponse> Handle(YourCommand request, CancellationToken cancellationToken)
{
// Implementation...
}
}
Then you need to register handlers through this extension method for IServiceCollection
and specifying assembly to search:
services.AddMediator(typeof(YourCommandHandler).Assembly);
When the AddMediator()
method is called, it registers the Mediator
class for dependency resolving and looks for handler classes implementing the IRequestHandler
interface (both IRequestHandler<>
that does not return a response and IRequestHandler<,>
that does return a response) in the specified assembly and registers them as dependencies.
This library does not pretend to compete with the popular MediatR library, and does not try to borrow its code in any way. This library was made only for the purpose of using it in its own developments by its author (that is, me)