DAO和Repository模式有什么区别? [英] What is the difference between DAO and Repository patterns?

查看:639
本文介绍了DAO和Repository模式有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据访问对象(DAO)和存储库模式之间有什么区别?我正在开发一个应用程序,使用Enterprise Java Bean(EJB3),Hibernate ORM作为基础结构,以域驱动设计(DDD)和测试驱动开发(TDD)作为设计技术。 h2_lin>解决方案

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



DAO将被视为更靠近数据库,通常以表为中心。知识库将被视为更接近域,只处理总根。存储库可以使用DAO来实现,但是你不会做相反的事情。

另外,Repository通常是一个较窄的界面。它应该是一个简单的对象集合,带有 Get(id) Find(ISpecification)添加(实体)。类似于 Update 的方法适用于DAO,但不适用于存储库 - 使用存储库时,对实体的更改通常由单独的UnitOfWork进行跟踪。



看起来很常见的实现叫做Repository,它实际上更像是一个DAO,因此我认为它们之间的区别存在一些混淆。


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 is an abstraction of data persistence. Repository is an abstraction of a collection of objects.

DAO would be considered closer to the database, often table-centric. Repository would be considered closer to the Domain, dealing only in Aggregate Roots. A Repository could be implemented using DAO's, but you wouldn't do the opposite.

Also, a Repository is generally a narrower interface. It should be simply a collection of objects, with a Get(id), Find(ISpecification), Add(Entity). 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.

It does seem common to see implementations called a Repository that are really more of a DAO, and hence I think there is some confusion about the difference between them.

这篇关于DAO和Repository模式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆