DDD—基本概念理解

1,581 阅读2分钟

Domain(核心层)

  • Aggregate

一组划定边界,用来表达领域概念的对象集合

比如人包含了姓名、性别、年龄、地址

  • Aggregate Root

对多个aggregate在一个高维度概念的聚合,对外隔离不可见的aggregate,隐藏内部实现。

Assert invariants on the entire aggregate, To fulfil this responsibility, only aggregate roots can be accessed by repositories

对外只提供这一种聚合根,可以通过人来修改其地址,而不能直接修改地址信息

  • Entity

在对象生命周期内,有唯一不变的identity用于区分是否是同一个实体;

比如每个人都有身份证编号,可以做为唯一区分,哪怕你们2个性别、名字都相同,也不能当作同一个人

  • Value Object

无identity标识,无生命周期(无CRUD操作),不可变,仅通过属性值判断是否表达相同的含义;通常是做为Entity或者Service的属性字段使用

比如人的户籍,直接通过省市区这个“具体的值”来判断是否相同

  • Factory

用来创建较为复杂的领域对象,如果简单,可直接用构造方法

  • Repository

负责与数据库的交互,我们他常说的CRUD,也就是我们常用的DAO

  • Service

无状态的,用于协调组织其他领域对象间的业务操作,注意这里不是指本领域对象;如果是调用了infrastructure的相关实现,则应该划分到application/service

Services are often abused in JEE development.Methods that should logically reside in the Entity or Value Object are placed in the service layer. As an example, the calcuateTotalPrice method is erroneously placed in the OrderService object instead of correctly residing in the Order entity itself.

  • Domain Event

用于标记领域对象产生变化的节点事件,比如用户注册完成、用户注销、用户退出登录等

Infrastructure

用于集成三方框架,例如spring、mybatis等;

Application

很轻量级的一层,用于协调domain层对象的使用; 无任何业务逻辑; 实现事务;

User Interfaces

为Application层提供输入、输出支持; 一般来讲是web层,比如常用的controller;

Appendix

DDD: Application File Structure

Domain-Driven Design and Spring