I use the Entity Framework. Let's say I have a realy complex linq query, that I need in multiple parts of my application, some services, some controllers.
Is there any design pattern I should follow?
First I thought, the Repository Pattern would perfectly be valid:
MyDto myRepository.GetDataFromComplexQuery();
But we shouldn't use the Repository Pattern with Entity Framework. The answer in the linked question is talking about using Services instead. However, I don't want to boil my service classes with a lot of (duplicating) complex query logic.
So I am wondering how I should design this and how to organize it in the solution?
Something like this feels very wrong:
namespace Foo.Data.DataHelpers
{
public class ComplexQuery
{
private DbContext _ctx;
public ComplexQuery(DbContext ctx)
{
_ctx = ctx;
}
public MyDto GetData()
{
query = //...
return query.Single();
}
}
}
Also sometimes the query is to complex and needs to replaced by plain old SQL for performance reason. But I don't think, this would change the answer?