Executing query via nested inner class query handler Background

I often see blogs/articles split out the implementation of the Query and QueryHandler classes. I had briefly asked a question on Stack Overflow about whether or not a self-executing Query class would be a violation of SOLID design principals (I deleted the question as it was feeling like the wrong place to ask for feedback). My opinion was that a Query class represents a single operation within a system, and that the separation of parameters/execution was unnecessary in most cases...

I understand that in some cases there may be legitimate reasons for doing so (i.e., using Query object AS your view model, not to my taste). One comment on the original question suggested the inability to cleanly apply decorators to a merged Query class in order to enhance query execution and although I had considered the suggestions at the time to be the responsibility of a QueryProcessor, it got me thinking: as modification of one will more often than not result in modification of the other. Perhaps nesting the handler class would be a happy middle ground as shown in the implementation below.

Implementation

/// &lt;summary&gt;
/// Query definition used to retrieve a specific user by their username.
/// &lt;/summary&gt;
public sealed class GetUserByUsernameQuery : Query&lt;User&gt;
{
    /// &lt;summary&gt;
    /// The unique user username to locate.
    /// &lt;/summary&gt;
    public Username Username { get; }

    /// &lt;summary&gt;
    /// Initializes a new instance of &lt;see cref="GetUserByUsernameQuery"/&gt;.
    /// &lt;/summary&gt;
    /// &lt;param name="username"&gt;The unique user username to locate.&lt;/param&gt;
    public GetUserByUsernameQuery(Username username)
    {
        if (username == null) throw new ArgumentNullException(nameof(username));

        Username = username;
    }

    /// &lt;summary&gt;
    /// Handles processing of a &lt;see cref="GetUserByUsernameQuery "/&gt; instance.
    /// &lt;/summary&gt;
    internal sealed class Handler : SqlQueryHandler&lt;GetUserByUsernameQuery, User&gt;
    {
        /// &lt;summary&gt;
        /// Executes the specified &lt;paramref name="query"/&gt; instance.
        /// &lt;/summary&gt;
        /// &lt;param name="query"&gt;The query instance to process.&lt;/param&gt;
        protected override IEnumerable&lt;User&gt; Execute(GetUserByUsernameQuery query)
        {
            using (var connection = OpenConnection())
                return connection.Query&lt;User&gt;("SELECT * FROM [dbo].[GetUserByUsername](@username);", query);
        }
    }
}


NOTE: No comments regarding SELECT *. The Inline Table Function explicitly selects only the required columns for the result set and I see no reason to define the columns again in the code.



I know some people consider inner classes to be evil, however in this case my thinking is that an inner class provides a nice balance between a self-executing Query and full separation between the Query/QueryHandler classes (i.e., easy to edit within a single file).

My rational:


There will only ever be a single handler class per query.
The handler will often have to be modified if the query class were to change.
Allows for decorators etc to be applied to the QueryHandler if required.
The QueryHandler class really cannot exist without the Query class.


Are there any major downsides to going down this path? Any other suggestions on how to achieve balance between the two ends of the spectrum? I would also be interested in feedback on using inline table functions for Dapper queries. I typically place my SQL in embedded resource files or a resource file, but I thought this might be better option?

NOTE: My use case does not require that the Query/QueryHandler ever need to live in different assemblies; thus dependencies are not a concern. And worst case, I could easily refactor if required.
I would say that we should be able to execute different query handlers given the same query parameters – for testing purposes. Simple data transfer object (query) being passed to the repository interface allows us to do it in a well-known and understandable way. 

The same time the following works well for me (SSAS MDX queries)…

To define MDX I use a partial class in multiple files (example):

// SalesSummary.cs
public static partial class MdxQueries
{
    public static IEnumerable&lt;TradeSummary&gt; SalesSummary(DateTime day, string department) =&gt;
        new MdxQuery($@"MDX SCRIPT WITH PARAMETER={day} AND PARAMETER={department}")
            .Select(r =&gt; new TradeSummary
            {
                Product = r["Product"],
                Total = r["Total"]
            });
}

// PurchaseSummary.cs
public static partial class MdxQueries
{
    public static IEnumerable&lt;TradeSummary&gt; PurchaseSummary(DateTime day, string department) =&gt;
        new MdxQuery($@"MDX SCRIPT WITH PARAMETER={day} AND PARAMETER={department}")
            .Select(r =&gt; new TradeSummary
            {
                Product = r["Product"],
                Total = r["Total"]
            });
}


Where library classes are:

public class MdxQuery : IEnumerable&lt;MdxRow&gt;
{
    static string ConnectionString =&gt; ConfigurationManager.ConnectionStrings["SSAS"]
        .ConnectionString;

    public MdxQuery(string mdx)
        : this(mdx, ConnectionString)
    {
    }

    public MdxQuery(string mdx, string connectionString)
    {
        _mdx = mdx;
        _connectionString = connectionString;
    }

    readonly string _mdx;
    readonly string _connectionString;

    public IEnumerator&lt;MdxRow&gt; GetEnumerator()
    {
        using (var connection = new AdomdConnection(_connectionString))
        {
            connection.Open();
            var command = connection.CreateCommand();
            command.CommandText = _mdx;
            var reader = command.ExecuteReader();
            while (reader.Read())
                yield return new MdxRow(reader);
        }
    }

    IEnumerator IEnumerable.GetEnumerator() =&gt; GetEnumerator();

    public static implicit operator MdxQuery(string mdx) =&gt;
        new MdxQuery(mdx);
}


Where:

public struct MdxRow
{
    internal MdxRow(AdomdDataReader reader)
    {
        Values = Enumerable.Range(0, reader.FieldCount)
            .ToDictionary(i =&gt; reader.GetName(i), i =&gt; new MdxValue(reader[i]));

        ValuesByIndex = Enumerable.Range(0, reader.FieldCount)
            .ToDictionary(i =&gt; i, i =&gt; new MdxValue(reader[i]));
    }

    public MdxValue this[string column] =&gt; Values[$"[Measures].[{column}]"];
    public MdxValue this[int column] =&gt; ValuesByIndex[column];
    Dictionary&lt;string, MdxValue&gt; Values { get; }
    Dictionary&lt;int, MdxValue&gt; ValuesByIndex { get; }
}


Where:

public struct MdxValue
{
    public MdxValue(object value)
    {
        Value = value;
    }

    object Value { get; }

    public static implicit operator string(MdxValue value) =&gt;
        Convert.ToString(value.Value);

    public static implicit operator int(MdxValue value) =&gt;
        Convert.ToInt32(value.Value ?? 0);

    public static implicit operator double(MdxValue value) =&gt;
        Convert.ToDouble(value.Value ?? 0);

    public static implicit operator int? (MdxValue value) =&gt;
        value.IsNull ? (int?)null : (int)value;

    public static implicit operator double? (MdxValue value) =&gt;
        value.IsNull ? (double?)null : (double)value;

    public static implicit operator DateTime(MdxValue value) =&gt;
        DateTime.Parse((string)value ?? DateTime.MinValue.ToString());

    public static implicit operator DateTime? (MdxValue value) =&gt;
        value.IsNull ? (DateTime?)null : (DateTime)value;

    public bool IsNull =&gt; Value == null;
}


All this stuff is used to implement repositories, which depend on DTO.