Chapter 2. Domains,
Subdomains, and Bounded
Contexts
淺談戰略設計與案例
本章重點
● 了解戰略設計
● 理解以下三個概念
○ Domain
○ Subdomain
○ Bounded Context
● 用一些案例來幫助理解 subdomain 與 bounded context
什麼是 Domain?
● 書本定義:每個組織都有自己獨特的業務範圍與做事方式,而在其中所做的活動
就是 Domain.
● 簡單來說:(公司所遇到的所有) 問題 + 解決方式 = Domain
● Domain Driven Design 也可以把它當作 Business Problem Driven Design
有了 Domain 之後呢?
● domain 容易承載太多含義,可以是整個業務系統、某個 core subdomain或是
supplimantary subdomain.
● 重點在於切分出 subdomain
● 幫我們一次專注在一個特定業務問題
什麼是 Domain Model?
對於某個特定業務領域的軟件模型,通常透過 Model 實現,這些 Model 同時包含數
據與行為能力,並準確表達該有的業務含義
[x] 單一的、內聚的、全功能式的模型
[o] Domain Model 的開發通常是用來專注於整個 business domain 的一個特定區塊
全能的domain model 是困難且易失敗的,對領域的拆解有助於我們理解與開發。
Domain 中存在著兩個空間
Problem Space
● Core Subdomain
● Supporting Subdomain
● Generic Subdomain
Solution Space
Domain
Core
Subdomain
SubdomainSubdomain
Bounded
Context
Bounded
Context
Bounded
Context
Bounded
Context
= UbiquItous Language
Subdomains
● Core subdomain 核心子領域
○ 讓整個系統值得被開發的原因/問題
○ 不能用現成或外包取代
○ 如果 core subdomain 不是整個架構最重要的,那代表你的架構沒有設計好
○ 需要花費最大心力來開發
● Supporting subdomain 支持子領域
○ 非核心,但支援著核心需要的功能
○ 專注於業務的某一方面
○ 視情況可以採用現成工具或外包
● Generic domain 通用子領域
○ 非核心,但被用於整個系統 (類似 infrastructure)
○ 可能也被 supporting subdomain 使用
○ 可外包、用現成套件
○ ex: 身份認證系統 、作業系統
Core Subdomain
Supporting Subdomain Generic Subdomain
Bounded Context
● 語意上的邊界
● Ubiquitous Language 就在這之中
● 讓裡面的模型概念、包含屬性操作,都擁有特殊的含義
● EX: Bank Account v.s. Literary Account
● 還有其他例子嗎?
● 理想:subdomain 與 bounded context 一對一
● 透過 problem space 減少判斷錯誤
● 評估 problem space 與 solution space 來釐清
○ 你的 core subdomain 的名稱與目標
○ 你的 core subdomain 包含哪些概念
○ 這個 core subdomain 的 supporting subdomain 與 generic subdomain 為何
○ 如何在各個子領域安排工作
○ 如何組出一個團隊
Strategic Design 戰略設計
● 理想上,我們會希望建立一個單一強大的 model 完整處理所有功能... but 不可
能! 實際上將之分成多個 Model 會更好開發。
● 戰略設計就是一系列管理與開發多個 domain model 的方法。
● Think in Context
Before DDD
Before DDD
1. 3 個系統,其中2 個內部系統 (bounded context)
2. 承擔太多業務功能
3. 加入新的功能很困難
4. 有些甚至可以外包或向第三方購買
5. 軟體的關注點沒有被釐清
先列出兩個 spaces
Problem Space
● 幫助客人買到想買的東西(商品推薦)
● 庫存管理
● 商品購買
● 權限檢核
● 人力資源管理
● 企業財務管理
Solution Space
● 最優獲取 Context
● 庫存 Context
● 地圖 Context
● 購買 Context
● ERP Context
After DDD
● 找出 Core Subdomain
● 庫存與地圖為不同 Context
試想今天要開發一個協作系統
● 有使用者
● 有論壇 (Forum)
● 可以發起討論 (Discussion)
● 可以貼文 (Post)
● 可以設定日曆 (Calendar)
Context is King
Bounded Context 不僅僅只包含 Model
● 可以是一個系統、一個應用或是一種業務服務
● Database schema
● UI
Smart UI Anit-Pattern
● ...
Bounded Context 的大小
● 不多不少剛剛好
● 迷思
○ 使用技術架構來設定 Bounded Context
○ 根據開發任務的分配來拆分 Bounded Context
● 專注在通用語言上 (Ubiquitous Language)!!
Examples
● 一個幫助團隊進行敏捷開發的軟體
● 可以使用多種記錄與協作工具
● 團隊中各種角色要有不同的功能
Collaboration Context
當身份認證混入商業邏輯中...
● 在論壇開啟一個討論
分組練習
● 五人一組
● 練習畫出一個產品/功能的 Subdomain 與 Bounded Context
● 步驟:
○ 列出 Problem Space 與 Solution Space
○ 將 Problem Space 畫在 Domain 圖上並分出 Core, Supporting 與 Generic
○ 將 Bounded Context 填上去
○ 討論與分享
題目1: Line 聊天功能
● 優勢:貼圖功能:
● 功能:
○ 文字訊息
○ 圖片訊息
○ 好友管理
○ 記事本
○ ...
參考文獻:Line 的使用者行為分析與研究
題目2: Netflix
● 優勢:推薦功能
● 功能:
○ 影片串流播放
○ 影片資訊管理
○ 會員與方案管理
○ 付款功能
Domain 圖
Reference
● 3. DDD Strategic Design in under 15 minutes
● Martin Fowler - Bounded Context
●
Before DDD
1. 3 個系統,其中2 個內部系統 (bounded context)
2. 承擔太多業務功能
3. 加入新的功能很困難
4. 有些甚至可以外包或向第三方購買
5. 軟體的關注點沒有被釐清
先列出兩個 spaces
Problem Space
● 幫助客人買到想買的東西(商品推薦)
● 庫存管理
● 商品購買
● 權限檢核
● 人力資源管理
● 企業財務管理
Solution Space
● 最優獲取 Context
● 庫存 Context
● 地圖 Context
● 購買 Context
● ERP Context
After DDD
● 找出 Core Subdomain
● 庫存與地圖為不同 Context
試想今天要開發一個協作系統
● 有使用者
● 有論壇 (Forum)
● 可以發起討論 (Discussion)
● 可以貼文 (Post)
● 可以設定日曆 (Calendar)
Context is King
Bounded Context 不僅僅只包含 Model
● 可以是一個系統、一個應用或是一種業務服務
● Database schema
● UI
Smart UI Anit-Pattern
● ...
Bounded Context 的大小
● 不多不少剛剛好
● 迷思
○ 使用技術架構來設定 Bounded Context
○ 根據開發任務的分配來拆分 Bounded Context
● 專注在通用語言上 (Ubiquitous Language)!!
Examples
● 一個幫助團隊進行敏捷開發的軟體
● 可以使用多種記錄與協作工具
● 團隊中各種角色要有不同的功能
Collaboration Context
當身份認證混入商業邏輯中...
● 在論壇開啟一個討論
分組練習
● 五人一組
● 練習畫出一個產品/功能的 Subdomain 與 Bounded Context
● 步驟:
○ 列出 Problem Space 與 Solution Space
○ 將 Problem Space 畫在 Domain 圖上並分出 Core, Supporting 與 Generic
○ 將 Bounded Context 填上去
○ 討論與分享
題目1: Line 聊天功能
● 優勢:貼圖功能:
● 功能:
○ 文字訊息
○ 圖片訊息
○ 好友管理
○ 記事本
○ ...
參考文獻:Line 的使用者行為分析與研究
題目2: Netflix
● 優勢:推薦功能
● 功能:
○ 影片串流播放
○ 影片資訊管理
○ 會員與方案管理
○ 付款功能
Domain 圖
Reference
● 3. DDD Strategic Design in under 15 minutes
● Martin Fowler - Bounded Context
●

2019-03-13-ddd taiwan-community-iddd-studygroup-2nd