Search code examples
c#asp.net-core-webapiarchitectural-patterns

MediatR when and why I should use it?


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


Solution

  • 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

    1. 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.

    2. 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.

    3. 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

    1. 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.
    2. 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.
    3. 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.
    4. 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.