根据MSDN数据库集:
DbSet<TEntity> Class
A
DbSet represents the collection of all entities in the context
, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext using the DbContext.Set method.
并根据MSDN数据库上下文:
DbContext Class
A
DbContext instance represents a combination of the Unit Of Work and
Repository patterns
such that it can be used to query from a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.
以便 EF
在内部使用 repository pattern
和 UOW
。
DbSet <----> Repository
DbContext <----> Unit Of Work
为什么我应该在我的 EF 顶部构建一个包含工作单元的存储库模式?
最佳答案
Why should i build a repository pattern with a unit of work on the top of my EF?
取决于您希望如何管理依赖项。
如果 Entity Framework 是您的抽象层并且数据库本身是依赖项,那么 Entity Framework 确实已经提供了您的存储库和工作单元。权衡是您的域依赖于 Entity Framework 。只要这种依赖性是可以接受的,你就很好。
另一方面,如果您想将 Entity Framework 本身视为一种可以在不更改域代码的情况下被替换掉的依赖项,那么您需要创建一个抽象作为其包装器。
基本上,这一切都归结为您划定什么是或不是“外部依赖性”的界限。对于某些项目,这无关紧要,对于某些项目,它是物理数据库,对于某些项目,它是数据访问框架等。
关于c# - 为什么我应该在我的 EF 顶部构建一个包含工作单元的存储库模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35322956/