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<Customer>
{
public string CustomerId { get; set; }
}
public class GetCustomerQueryHandler : IQueryHandler<GetCustomerQuery, Customer>
{
public void Handle(GetCustomerQuery query)
{
return _dbContext.Customers.FirstOrDefault(c => 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<Post> 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<T> : IQuery<T>
{
public void AddInclude(Expression<Func<T, object>> include)
{
....
}
}
Is this considered a leaky abstraction, as it exposes the structure of the database?
Is there a better way around this problem?
Posts
need to be anIList
? IIRC if you make it something that can be lazy-loaded, you can avoid the problem that way \$\endgroup\$