Disclaimer: Since there aren't any great answers yet, I decided to post a part from a great blog post I read a while ago, copied almost verbatim. You can find the full blog post here. So here it is:
We can define the following two interfaces:
public interface IQuery<TResult>
{
}
public interface IQueryHandler<TQuery, TResult> where TQuery : IQuery<TResult>
{
TResult Handle(TQuery query);
}
The IQuery<TResult>
specifies a message that defines a specific query with the data it returns using the TResult
generic type. With the previously defined interface we can define a query message like this:
public class FindUsersBySearchTextQuery : IQuery<User[]>
{
public string SearchText { get; set; }
public bool IncludeInactiveUsers { get; set; }
}
This class defines a query operation with two parameters, which will result in an array of User
objects. The class that handles this message can be defined as follows:
public class FindUsersBySearchTextQueryHandler
: IQueryHandler<FindUsersBySearchTextQuery, User[]>
{
private readonly NorthwindUnitOfWork db;
public FindUsersBySearchTextQueryHandler(NorthwindUnitOfWork db)
{
this.db = db;
}
public User[] Handle(FindUsersBySearchTextQuery query)
{
return db.Users.Where(x => x.Name.Contains(query.SearchText)).ToArray();
}
}
We can now let consumers depend upon the generic IQueryHandler
interface:
public class UserController : Controller
{
IQueryHandler<FindUsersBySearchTextQuery, User[]> findUsersBySearchTextHandler;
public UserController(
IQueryHandler<FindUsersBySearchTextQuery, User[]> findUsersBySearchTextHandler)
{
this.findUsersBySearchTextHandler = findUsersBySearchTextHandler;
}
public View SearchUsers(string searchString)
{
var query = new FindUsersBySearchTextQuery
{
SearchText = searchString,
IncludeInactiveUsers = false
};
User[] users = this.findUsersBySearchTextHandler.Handle(query);
return View(users);
}
}
Immediately this model gives us a lot of flexibility, because we can now decide what to inject into the UserController
. We can inject a completely different implementation, or one that wraps the real implementation, without having to make changes to the UserController
(and all other consumers of that interface).
The IQuery<TResult>
interface gives us compile-time support when specifying or injecting IQueryHandlers
in our code. When we change the FindUsersBySearchTextQuery
to return UserInfo[]
instead (by implementing IQuery<UserInfo[]>
), the UserController
will fail to compile, since the generic type constraint on IQueryHandler<TQuery, TResult>
won't be able to map FindUsersBySearchTextQuery
to User[]
.
Injecting the IQueryHandler
interface into a consumer however, has some less obvious problems that still need to be addressed. The number of dependencies of our consumers might get too big and can lead to constructor over-injection - when a constructor takes too many arguments. The number of queries a class executes can change frequently, which would require constant changes into the number of constructor arguments.
We can fix the problem of having to inject too many IQueryHandlers
with an extra layer of abstraction. We create a mediator that sits between the consumers and the query handlers:
public interface IQueryProcessor
{
TResult Process<TResult>(IQuery<TResult> query);
}
The IQueryProcessor
is a non-generic interface with one generic method. As you can see in the interface definition, the IQueryProcessor
depends on the IQuery<TResult>
interface. This allows us to have compile time support in our consumers that depend on the IQueryProcessor
. Let's rewrite the UserController
to use the new IQueryProcessor
:
public class UserController : Controller
{
private IQueryProcessor queryProcessor;
public UserController(IQueryProcessor queryProcessor)
{
this.queryProcessor = queryProcessor;
}
public View SearchUsers(string searchString)
{
var query = new FindUsersBySearchTextQuery
{
SearchText = searchString,
IncludeInactiveUsers = false
};
// Note how we omit the generic type argument,
// but still have type safety.
User[] users = this.queryProcessor.Process(query);
return this.View(users);
}
}
The UserController
now depends on a IQueryProcessor
that can handle all of our queries. The UserController
's SearchUsers
method calls the IQueryProcessor.Process
method passing in an initialized query object. Since the FindUsersBySearchTextQuery
implements the IQuery<User[]>
interface, we can pass it to the generic Execute<TResult>(IQuery<TResult> query)
method. Thanks to C# type inference, the compiler is able to determine the generic type and this saves us having to explicitly state the type. The return type of the Process
method is also known.
It is now the responsibility of the implementation of the IQueryProcessor
to find the right IQueryHandler
. This requires some dynamic typing, and optionally the use of a Dependency Injection framework, and can all be done with just a few lines of code:
sealed class QueryProcessor : IQueryProcessor
{
private readonly Container container;
public QueryProcessor(Container container)
{
this.container = container;
}
[DebuggerStepThrough]
public TResult Process<TResult>(IQuery<TResult> query)
{
var handlerType = typeof(IQueryHandler<,>)
.MakeGenericType(query.GetType(), typeof(TResult));
dynamic handler = container.GetInstance(handlerType);
return handler.Handle((dynamic)query);
}
}
The QueryProcessor
class constructs a specific IQueryHandler<TQuery, TResult>
type based on the type of the supplied query instance. This type is used to ask the supplied container class to get an instance of that type. Unfortunately we need to call the Handle
method using reflection (by using the C# 4.0 dymamic keyword in this case), because at this point it is impossible to cast the handler instance, since the generic TQuery
argument is not available at compile time. However, unless the Handle
method is renamed or gets other arguments, this call will never fail and if you want to, it is very easy to write a unit test for this class. Using reflection will give a slight drop, but is nothing to really worry about.
To answer one of your concerns:
So I'm looking for alternatives that encapsulate the entire query, but
still flexible enough that you're not just swapping spaghetti
Repositories for an explosion of command classes.
A consequence of using this design is that there will be a lot of small classes in the system, but having a lot of small/focused classes (with clear names) is a good thing. This approach is clearly much better then having many overloads with different parameters for the same method in a repository, as you can group those in one query class. So you still get a lot less query classes than methods in a repository.