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.

Is it good to handle multiple requests in a single handler?

See original GitHub issue

Let’s take some CRUD application for example. I have 4 requests for Create, Read, Update, Delete actually 5 with Get All (I’m building resource-oriented web API)

Is it good to handle them in a single handler like or is something wrong with this approach?:

public class ProductRequestHandler : 
    IRequestHandler<CreateProductRequest, ProductResult>, 
    IRequestHandler<GetProductRequest, ProductResult>
    IRequestHandler<GetAllProductsRequest, ProductListResult>
    IRequestHandler<UpdateProductRequest, ProductResult>
    IRequestHandler<DeleteProductRequest, bool>
{
...
}

I’m trying to move away from DDD services that handle similar logic but I don’t know if it’s worth it.

Also, could someone tell me why shouldn’t my Create request return anything if I wanted to apply CQRS? What’s the benefit of commands not returning anything? Doing this just for the sake of “responsibility segregation” doesn’t convince me. My DDD services returned stuff in my Create methods and my Update methods.

I’m sorry, the question might be trivial for some of you but it’s actually pretty confusing topic. And I’d like to get my mind right

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
jbogardcommented, Jun 14, 2018

Ditto. Don’t combine your handlers, keep them separate, reduce coupling across handlers. For a real example, you can look at my various ContosoUniversity projects under my root profile.

4reactions
no1melmancommented, Jun 14, 2018

TL:DR; If you implement all those interfaces in that single class - are you not just going back to the n-tier architecture days, where the service layer was a class that held all the logic. If you want to do that, I don’t see the point in using MediatR.

Longer version

MediatR allows you to break your application up into distinct requests (like microservices) and handle a specific pipeline for each. This gives enormous benefits, both in maintainability and flexibility.

I feel that with your approach you’re kinda of breaking those principles thus rendering the underlying framework a tad irrelevant.

Nothing is obviously stopping you from doing that, but to answer your question is this a good approach, my gut feeling would be not really. This is just my take - I have no doubt others may feel differently

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can a single socket handle multiple concurrent requests?
In TCP, a single listen socket will be used to accept requests from connections. You can then distinguish each connection with its own...
Read more >
How does a web server handle multiple requests?
A web server generally handles multiple requests by queueing them up and then servicing them one at a time. Of course, this isn't...
Read more >
Ways to handler front end action which requires multiple ...
I went with a RESTful API design at first, but I'm now having second thought. Persistent data are saved in MongoDB. So to...
Read more >
How do servers handle multiple requests simultaneously ...
Servers can handle requests in different ways, utilizing threads or processes. Some examples include: Servers that use a single thread to ...
Read more >
Question about Go API and handling multiple requests
I am trying to create an api for using AWS with Go and then hook it up to mobile apps just to see...
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