Is it possible to use precompiled linq queries with repositories. Current I have my repositories set up like
public class CustomerRepository : EntityRepository
{
private readonly IContext _context;
public CustomerRepository(UnitOfWork uow)
{
_context = uow.context;
}
}
I would be able to create a precompiled query in the following manner by using my actual context class MyEntities : ObjectContext,IContext.
static Func<ObjectContext, int, Customer> _custByID;
public static Customer GetCustomer( int ID)
{
if (_custByID == null)
{
_custByID = CompiledQuery.Compile<MyEntities, int, Customer>
((ctx, id) => ctx.Customers.Where(c => c.CustomerID == id).Single());
}
return _custByID.Invoke(_context, ID);
}
The problem is that the Compile methods TArg0 takes in a type derived from ObjectContext. Since my whole purpose of using repositories with IContext was to hide entity framework related code using the above doesnt make sense. How should I go about using precompiled linq queries. Should I move them to a separate class library which references my model and the entity framework or is my understanding of the repositories incorrect? I am using EF4 in an ASP.net application.
The repository implementation should have the knowledge of the data access technology. The responsibility of the repository is to talk to the underlying data source inorder to satisfy the contract. It would be useless to have a repository if you can not perform such optimizations because the
ObjectSetis already a repository. Creating another layer of indirection between repository and EF is a useless abstraction.