Controller > Service > MediatR > Repository.... or Controller > MediatR > Service > ... ?
See original GitHub issueI find both in many examples over web but not definitive answer why.
It seems to me that having Controller > Service > MediatR > Repository
is the natural way especially when it comes to the situation that POST/PUT/PATCH (in other words, command http requests) can return results because within the service we can issue a command and after that issue a query in order to return result to the client. Having MediatR directly in controller would either force command handler to return value or have retrieve logic in controller, both of which is not nice.
If not done that way, i see no other way how to return something to the client in response to POST requests and not violate CQRS rules that commands don’t return anything.
In short:
Is it usual/best practice to have Controller > Service > MediatR > ...
?
thanks
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
Jimmy’s approach of Keep-It-Simple is a good place to start. Just use
Controller > MediatR > do work
as a default approach. If you desire more structure and consistency between how various handlers behave, or if you don’t always need to do much interesting work (many queries are trivial for instance), you can look at migrating from Controllers to ApiEndpoints and then only using MediatR when there is a reason to use its pipeline. Most queries will just beEndpoint -> Repository
or evenEndpoint -> DbContext
.See ApiEndpoints if you’re not familiar with it already.
@ardalis @jbogard
guys, your responses gave me some very valuable insights and thank you both a bunch!
as far as i’m concerned, this issue can be closed.