cola漫谈
cola是一個(gè)絕佳的DDD領(lǐng)域模式驅(qū)動(dòng)的工程參考。
文章目錄
- 生成第一個(gè)cola工程
- 分層解析
- 實(shí)操
生成第一個(gè)cola工程
mvn archetype:generate \-DgroupId=com.alibaba.cola.demo.web \-DartifactId=demo-web \-Dversion=1.0.0-SNAPSHOT \-Dpackage=com.alibaba.demo \-DarchetypeArtifactId=cola-framework-archetype-web \-DarchetypeGroupId=com.alibaba.cola \-DarchetypeVersion=4.3.1標(biāo)準(zhǔn)的業(yè)務(wù)系統(tǒng)分層大概如下:
分層解析
- adapter:適配層。負(fù)責(zé)前端展示,包括但不限于以下(web,wireless,wap)的路由和適配,相當(dāng)于controller
- application:應(yīng)用層。主要負(fù)責(zé)獲取輸入,組裝上下文,參數(shù)校驗(yàn),調(diào)用領(lǐng)域?qū)幼鰳I(yè)務(wù)處理,如果需要的話,發(fā)送消息通知等。層次是開(kāi)放的,應(yīng)用層也可以繞過(guò)領(lǐng)域?qū)?#xff0c;直接訪問(wèn)基礎(chǔ)實(shí)施層
- domain:領(lǐng)域?qū)印V饕欠庋b了核心業(yè)務(wù)邏輯,并通過(guò)領(lǐng)域服務(wù)(Domain Service)和領(lǐng)域?qū)ο?#xff08;Domain Entity)的方法對(duì)App層提供業(yè)務(wù)實(shí)體和業(yè)務(wù)邏輯計(jì)算。領(lǐng)域是應(yīng)用的核心,不依賴任何其他層次
- infrastructure:基礎(chǔ)實(shí)施層。主要負(fù)責(zé)技術(shù)細(xì)節(jié)問(wèn)題的處理,比如數(shù)據(jù)庫(kù)的CRUD、搜索引擎、文件系統(tǒng)、分布式服務(wù)的RPC等。此外,領(lǐng)域防腐的重任也落在這里,外部依賴需要通過(guò)gateway的轉(zhuǎn)義處理,才能被上面的App層和Domain層使用
打開(kāi)cola包結(jié)構(gòu)如下:
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-6CF9p48o-1677494186762)(C:\Users\Vainycos\AppData\Roaming\Typora\typora-user-images\image-20220831153358888.png)]
對(duì)照上述標(biāo)準(zhǔn)分層,我們進(jìn)行詳細(xì)分析cola的架構(gòu)設(shè)計(jì):
Adapter:
- web:非必選。處理頁(yè)面請(qǐng)求的Controller
- wireless:非必選。處理無(wú)線端的適配
- wap:非必選。處理wap端的適配
App:
- executor:必選。處理request,包括command和query
- consumer:非必選。處理外部message
- scheduler:非必選。處理定時(shí)任務(wù)
Domain:
- model:非必須。領(lǐng)域模型
- ability:非必須。領(lǐng)域能力,包括DomainService
- gateway:必須。領(lǐng)域網(wǎng)關(guān),解耦利器
infrastructure:
- gatewayimpl:必選。網(wǎng)關(guān)實(shí)現(xiàn)
- mapper:非必選。ibatis數(shù)據(jù)庫(kù)映射
- config:非必須。配置信息
Client:
- api:必選。服務(wù)對(duì)外透出的API
- dto:必選。服務(wù)對(duì)外的DTO
實(shí)操
建立一個(gè)學(xué)生管理模型,demo示例包含兩個(gè)方法,分別是新增單個(gè)學(xué)生以及查詢list。
infrastructure層,新建student.DO包,并新建StudentDO數(shù)據(jù)庫(kù)映射實(shí)體類:
@Data public class StudentDO {/*** 自增主鍵id*/private Long id;/*** 姓名*/private String name;/*** 年齡*/private int age; }domain層,
新建student包,并新建Student領(lǐng)域類,作為infra層的領(lǐng)域隔絕,去除id屬性
@Data public class Student {/*** 姓名*/private String name;/*** 年齡*/private int age; }新建StudentGateway接口
public interface StudentGateway {Response add();MultiResponse list(); }在infrastructure層,新建mapper文件以及xml文件
@Mapper public interface StudentMapper {int insert(CustomerDO customerDO);List<StudentDO> selectList(); } <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.alibaba.demo.student.StudentMapper"><resultMap type="com.alibaba.demo.student.DO.StudentDO" id="studentMap"><result property="id" column="id"/><result property="name" column="name"/><result property="age" column="age"/></resultMap><select id="selectList" resultType="com.alibaba.demo.student.DO.StudentDO" resultMap="studentMap">select * from student</select><insert id="insert" parameterType="com.alibaba.demo.student.DO.StudentDO" useGeneratedKeys="true" keyProperty="id">insert into student(name, age)VALUES (#{name},#{age})</insert></mapper>實(shí)現(xiàn)StudentGateway接口,新建StudentGatewayImpl
@Component public class StudentGatewayImpl implements StudentGateway {@Autowiredprivate StudentMapper studentMapper;@Overridepublic Response add(Student student) {StudentDO studentDO = new StudentDO();// 可使用beancopystudentDO.setName(student.getName());studentDO.setAge(student.getAge());studentMapper.insert(studentDO);return Response.buildSuccess();}@Overridepublic MultiResponse list() {return MultiResponse.of(studentMapper.selectList());} }client包新增StudentService以及對(duì)應(yīng)的入?yún)⒑统鰠⒍x
public interface StudentService {Response add();MultiResponse list();}adapter->app->client->domain->infa
參考資料:
- COLA 4.0:應(yīng)用架構(gòu)的最佳實(shí)踐
總結(jié)
- 上一篇: 微信小程序实战--高仿人民日报
- 下一篇: 量化投资学习-13:一张图残酷的展现了庄