Mediatr: IRequestHandler.Handle without parameter but returning T

Created on 5 May 2017  路  7Comments  路  Source: jbogard/MediatR

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?

Most helpful comment

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!

All 7 comments

If you're not passing a parameter, what exactly is your handler handling?

Why not just send an empty message like:

public class GetCreateViewModelMessage {} : IRequest<SomeReturnType>

public class GetCreateViewModelMessageHandler : IRequestHandler<GetCreateViewModelMessage, SomeReturnType>
{
    public SomeReturnType Handle(GetCreateViewModelMessage message)
    {
        return new SomeReturnType();
    }
}

@dgw2jr Are you familiar with the PRG pattern? GET actions should only build the views, and I don't necessarily have to have parameters for that. My handler is in charge of building the viewmodel and returning it.

This approach of having an empty command is what I'm doing right now. Actually is the first thing that comes to mind when you think of a pseudo solution. But I think it's kinda hacky and I personally don't like this solution.

It sounds like you want a different abstraction then. What you are requesting is not possible. How would the mediator know what handler to select without a message/request type?

@dgw2jr Sorry, I thought it was implied in my question. Of course we can't do that within MediaR because of interface's generic constraints. What I'm asking though is for alternatives to that flow. I think this is such a common case that the solution would be simpler.

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!

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.

An empty class is perfectly fine, it signals the intent. You can compare it with an MVC action method/name (the intent) without any parameters.

MVC Action method > Request class
MVC Action method parameters > Request class properties

If you don't have any parameters, you don't need any properties

Was this page helpful?
0 / 5 - 0 ratings