DAO 和 Repository 模式有什么区别? [英] What is the difference between DAO and Repository patterns?
问题描述
数据访问对象 (DAO) 和存储库模式之间有什么区别?我正在使用 Enterprise Java Beans (EJB3)、Hibernate ORM 作为基础设施、域驱动设计 (DDD) 和测试驱动开发 (TDD) 作为设计技术来开发应用程序.
What is the difference between Data Access Objects (DAO) and Repository patterns? I am developing an application using Enterprise Java Beans (EJB3), Hibernate ORM as infrastructure, and Domain-Driven Design (DDD) and Test-Driven Development (TDD) as design techniques.
推荐答案
DAO
是数据持久化的抽象.Repository
是对象集合的抽象.
DAO
is an abstraction of data persistence.
Repository
is an abstraction of a collection of objects.
DAO
会被认为更接近数据库,通常以表为中心.Repository
会被认为更接近于域,只处理聚合根.
DAO
would be considered closer to the database, often table-centric.
Repository
would be considered closer to the Domain, dealing only in Aggregate Roots.
Repository
可以使用 DAO
来实现,但你不会反其道而行之.
Repository
could be implemented using DAO
's, but you wouldn't do the opposite.
此外,Repository
通常是一个较窄的接口.它应该是一个简单的对象集合,带有 Get(id)
、Find(ISpecification)
、Add(Entity)
.
Also, a Repository
is generally a narrower interface. It should be simply a collection of objects, with a Get(id)
, Find(ISpecification)
, Add(Entity)
.
Update
之类的方法适用于 DAO
,但不适用于 Repository
- 当使用 Repository
时,对实体的更改通常由单独的 UnitOfWork 跟踪.
A method like Update
is appropriate on a DAO
, but not a Repository
- when using a Repository
, changes to entities would usually be tracked by separate UnitOfWork.
看到称为 Repository
的实现似乎很常见,它实际上更像是一个 DAO
,因此我认为它们之间的区别存在一些混淆.
It does seem common to see implementations called a Repository
that is really more of a DAO
, and hence I think there is some confusion about the difference between them.
这篇关于DAO 和 Repository 模式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!