IRequestHandler.Handle without parameter but returning T
See original GitHub issueHello, I’m wondering how to model the situation bellow:
I have both Get:Create
and Post:Create
actions using the PRG pattern…
Then I have a handler for the Get:Create
that should return a viewModel/command with some filled data for the creation screen (could be a list of options it doesn’t matter).
I know that Mediatr provides IRequestHandler<T>
for void Handle(T)
and IRequestHandler<T,U>
for U Handle(T)
. But the thing is that I don’t need to pass any parameters to this handler; i just want the return. How can I solve this?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top Results From Across the Web
c# - IRequestHandler return void
Workaround solution for anyone who doesn't want to use Unit for some reason. You can create class named as VoidResult or EmptyResult then...
Read more >CQRS and MediatR in ASP.NET Core
Notice this time the IRequest signature doesn't have a type parameter. This is because we aren't returning a value.
Read more >Diagnosing and Fixing MediatR Container Issues
Publish returns only Task , there's nothing to assert in the return value. We can assert the side effects of the handler but...
Read more >Why I don't use MediatR for CQRS
Some guidelines recommend that the Handle method of ICommandHandler should not return anything. However, in real world applications, ...
Read more >Why Do We Need MediatR?
MediatR calls this code request handler. Request handlers must implement IRequestHandler<TRequest, TResponse> interface, where TRequest must ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So I understand that MediatR, when sending a request, uses the Request Type and Reflection to look up the Request’s handler and executes.
My question is, what if your request object has no properties? What you end up with is an empty class that is only there so that MediatR knows which handler to execute.
Is this intended? Or is there a better way to deal with this that I am overlooking?
My question is: Is this the intended/accepted/supported way to handle this scenario with the MediatR library?
Thanks.
I tried asking this question on stack overflow and they say that it is opinion based…
I have a similar scenario as above, where the only purpose of the request type is so that MediatR can find the handler for that particular query.
It looks like this is the way everybody does this with MediatR correct?
This isn’t an issue I’m only looking for confirmation on if what I am doing in my application is right or wrong and if there is another way or not?
Thanks.
Empty message is the way to go. Even though the request may have zero values, the request type itself signals the intent of the request - and that itself is information!