CQRS And MediatR
Command Query Responsibility Segregation(CQRS) is a design pattern where you separate your command and query requests. The main idea is using different models for readig and writing operations.
If you will have write-heavy and read-heavy operations in your application you should consider to use CQRS pattern. I am developing a project with Onion Architecture and I am using CQRS pattern, if you are more person who loves to see examples more than read you can check the project. Link will be at the end of the Article.
As I say the purpose is to separate Commands(Create, Update, Delete) and queries(Read). And generally if you are developing ASP.NET when you search for CQRS you will come across MediatR package too.
MediatR is a package which helps us to use CQRS pattern easily. You can add it to your project from Manage NuGet Packages section.
I know you are thinking about what is MediatR and why do we need it for using CQRS Pattern?
The reason is that as I say before our commands and queries are separate and using different models. Thats where MediatR helps us. We are calling methods with MediatR package and sending the model classes as parameter and MediatR does understand where it should go and run the method.
Lets give a quick example to understand it better.
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly IMediator _mediator;
public UserController(IMediator mediator)
{
_mediator = mediator;
}
[HttpPost]
[Route("Login")]
public async Task<IActionResult> Login(LoginUserCommand command)
{
var res = await _mediator.Send(command);
return Ok(res);
}
}
Here we have a user controller and we have an endpoint for login operation as you can see in the code. And I injected IMediator interface. IMediator interface is coming from IMediatR package.
And when we want to call the service for operation we dont need to imlement any other service classes because we are sending it with IMediator’s Send method and it takes LoginUserCommand Class as the parameter. Now IMediator will find the method which is using the same model and run there and complete the operation.
public class LoginUserCommandHandler : IRequestHandler<LoginUserCommand, LoginUserViewModel>
{
public async Task<LoginUserViewModel> Handle(LoginUserCommand request, CancellationToken cancellationToken)
{
// Code for Login Operation
}
}
As you can see in the code, we have a method which implements IReqeustHandler and thats why Send method willl know which RequestHandler method needs to work from parameter model class.
This is the model class:
public class LoginUserCommand : IRequest<LoginUserViewModel>
{
public string EmailAddress { get; set; }
public string Password { get; set; }
}
you can find this and some other exaples from this repo. My purpose was giving a perspective about CQRS and MediatR with an example. I hope the Article gave that.
I am learning a lot while I am writing I hope you learned a lot too while you are reading and enjoyed it. Don’t forget to follow on Medium to keep learning together. See you soon :)