question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Controller > Service > MediatR > Repository.... or Controller > MediatR > Service > ... ?

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ardaliscommented, Feb 22, 2021

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 be Endpoint -> Repository or even Endpoint -> DbContext.

See ApiEndpoints if you’re not familiar with it already.

0reactions
codefactorydevelopmentcommented, Feb 22, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Replacing service layer with MediatR - is it worth to do it?
Controller -> MediatR -> Command handlers -> Repository. It seems like with MediatR I can't have a single model for Create, Update and...
Read more >
MediatR vs Services or Why slices architecture better
This is how an ideal controller should look: When coming to application logic, there are two ways how it can be structured: with...
Read more >
CQRS and MediatR in ASP.NET Core
The IMediatR interface allows us to send messages to MediatR, which then dispatches to the relevant handlers. Because we already installed the ...
Read more >
All you need to know about introducing the MediatR library ...
Instead of referencing multiple services, controllers now only reference the MediatR class. This makes it easier to maintain code and apply ...
Read more >
How to implement CQRS with MediatR in ASP.NET Core
The handler is the one responsible for processing the request sent from the service calling MediatR. Each request sent to MediatR is assigned ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found