Generic queries and handlers without reflection I'm trying to implement generic queries and handlers so that I can make requests with simple syntax, like this:

var query = new HelloQuery("hi");
var result = processor.Process(query);


This post explains a brilliant technique for accomplishing this, but it uses reflection and dynamic to resolve handlers for queries.  I came up with an alternative using generics.

The central idea is the IQuery:

public interface IQuery&lt;TSelf, TResult&gt;
    where TSelf : IQuery&lt;TSelf, TResult&gt;
{
}


One such implementation could be:

public class HelloQuery : IQuery&lt;HelloQuery, string&gt;
{
    public HelloQuery(string message)
    {
        this.message = message;
    }

    private readonly string message;
    public string Message
    {
        get { return this.message; }
    }
}


The TSelf type argument is the key difference between my technique and the other post's technique.  It allows us to avoid reflection in the HandlerResolver:

public interface IHandlerResolver
{
    IQueryHandler&lt;TQuery, TResult&gt; GetHandlerFor&lt;TQuery, TResult&gt;()
        where TQuery: IQuery&lt;TQuery, TResult&gt;;
}

public class HandlerResolver : IHandlerResolver
{
    private readonly IContainer container;

    public HandlerResolver(IContainer container)
    {
        this.container = container;
    }

    public IQueryHandler&lt;TQuery, TResult&gt; GetHandlerFor&lt;TQuery, TResult&gt;()
        where TQuery : IQuery&lt;TQuery, TResult&gt;
    {
        var handler = this.container.Get&lt;IQueryHandler&lt;TQuery, TResult&gt;&gt;();
        return handler;
    }
}


You would register handlers at the composition root.  Here's the interface for a handler, and an implementation for HelloQuery:

public interface IQueryHandler&lt;TQuery, TResult&gt;
    where TQuery : IQuery&lt;TQuery, TResult&gt;
{
    TResult Handle(TQuery request);
}

public class HelloQueryHandler : IQueryHandler&lt;HelloQuery, string&gt;
{
    public string Handle(HelloQuery request)
    {
        var response = string.Format("Well \"{0}\" yourself!", request.Message);
        return response;
    }
}


And finally, the actual query processor:

public interface IQueryProcessor
{
    TResult Process&lt;TQuery, TResult&gt;(IQuery&lt;TQuery, TResult&gt; request)
        where TQuery : IQuery&lt;TQuery, TResult&gt;;
}

public class QueryProcessor : IQueryProcessor
{
    private readonly IHandlerResolver handlerResolver;

    public QueryProcessor(IHandlerResolver handlerResolver)
    {
        this.handlerResolver = handlerResolver;
    }

    public TResult Process&lt;TQuery, TResult&gt;(IQuery&lt;TQuery, TResult&gt; request)
        where TQuery : IQuery&lt;TQuery, TResult&gt;
    {
        var handler = this.handlerResolver.GetHandlerFor&lt;TQuery, TResult&gt;();
        var response = handler.Handle((TQuery)request);
        return response;
    }
}


The interfaces would be simpler without the queries' TSelf type argument, but then the C# compiler can't infer the types, and you end up having to spell out the full Process() call like processor.Process&lt;HelloQuery, string&gt;(query).  I'm trying to avoid this ugliness.

My question is:  Is the TSelf type argument a code smell?  Is there a better alternative that doesn't require falling back on reflection?
One objection I could raise is that your generic type constraint doesn't guarantee what you think it might guarantee. Eg. you could have:

// TSelf is actually not T of Self
public class HelloQuery : IQuery&lt;GoodbyeQuery, string&gt; 
{
}

// This one is fine
public class GoodbyeQuery: IQuery&lt;GoodbyeQuery, string&gt; 
{
}


In other words, TSelf doesn't actually have to be the T of the class itself. This is not horrible, especially since you seem to only be using the interface as metadata, but it can still lead to confusing results during dependency injection if you've accidentally screwed up a class definition.

However, as long as you're not a masochist and don't declare nonsensical types like this, there is nothing wrong with this. This is an established design pattern known as the curiously recurring template pattern.