如何将实体框架包含扩展方法引入通用 IQueryable
事情是这样的。
我有一个接口,我会将属于 EntityFramework 库的 Include
扩展方法放入我不需要的 IRepository
层了解EntityFramework。
public interface IRepository<TEntity>
{
IQueryable<TEntity> Entities { get; }
TEntity GetById(long id);
TEntity Insert(TEntity entity);
void Update(TEntity entity);
void Delete(TEntity entity);
void Delete(long id);
}
所以我有扩展方法:
public static class IncludeExtension
{
static IQueryable<TEntity> Include<TEntity>(this IQueryable<TEntity> query,
string path)
{
throw new NotImplementedException();
}
}
但我不知道如何在这一层实现它,我会将它发送到我的EntityFramework(或任何将实现IRepository的人)来处理。
我需要同样的具有扩展方法的接口。
有光吗?
Here's the thing.
I have an interface, and I would to put the Include
extension method, who belongs to EntityFramework library, to my IRepository
layer wich dont needs to knows about EntityFramework.
public interface IRepository<TEntity>
{
IQueryable<TEntity> Entities { get; }
TEntity GetById(long id);
TEntity Insert(TEntity entity);
void Update(TEntity entity);
void Delete(TEntity entity);
void Delete(long id);
}
So I have the extension method:
public static class IncludeExtension
{
static IQueryable<TEntity> Include<TEntity>(this IQueryable<TEntity> query,
string path)
{
throw new NotImplementedException();
}
}
But I don't know how to implement it in this layer, and I would to send it to my EntityFramework (or whatever who will implement the IRepository) to deal with.
I need same to a Interface with a extension method.
Any light?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Include
是有漏洞的抽象,它仅适用于实体框架。 EF 4.1 已包含包含
在通用IQueryable
上,但它在内部仅将传递的通用IQueryable
转换为通用ObjectQuery
或DbQuery
并调用它们的Include
。以下是如何包装包含在存储库中 (存储库实现依赖于 EF,因此可以直接使用 EF 提供的
Include
)。Include
is leaky abstraction and it works only with Entity framework. EF 4.1 already containsInclude
over genericIQueryable
but it internally only converts passed genericIQueryable
to genericObjectQuery
orDbQuery
and calls theirInclude
.Here is some example how to wrap that include in repository (repository implementation is dependent on EF so it can use
Include
provided by EF directly).这个问题有点老了,但如果您或其他人仍在寻找,这里有两个独立于 EF 的解决方案:
1。基于反射的解决方案
如果
IQueryable
未转换为DbQuery
或ObjectQuery
,.NET Framework 会采用此解决方案。 >。跳过这些转换(及其提供的效率),您就可以将解决方案与实体框架解耦。2.基于动态的解决方案
这里,
QueryInclude
方法使用dynamic
类型来避免反射。This question is a bit old, but here are two EF-independent solutions if you or anyone else is still looking:
1. Reflection-based Solution
This solution is what the .NET Framework falls back to if the
IQueryable
does not cast to aDbQuery
orObjectQuery
. Skip these casts (and the efficiency it provides) and you've decoupled the solution from Entity Framework.2. Dyanmics-based Solution
Here the
QueryInclude<T>
method uses thedynamic
type to avoid reflection.在 Entity Framework 5.0 中,它们现在提供了 IQueryable 的扩展方法来添加 Include 功能。您只需要添加一个 using“System.Data.Entity”即可解析扩展方法。如需直接文档,请访问此处
In Entity Framework 5.0 they now provide an extension method to IQueryable to add the Include functionality. You will just need to add a using "System.Data.Entity" in order to resolve the extension method. For direct documentation go here