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


当前回答

dao可能并不总是显式地与Only DataBase相关,

它可以只是一个访问数据的接口,在这种情况下,数据可以从DB/Cache甚至REST(现在不太常见,因为我们可以很容易地在各自的REST /IPC客户端中分离它们)访问。

这种方法中的回购可以由任何ORM解决方案实现,如果底层缓存/回购发生变化,它不会被传播/影响服务/业务层。

dao可以接受/返回域类型。考虑一个学生域,关联的DAO类将是StudentDao

StudentDao {

    StudentRepository, 
    StudentCache, 

    Optional<Student> getStudent(Id){

       // Use StudentRepository/StudentCache to Talk to DD & Cache 
       // Cache Type can be the same as Domain Type, DB Type(Entities) should be a Same/Different Type.
   
    }

    Student updateStudent(Student){

       // Use StudentRepository/StudentCache to Talk to DD & Cache 
       // Cache Type can be the same as Domain Type, DB Type(Entities) should be a Same/Different Type.
   
    }

}

dao可以接受/返回子域类型。考虑一个学生域,它有子域,比如考勤/科目,它有一个DAO类StudentDao,

StudentDao {

    StudentRepository, SubjectRepository, AttendanceRepository
    StudentCache, SubjectCache, AttendanceCache

    Set<Subject> getStudentSubject(Id){

       // Use SubjectRepository/SubjectCache to Talk to DD & Cache 
       
    }

    Student addNewSubjectToStudent(ID, Subject){

       // Use SubjectRepository/SubjectCache to Talk to DD & Cache 
      
   
    }

}

其他回答

存储库不过是设计良好的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或Repository模式是否最适用于以下情况: 假设您希望为各种类型的数据源(如RDBMS、LDAP、OODB、XML存储库和平面文件)的持久机制提供统一的数据访问API。

如果有兴趣,也可以参考以下链接:

http://www.codeinsanity.com/2008/08/repository-pattern.html

http://blog.fedecarg.com/2009/03/15/domain-driven-design-the-repository/

http://devlicio.us/blogs/casey/archive/2009/02/20/ddd-the-repository-pattern.aspx

http://en.wikipedia.org/wiki/Domain-driven_design

http://msdn.microsoft.com/en-us/magazine/dd419654.aspx

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

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

DAO提供了一种更简单的方式从存储中获取数据,隐藏了丑陋的查询。

存储库也处理数据并隐藏查询等,但是,存储库处理业务/域对象。

存储库将使用DAO从存储中获取数据,并使用该数据恢复业务对象。

例如,一个DAO可以包含一些类似-的方法

 public abstract class MangoDAO{
   abstract List<Mango>> getAllMangoes();
   abstract Mango getMangoByID(long mangoID);
}

一个存储库可以包含一些像这样的方法

   public abstract class MangoRepository{
       MangoDao mangoDao = new MangDao;

       Mango getExportQualityMango(){

       for(Mango mango:mangoDao.getAllMangoes()){
        /*Here some business logics are being applied.*/
        if(mango.isSkinFresh()&&mangoIsLarge(){
           mango.setDetails("It is an export quality mango");
            return mango;
           }
       }
    }
}

这个教程帮助我很容易地理解了主要概念。

存储库是更抽象的面向领域的术语,是领域驱动设计的一部分,它是领域设计的一部分,是一种公共语言,DAO是数据访问技术的技术抽象,存储库只关心管理现有数据和创建数据的工厂。

查看这些链接:

http://warren.mayocchi.com/2006/07/27/repository-or-dao/ http://fabiomaulo.blogspot.com/2009/09/repository-or-dao-repository.html