I am using Entity Framework 6 and implemented some kind of repository pattern. Initially, my repositories had functions such as GetAllthose returned IEnumerableto avoid unnecessarily abstraction of the data layer. But my service level, which is a wrapper around my repositories and contains business logic, needs more control over requests. Control, such as high loading of related objects, selection of only certain columns, etc. Thus, trying to avoid just exposing DbContext at my service level, I adjusted the repositories so that they now return IQueryableso that the service level can do things like chain Includeto query, etc.
The problem is that my repositories have a method to return a single object, and the function simply returns POCO for the object. These are the most likely functions for intensive loading causing Include. It is impossible to do this from POCO, obviously.
Is there something like IQueryablefor a single object for an additional logic chain Include, etc.? Or a way to configure this logic for this? An example of my repository:
namespace Portal.Repositories
{
public class UploadRepository : IUploadRepository
{
private readonly IPortalContext m_context;
public UploadRepository(IPortalContext context)
{
m_context = context;
}
#region Methods
public int Count(Expression<Func<Upload, bool>> predicate)
{
return m_context.Uploads.Count(predicate);
}
public Upload Get(Expression<Func<Upload, bool>> predicate)
{
return m_context.Uploads.FirstOrDefault(predicate);
}
public Upload Insert(Upload entity)
{
return m_context.Uploads.Add(entity);
}
public Upload Delete(Upload entity)
{
return m_context.Uploads.Remove(entity);
}
public IQueryable<Upload> All()
{
return m_context.Uploads;
}
public IQueryable<Upload> Where(Expression<Func<Upload, bool>> predicate)
{
return m_context.Uploads.Where(predicate);
}
#endregion
}
}
You can see my method Getand how my services cannot choose when to Includeor not, because it just returns POCO.
source
share