Is it good to handle multiple requests in a single handler?
See original GitHub issueLet’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:
- Created 5 years ago
- Reactions:5
- Comments:16 (4 by maintainers)
Top GitHub Comments
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.
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