在组织上,我应该在哪里把常用的查询使用C首先实体框架$ C $的时候?实体、框架、常用、时候

2023-09-02 11:45:27 作者:未满

我在铺设使用EF 4.1 code首先一个新的数据层,从较早的自制数据层迁移。

I'm laying out a new data layer using EF 4.1 Code First, migrating from an older homebrew data layer.

我已经设置了两个组件,一个是我的背景和一个用于所有的POCO code第一类。

I have set up two assemblies, one for my context and one for all the POCO code first classes.

我有一些业务逻辑,例如,用于在几个不同的地方对一个表的查询(或几个表)。 我应该在哪里放呢?的

I have some business logic, for instance, a query against one table (or a few tables) that is used in several different places. Where should I put it?

不能走了POCO类,因为它加入了一个情侣对表,因此需要一个上下文。它可以去的背景下,但这种情况下会变得臃肿与数百杂乱无章的查询。 是否有一个共同的模式或安排所有的业务逻辑?

It can't go in a POCO class because it joins a couple tables and so needs a context. It could go in the context, but that context would become bloated with hundreds of disorganized queries. Is there a common pattern or arrangement for all the business logic?

推荐答案

看起来像仓库模式是一切的解决方案... Repository是不是银弹!

Looks like the repository pattern is solution for everything ... Repository is not a silver bullet!

我使用的存储库模式与EF每天,因为当我开始我的当前项目数个月前,它看起来像推荐的解决方案。我的结论:

I'm using the repository pattern with EF everyday because when I started my current project several months ago it looked like recommended solution. My conclusions:

在库使得与EF互动更难。只需浏览相关EF标签问题,你会看到什么样的复杂性必须直接上下文,changetracker等进行处理。 在通用存储库是一些作品的CRUD操作而不是真正的DDD的场景。一旦您的存储库总根的作品(DDD)通用的方法失败。 因为你会嘲笑库和测试不依赖上层EF和数据库的总体思路,一旦你暴露的IQueryable 失败单元测试不会在所有的工作。 LINQ到实体的LINQ到对象和模拟的不处理参照完整性这么多次,我看到绿色的单元测试和运行时异常只子集。与EF正确的测试方法是集成测试。惩戒仓库只是用于测试不相关的数据访问真正的业务逻辑。如果您没有集成测试为您的企业的方法访问或持久化数据,你没有测试它。 在揭露专门的方法,如GetByXXX只是退后一步。大多数这些方法都只能使用一次。您将结束与code相似,用于包装的存储过程的调用库。许多开发人员喜欢的ORM,只是因为他们能避免这样的刚性结构。 Repository makes interaction with EF much harder. Just browse questions related to EF tags and you will see what complexities must be handled directly on the context, changetracker, etc. Generic repository is something that works for CRUD operations but not for real DDD scenarios. Once your repository works with aggregate roots (DDD) generic approach fails. Unit testing doesn't work at all because general idea that you will mock repository and test your upper layer without dependencies to EF and database fails once you expose IQueryable. Linq-to-entities is only subset of Linq-to-objects and mock doesn't handle referential integrity so many times I saw green unit tests and runtime exceptions. The correct testing approach with EF are integration tests. Mocking repository is only for testing real business logic not related to data access. If you don't have integration test for your business method accessing or persisting data you didn't test it. Exposing specialized methods like GetByXXX is just step back. Most of these methods are used only once. You will end with the code similar to repositories used for wrapping stored procedures calls. Many developers like ORM just because they can avoid such rigid architecture.

EF本身已经提供了存储库模式 - DbSet 对象集是仓库和的DbContext 的ObjectContext 是工程单位。所以,在我看来,库模式是过度使用。它可以在你需要严格的分层或在放置额外的逻辑来的方法,大项目是有用的。使用存储库,只是因为你想换行访问EF往往是没有价值code和复杂性只是附加层。

EF itself already offers repository pattern - DbSet and ObjectSet are repositories and DbContext and ObjectContext are Unit of works. So in my opinion repository pattern is overused. It can be useful in large projects where you need strict layering or in case of placing additional logic to its methods. Using repository just because you want to wrap access to EF is often valueless code and just additional layer of complexity.

您可以用同样的方法创建可重用的方法定义查询。

You can in the same way create reusable methods defining your queries.