Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use IQueryHandler when Handle() doesn't need any query params? #8

Open
ghost opened this issue Mar 31, 2017 · 1 comment
Open
Labels

Comments

@ghost
Copy link

ghost commented Mar 31, 2017

Hi,

I want to use the IQueryHandler for a GetAllTemplates() but it demands a IQuery Type to be associated with it so that it can be passed to the handle function, but this handler does not need any parameters and I would be creating an empty query type. How do I get around this?

@dotnetjunkie
Copy link
Owner

dotnetjunkie commented Mar 31, 2017

but this handler does not need any parameters and I would be creating an empty query type. How do I get around this?

You shouldn't go around it. The message type is the identifying part of your use case. Every use case should have a message, even if this means that the message is parameterless. It is not unusual to see query messages without parameters.

Don't forget the message isn't really 'empty', it already contains 2 essential pieces of information:

  • The name of the use case. This allows the message to be routed to the right handler.
  • The return type. This allows type safety, allows applying cross-cutting concerns, allows analysing this information in an automated fashion, and communicates to consumers what data they should expect.

Here is an example of a very valid query message:

public class GetCurrentUserLoginStatistics : IQuery<LoginStatistics> { }

This means that the handler doesn't use the query message, which is completely fine:

public class GetCurrentUserLoginStatisticsHandler
    : IQueryHandler<GetCurrentUserLoginStatistics, LoginStatistics>
{
    public LoginStatistics Handle(GetCurrentUserLoginStatistics query)
    {
        return new LoginStatistics { ... };
    }
}

Don't be worried about this and don't try to optimize this. This typically only leads to more pain than that it fixes. Again: you are defining your use cases with types. You effectively apply type based programming.

Repository owner locked and limited conversation to collaborators Jan 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant