Which method is better: 
Dependency Injection or MediatR approach?

Which method is better: Dependency Injection or MediatR approach?

In the MediatR approach, we have a cleaner and more organized code because we have hidden dependencies (Dependency Hiding) in case this becomes a problem in the future.

In the DI method, the relationship between service and contract is clearly known, but in the MediatR approach, this relationship does not have the necessary transparency, Troubleshooting is much more difficult and time-consuming than the DI method, And in most cases, trace is needed to find dependencies. Therefore, in large projects, the maintenance will be difficult.

Regarding performance, the DI approach is better than the MediatR approach because this approach uses the Reflection method, which is slower than the DI method.

As a result, this point should be considered in high-performance projects. Another point that should be considered for using the MediatR approach is the implementation of Unit-Test.

This issue is more difficult and time-consuming for MediatR than the DI method.

MediatR Approach:

- Offers cleaner and more organized code due to hidden dependencies (Dependency Hiding).

- Lacks transparency in the relationship between service and contract, making troubleshooting more challenging and time-consuming.

- Requires tracing to identify dependencies, complicating maintenance in large projects.

- Slower performance compared to the DI method due to its use of Reflection.

DI Method:

- Provides clear transparency in the relationship between service and contract.

- Easier troubleshooting and maintenance, especially in large projects.

- Better performance than the MediatR approach.

Considerations:

- Performance: The DI method outperforms the MediatR approach due to the slower nature of Reflection used in MediatR.

- Maintenance: MediatR can complicate troubleshooting and maintenance in large projects due to its hidden dependencies.

- Unit Testing: Implementing unit tests is more challenging and time-consuming with MediatR compared to the DI method.

In conclusion, while MediatR offers cleaner code with hidden dependencies, it may pose challenges in performance, maintenance, and unit testing, especially in high-performance projects where these factors are critical considerations.


"There is a big difference between something that is attractive and something that is practical"

If you use MediatR- MediatR Extensions will help you.

https://plugins.jetbrains.com/plugin/18347-mediatr-extensions



要查看或添加评论,请登录

Rahim Lotfi的更多文章

  • Understanding the Software Development Process

    Understanding the Software Development Process

    The software development process. The software development process is divided into two dimensions: horizontal and…

    7 条评论
  • Ambassador Pattern vs Anti-Corruption Layer

    Ambassador Pattern vs Anti-Corruption Layer

    Ambassador Pattern: This pattern serves as a representative or intermediary for managing communication between two…

    2 条评论
  • ????? ??????? Ambassador Pattern ? Anti-Corruption Layer

    ????? ??????? Ambassador Pattern ? Anti-Corruption Layer

    ????? ???? (Ambassador Pattern). ??? ???? ?? ????? ?? ??????? ?? ????? ???? ?????? ?????? ??? ?? ????? ?? ???????? ??…

  • Different order of code execution and deadlock

    Different order of code execution and deadlock

    A deadlock occurs when your program is engaged with various resources, and in different scenarios, the order of code…

  • Boundary of Data-Bounded context of data type

    Boundary of Data-Bounded context of data type

    In organizations and large projects, there are various types of data, each responsible for different tasks. Different…

    4 条评论
  • Dynamic Aggregate-DDD

    Dynamic Aggregate-DDD

    Programming today is all about costs. One of the most important challenges for software companies is performance.

    1 条评论
  • CQRS Implementation Story

    CQRS Implementation Story

    This is a story about points to consider when implementing the CQRS architecture pattern. This article is based on…

社区洞察

其他会员也浏览了