Implementing a query
A query is expected to search for data and return a result. This search might be complicated, or it might be simple enough. The fact, though, is that we implement this pattern as an easy way to segregate the query logic from the originator of the request (such as the controller) and from the command logic. This type of separation increases a team’s ability to maintain either side of the application without stepping on each other’s toes, so to speak. We will similarly use the Mediator pattern to govern how we carry out these operations, and we will need a query model and a handler.
Creating a query model
Our model is simple enough as we can leave it empty or include properties that will play a part in the process to be carried out in the handler. We inherit IRequest<>
, which defines a return type. I would go out on a limb and say that a return type is necessary, considering that this is a query.
Let us look at two examples of queries...