solidservices icon indicating copy to clipboard operation
solidservices copied to clipboard

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

Open ghost opened this issue 8 years ago • 1 comments

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?

ghost avatar Mar 31 '17 08:03 ghost

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.

dotnetjunkie avatar Mar 31 '17 09:03 dotnetjunkie