It might have been asked before but I cannot find even in the official site why I should use MediatR and what problems it solves?
Is it because I can pass a single object in my constructor rather than a multitude of Interfaces?
Is it a replacement or competitor of ServicesBus etc...
Basically what are the benefit and what problem does it solve
I want to buy into it but its not clear to me why I should use it.
many thanks
Is it because I can pass a single object in my constructor rather than a multitude of Interfaces?
No.
Is it a replacement or competitor of ServicesBus etc...
No.
Basically what are the benefit and what problem does it solve
Among other things, one of the problem MediatR is trying to solve is DI Constructor Explosion in your MVC controllers
public DashboardController(
ICustomerRepository customerRepository,
IOrderService orderService,
ICustomerHistoryRepository historyRepository,
IOrderRepository orderRepository,
IProductRespoitory productRespoitory,
IRelatedProductsRepository relatedProductsRepository,
ISupportService supportService,
ILog logger
)
This is a highly debated topic and there is no one-size-fits-all solution, take a look at this question
How to avoid Dependency Injection constructor madness?
If you want to hide dependencies behind even more abstractions, then at this point you will want to take a look at all the options, like refactoring, separating concerns a little more, or other techniques.
In all honesty, the example problem and solution given on the MediatR website is a little suspect, however it does have its uses. In short, you need to choose whats right for you and your environment.
Why you might use it
MediatR simplifies communication between components in a .NET application, often aiding in implementing the CQRS (Command Query Responsibility Segregation) pattern. It allows decoupling of in-process messaging, making it easier to manage and maintain complex interactions within an application.
At its core, the Mediator pattern involves an object, the mediator, which encapsulates how a set of objects interact. MediatR acts as this mediator by handling requests and dispatching them to the appropriate handlers.
By using MediatR, you can avoid direct dependencies between components (like UI and business logic). It allows components to remain isolated, only depending on the MediatR interfaces, not on each other.
Why you might not want to use it