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.

IRequestHandler.Handle without parameter but returning T

See original GitHub issue

Hello, 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
zachpainter77commented, Dec 6, 2019

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.

//Empty Request Class????
//Should I do this some other way?
public class GetAllThingsQuery : IRequest<List<Thing>> {}

public class GetAllThingsQueryHandler : IRequestHandler<GetAllThingsQuery, List<Thing>> 
{
    private readonly IRepository<Thing> _myRepo;

    public GetAllThingsQueryHandler(IRepository<Thing> myRepo)
    {
      _myRepo = myRepo;
    }

    public async Task<List<Thing>> Handle(GetAllThingsQuery request, CancellationToken cancellationToken)
    {
        return await _myRepo.GetAllAsync();
    {
}

//from controller or whatever
_mediator.Send(new GetAllThingsQuery());

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.

6reactions
jbogardcommented, May 12, 2017

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!

Read more comments on GitHub >

github_iconTop 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 >

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