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
- For small or simple applications with minimal complexity,
introducing MediatR can add unnecessary overhead. The Mediator
pattern, and thus MediatR, is more beneficial in applications with a
lot of cross-cutting concerns or where clear separation of commands
and queries is necessary. In straightforward applications with a
direct flow, the additional layer of abstraction may complicate the
codebase without providing significant benefits.
- Relying on MediatR means introducing an external dependency into
your project. In some scenarios, especially in tightly controlled or
highly regulated environments, adding external libraries may be
discouraged or require extensive vetting.
- Debugging can become more complex with MediatR. Tracing the flow of
a request through the system is more straightforward with direct
method calls. With MediatR, you have to trace through the mediator
and its dispatch mechanisms, which can obscure the direct path
from request to response.
- If not used judiciously, MediatR can lead to an anti-pattern where
too much logic is offloaded to handlers, making them bulky and
difficult to manage. It requires discipline to ensure that
handlers remain focused and maintainable.