打破对实体框架的依赖

发布于 2024-10-31 21:49:15 字数 288 浏览 7 评论 0原文

我终于有了一个我认为结构非常好的项目。在阅读了 Fowler 的洋葱架构、学习了 IOC/DI 的 Ninject 并调整了我的 Psuedo 存储库类之后,我使用 EF 4.1,因此 DbSet 和 DbContext 提供了存储库的大部分内容;当我想要在我的存储库中“包含”其他实体/相关实体时,我现在面临着对实体框架的令人恼火的依赖。

有人对如何打破这种依赖性有任何建议吗? 例如,我有一个服务层,一旦我决定使用 .Ininclude,就会调用存储库,我就绑定到又胖又重的实体框架...我应该使用 .Join 还是可以以某种方式抽象 EF?

I have, what I think is a very well structured project, finally. After reading Fowler's onion architecture, learning Ninject for IOC/DI and tweaking my Psuedo repository classes, I am using EF 4.1 so DbSet and DbContext supply the repository for the most part; I am now faced with the irritating dependency on Entity Framework when wanting to "Include" other Entities / Related Entities in my repository.

Does anyone have any suggestions on how to break this dependency?
For example, I have a service layer that makes calls to Repository As soon as I decide to use .Include I am bound to the fat and heavy Entity Framework... Should I go with .Join or can EF be abstracted in some way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-11-07 21:49:15

Julie Lerman 在她的博客上发表了一篇文章,其中展示了 IQueryable 上的 Include 扩展方法,该方法允许您在代码中使用 Include 且不会破坏单元测试:

public static class MyExtensions
{
  public static IQueryable<TSource> Include<TSource>
    (this IQueryable<TSource> source, string path)
  {
    var objectQuery = source as ObjectQuery<TSource>;
    if (objectQuery != null)
    {
      return objectQuery.Include(path);
    }
    return source;
  }
}

请在此处查看她的完整博客文章:
敏捷实体框架 4 存储库第 5 部分: IObjectSet 和 Include

Julie Lerman has a post on her blog where she shows an Include extension method on IQueryable<T>, which allows you to use Include in your code and not break your unit tests:

public static class MyExtensions
{
  public static IQueryable<TSource> Include<TSource>
    (this IQueryable<TSource> source, string path)
  {
    var objectQuery = source as ObjectQuery<TSource>;
    if (objectQuery != null)
    {
      return objectQuery.Include(path);
    }
    return source;
  }
}

See her full blog post here:
Agile Entity Framework 4 Repository Part 5: IObjectSet and Include

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文