数据访问对象(DAO)和存储库模式之间的区别是什么?我正在开发一个应用程序,使用企业Java bean (EJB3), Hibernate ORM作为基础设施,领域驱动设计(DDD)和测试驱动开发(TDD)作为设计技术。


当前回答

如果我们考虑设计模式DAO和Repository的原始定义,就会发现它们非常相似。主要的区别是字典和它们的来源(Oracle vs. Fowler)。

引用:

DAO - "separates a data resource's client interface from its data access mechanisms" and "The DAO implements the access mechanism required to work with the data source. The data source could be a persistent store like an RDBMS, an external service like a B2B exchange, a repository like an LDAP database, or a business service accessed via CORBA Internet Inter-ORB Protocol (IIOP) or low-level sockets." Repository - "Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects." and "Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers."

基于这些引用,这两种设计模式都调解了领域层和数据层之间的通信。此外,Repository与ORM相关联,而DAO则是用于从任何地方访问数据的更通用的接口。

其他回答

根据Spring文档,没有明显的区别:

@Repository注释是任何类的标记,该类满足 存储库(也称为数据访问对象)的角色或原型 或刀)。

DAO是数据持久性的抽象。 存储库是对象集合的抽象。

DAO被认为更接近数据库,通常以表为中心。 存储库将被认为更接近域,只处理聚合根。

存储库可以使用DAO来实现,但您不会做相反的事情。

另外,Repository通常是一个更窄的接口。它应该是一个简单的对象集合,包含Get(id)、Find(isspec)、Add(Entity)。

像Update这样的方法适用于DAO,但不适用于Repository——当使用Repository时,对实体的更改通常由单独的UnitOfWork跟踪。

被称为Repository的实现实际上更像是一个DAO,这似乎很常见,因此我认为它们之间的区别存在一些混淆。

如果我们考虑设计模式DAO和Repository的原始定义,就会发现它们非常相似。主要的区别是字典和它们的来源(Oracle vs. Fowler)。

引用:

DAO - "separates a data resource's client interface from its data access mechanisms" and "The DAO implements the access mechanism required to work with the data source. The data source could be a persistent store like an RDBMS, an external service like a B2B exchange, a repository like an LDAP database, or a business service accessed via CORBA Internet Inter-ORB Protocol (IIOP) or low-level sockets." Repository - "Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects." and "Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers."

基于这些引用,这两种设计模式都调解了领域层和数据层之间的通信。此外,Repository与ORM相关联,而DAO则是用于从任何地方访问数据的更通用的接口。

存储库不过是设计良好的DAO。

ORM是以表为中心的,而不是DAO。

没有必要在存储库中使用多个DAO,因为DAO本身可以对ORM存储库/实体或任何DAL提供者做完全相同的事情,无论汽车在哪里以及如何持久化1个表、2个表、n个表、半个表、一个web服务、一个表和一个web服务等等。 服务使用多个DAO/存储库。

我自己的DAO,假设CarDao只处理Car DTO,我的意思是,只在输入中接收Car DTO只在输出中返回Car DTO或Car DTO集合。

因此,就像Repository一样,对于业务逻辑来说,DAO实际上是一个IoC,允许持久性接口不受持久性策略或遗留问题的影响。 DAO既封装了持久性策略,也提供了与域相关的持久性接口。 对于那些不理解定义良好的DAO实际上是什么的人来说,存储库只是另一个词。

在一个非常简单的句子中:显著的区别是 存储库代表集合,而dao更接近数据库,通常更接近 table-centric。