---
# System prepended metadata

title: 用模式語言解壓縮 CQRS：以 ezKanban 系統為例

---

# 用模式語言解壓縮 CQRS：以 ezKanban 系統為例

## ezKanban 簡介

### background

- 18年- now: 跑Scrum
- 2020/6 - now:  mobbing 

### 使用技術

DDD + Event Storming + Clean Architecture + CQRS + Even Sourcing + State Sourcing + DCI + BDD/SBE + DBC

> 像養蠱一樣，如何保持兼容不衝突

## kanban

vertical: stage
horizon: swimlane

Core domain: 看板

## why CQRS

- 速度
    - 很多人同時共用/協作
    - cache同步問題：每個人的狀態都不同
- It is important to constrain relationships as much as possible. A bidirectional association means that both objects can be understood only together. **When application requirements do not call for traversal in both directions, adding a traversal direction reduces interdependence and simplified**

## 聚合

- Board
- workflow
- tag
- card

同時多人移動卡片
- problem space: 可以
- solution space: ?? (depend on 如何切aggregate)
- 

aggregate: transaction boundary
    - 儲存單位為aggregate

問題
- board 裡有很多workflow (從workflowRepository get foreach)
- workflow裡有很多card (從cardRepository get foreach)
- 再join成 view model (dto)傳給前端

## Event handler 

eventually consistancy

不好做

如何保證一定送達：至少送達一次

DDD 的domain model同時負擔2個責任：read & write

idempotent

為什麼要讀寫分離：
- 可幫write做最佳化
- 可幫read做最佳化
- 可對read scaling

Write需要Read情境: 寫入資料前需要查詢資料進行檢核
Read需要Write情境: projector

CA: Screaming Architecture

CA 用在 Read Model 沒問題
ddd 用在  Write Model 合適 

write儘量維護one way, read就可以any way

denomalized eventually consistancy

## pattern language

- used to solve a complex problem
- a directed acyclic graph with a single root node as its goal
- a pattern defines the context for the patterns directly beneath it
- a pattern supports the pattern abouve it

## 有向圖

根節點：pattern language 要解決的目標

[Get your hands dirty in CA](https://www.amazon.com/Hands-Dirty-Clean-Architecture-hands/dp/1839211962)

## Event sourcing

特性：
- 寫入很快
- 查詢很慢

## model

read model: 一個畫面一個, case by case
write model: per aggregate

## CQRS在read裡的write會遇到的問題

- duplication 取代 dependency
- reference

## exceptional handing

- happy path
- exceptional path

會相互有關係

## 結論：套用pl解決DDD + CA + CQRS 的模型衝突問題



## others

> 對舊的東西太熟會傾向排斥新的東西，要放下成見去了解
> e.g.對物件導向太熟，覺得ddd只是換句話說












