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.