Exposing includes to client over webservice for database queries I am using a CQRS type patter for querying data in my application. I am currently using entity framework to connect to the database.

public class GetCustomerQuery : IQuery&lt;Customer&gt;
{
     public string CustomerId { get; set; }
}

public class GetCustomerQueryHandler : IQueryHandler&lt;GetCustomerQuery, Customer&gt;
{
    public void Handle(GetCustomerQuery query)
    {
        return _dbContext.Customers.FirstOrDefault(c =&gt; c.Id == query.CustomerId);
    }
}


The problem I am having with this design is that the Customer object has relationships with other entities:

public void Customer
{
    public string Id { get; set; }
    public IList&lt;Post&gt; Posts { get; set; }
}


The query handler shown above will not return the list of posts, as this is a one-to-many relationship in the database. The way I was thinking to solve this was to add a BaseQuery object which allowed settings includes:

public abstract class BaseQuery&lt;T&gt; : IQuery&lt;T&gt;
{
    public void AddInclude(Expression&lt;Func&lt;T, object&gt;&gt; include)
    {
      ....
    }
}


Is this considered a leaky abstraction, as it exposes the structure of the database?

Is there a better way around this problem?
I don't think you should add a method for setting includes on the query object. That means your consumers have to know which properties need to be included and which ones don't.

I think you should be explicit about it on the query. One approach could be to simply add a property:

// Slightly odd use of "WithPosts" to make it more comparable.
public class GetCustomerQuery : IQuery&lt;Customer&gt;
{
    public string CustomerId { get; private set; }

    public bool IncludePosts { get; private set; }

    public GetCustomerQuery(string customerId) 
    {
        CustomerId = customerId;
    }

    public GetCustomerQuery WithPosts()
    {
        return new GetCustomerQuery(CustomerId) { IncludePosts = true};
    }
}


You could also leverage some inheritance:

public class GetCustomerWithPostsQuery : GetCustomerQuery
{
    public GetCustomerWithPostsQuery(string customerId) : base(customerId)
    {
    }
}

public class GetCustomerQuery : IQuery&lt;Customer&gt;
{
    public string CustomerId { get; private set; }

    public GetCustomerQuery(string customerId) 
    {
        CustomerId = customerId;
    }

    public GetCustomerWithPostsQuery WithPosts()
    {
        return new GetCustomerWithPostsQuery(CustomerId);
    }
}


Either way:

var customerQueryWithoutPosts = new CustomerQuery("Dennis");
var customerQueryIncludingPosts = new CustomerQuery("Dennis").WithPosts();
// ... profit?


You can then either check the query's IncludePosts property OR have a different handler for the specialised GetCustomerWithPostsQuery query object.