美文网首页
DAO and Repository 2023-04-25

DAO and Repository 2023-04-25

作者: 9_SooHyun | 来源:发表于2023-04-24 12:05 被阅读0次

A Repository can cover a Dao, since it allows you to access/persist data, but the repository has a more precise definition based on simulating interaction with a collection of data.

same

Object-oriented applications that access a database, must have some logic to handle database access. In order to keep the code clean and modular, it is recommended that database access logic should be isolated into a separate module. In layered architecture, this module is Data Access Layer (DAL).
数据库交互的逻辑必须独立出来,无需业务层关心细节,保持app架构清晰简洁

DAO and Repository pattern are both ways of implementing DAL. So, let's start with DAL, first.
DAO and Repository是DAL的两种具体实现

DAO pattern is a way of generating DAL, where typically, each domain entity has its own DAO. For example, User and UserDao, Appointment and AppointmentDao, etc.
An example of DAO with Hibernate: http://gochev.blogspot.ca/2009/08/hibernate-generic-dao.html.

Like DAO, Repository pattern is also a way achieving DAL. The main point in Repository pattern is that, from the client/user perspective, it should look or behave as a collection.
Suppose you're modeling a pet shop and you have a table PetType with different animals and their attributes (name: "Cat", type: "Mammal", etc.) referenced by a table Pet of the concrete pets you have in the shop (name: "Katniss", breed: "Calico", etc.). If you want to add an animal of a type not already in the database, you could use a repository to group the two separate DAO calls (one to create PetType and the other for the Pet) in one method, avoiding coupling in DAOs

difference

abstraction

DAO is an abstraction of data persistence.
Repository is an abstraction of a collection of objects.

position

DAO would be considered closer to the database, often table-centric.
Repository would be considered closer to the business domain, dealing only in Aggregate Roots.
this is like what described above

operating value

A DAO returns data by its very definition (a data access object).
A Repository, by its definition, returns domain aggregated objects.

Repository could be implemented using DAO's, but you wouldn't do the opposite. It should be simply a collection of objects, with a Get(id), Find(ISpecification), Add(Entity).

Remember, a DAO will be a per-table object, whereas a Repository will almost always have to use multiple DAO's to build some aggregated objects. If you find that is not the case, that your Repository and Entity only need to access a single table, then you are most likely building an anemic domain.

refer https://stackoverflow.com/questions/8550124/what-is-the-difference-between-dao-and-repository-patterns

总结

DAO就是单纯的db操作封装,与业务逻辑无关。通常一个db table对应一个dao object
Repository是带有业务逻辑的数据操作封装,与业务逻辑强相关,是DDD中提出的概念。通常一个repo暴露的单个method就涉及多方数据的操作

相关文章

  • @Repository、@Service、@Controller

    @Repository注解用于标注数据访问组件,即实现Dao层的访问。具体使用是将“@Repository”放于想...

  • @Service("AAA")@Component

    @Component – 指示自动扫描组件。@Repository – 表示在持久层DAO组件。@Service ...

  • @Mapper和@Repository的区别

    @Repository需要在Spring中配置扫描地址( @MapperScan("com.anson.dao"...

  • Spring Bean管理(注解方式)

    使用注解定义Bean @Component 描述Spring框架中Bean @Repository 用于对DAO实...

  • java

    dao层:必须使用@Repository注解 注解 含义 @Component 最普通的组件,可以被注入到...

  • 2020-04-27

    Spring Bean 的装配方式@Repository:用于对 DAO 的实现类进行注解;@Service:用于...

  • Spring注解的作用

    @Repository 通过Spring注解定义一个Dao @Autowired 可以对成员变量、方法和构造...

  • Unitils H2 测试基础设施

    1. 为什么使用Unitils H2 1.1 DAO(REPOSITORY) 层的业务需要做测试,可以直接测试出S...

  • SpringDataJpa复杂查询-动态查询条件

    Jpa的Dao层只继承Repository做复杂查询,不能做到不传某不一个条件,where子句根据条件是否为空动态...

  • 18.Spring的数据访问

    1.数据访问对象DAO或者Repository组件为了避免持久化的逻辑分散到应用中的各个组件中,最好是将数据访问的...

网友评论

      本文标题:DAO and Repository 2023-04-25

      本文链接:https://www.haomeiwen.com/subject/qieujdtx.html