I want to be able to specify a typed interface which can be used with queries. I ultimately want to be able to do something like:
var query = _queryfactory.Create<IActiveUsersQuery>();
var result = query.Execute(new ActiveUsersParameters("a", "b"));
foreach (var user in result)
{
Console.WriteLine(user.FirstName);
}
Looks simple enough, ehh? Notice that the query got typed parameters and a typed result. To be able to restrict the query factory to only contain queries we’ll have to specify something like:
public interface IQuery<in TParameters, out TResult>
where TResult : class
where TParameters : class
{
TResult Invoke(TParameters parameters);
}
But that’s going to spread like cancer:
// this was easy
public interface IActiveUsersQuery : IQuery<ActiveUsersParameters, UserResult>
{
}
//but the factory got to have those restrictions too:
public class QueryFactory
{
public void Register<TQuery, TParameters, TResult>(Func<TQuery> factory)
where TQuery : IQuery<TParameters, TResult>
where TParameters : class
where TResult : class
{
}
public TQuery Create<TQuery, TParameters, TResult>()
where TQuery : IQuery<TParameters, TResult>
where TParameters : class
where TResult : class
{
}
}
Which ultimately leads to a factory invocation like:
factory.Create<IActiveUsersQuery, ActiveUsersParameters, UserResult>();
Not very nice since the user have to specify the parameter type and the result type.
Am I trying to control it too much? Should I just create a dummy interface:
public interface IQuery
{
}
to show the intent and then let the users create whatever they like (since they, and not the factory, will invoke the correct query).
However, the last option isn’t very nice since it won’t let me decorate queries (for instance dynamically cache them by using a caching decorator).
I completely understand what you are trying to do here. You are applying the SOLID principles, so query implementations are abstracted away from the consumer, who merely sends a message (DTO) and gets a result. By implementing a generic interface for the query, we can wrap implementations with a decorator, which allows all sorts of interesting behavior, such as transactional behavior, auditing, performance monitoring, caching, etc, etc.
The way to do this is to define the following interface for a messsage (the query definition):
And define the following interface for the implemenation:
The
IQuery<TResult>is some sort of marker interface, but this allows us to define statically what a query returns, for instance:An implementation can be defined as follows:
Consumers can no take a dependency on the
IQueryHandler<TQuery, TResult>to execute queries:This allows you to add cross-cutting concerns to query handlers, without consumers to know this and this gives you complete compile time support.
The biggest downside of this approach (IMO) however, is that you’ll easily end up with big constructors, since you’ll often need to execute multiple queries, (without really violating the SRP).
To solve this, you can introduce an abstraction between the consumers and the
IQueryHandler<TQuery, TResult>interface:Instread of injecting multiple
IQueryHandler<TQuery, TResult>implementations, you can inject oneIQueryProcessor. Now the consumer will look like this:The
IQueryProcessorimplementation can look like this:It depends on the container (it is part of the composition root) and uses
dynamictyping (C# 4.0) to execute the queries.This
IQueryProcessoris effectively yourQueryFactory.There are downsides of using this
IQueryProcessorabstraction though. For instance, you miss the possibility to let your DI container verify whether a requestedIQueryHandler<TQuery, TResult>implementation exists. You’ll find out at the moment that you callprocessor.Executeinstead when requesting a root object. You can solve this by writing an extra integration test that checks if anIQueryHandler<TQuery, TResult>is registered for each class that implementsIQuery<TResult>. Another downside is that the dependencies are less clear (theIQueryProcessoris some sort of ambient context) and this makes unit testing a bit harder. For instance, your unit tests will still compile when a consumer runs a new type of query.You can find more information about this design in this blog post: Meanwhile… on the query side of my architecture.