Upgrade to Pro — share decks privately, control downloads, hide ads and more …

7th DDD book club-Aggregate & Repository

國昭
July 03, 2019

7th DDD book club-Aggregate & Repository

This slide is talking about what and how in Aggregate of DDD. In this slide, you can understand how to design and model an aggregate.

This slide is published in a Taiwanese social community: DDD.tw.

if you are interested in our social community, you can join us, here is our facebook URL:
https://www.facebook.com/groups/dddtaiwan/

國昭

July 03, 2019
Tweet

More Decks by 國昭

Other Decks in Technology

Transcript

  1. 聚合的概念 “A collection of objects that are bound together by

    a root entity, otherwise known as an aggregate root. The aggregate root guarantees the consistency of changes being made within the aggregate by forbidding external objects from holding references to its members.” -- wikipedia
  2. 衍伸探討 根物件 一致性 防禦性設計 識別碼 不變條件 值物件 建模 尺寸 邊界

    實體 交易 ACID 最終一致性 即時一致性 Unit of Work 聚合參考 事件驅動 CAP 屬性檢查 模型檢查 方法參數檢查 • 以一個Entity作聚合的根物件 • 保障狀態異動的一致性 • 拒絕外部外部直接參考聚合內的成員
  3. 實體(Entity) “An object that is not defined by its attributes,

    but rather by a thread of continuity and its identity.” -- wikipedia • 由識別碼來識別不同的實體(Instance) • 由它的狀態異動歷程來區別
  4. 值物件 “An object that contains attributes but has no conceptual

    identity. They should be treated as immutable.” -- wikipedia • 以所擁有的所有屬性值,作為識別(Conceptual Whole) • 本身具有不可變的特性(Immutable)
  5. 以值對象作為實體識別碼 • Allowing for future changes to the underlying identity

    values without “shotgun surgery”. • Guard against invalid values for identity. • The compiler can help you stop mistakes. • Formatting you Id string for persistent. 參考資料: https://buildplease.com/pages/vo-ids/
  6. 不可變的規則 • 若一個Backlog Item被提交到Sprint中,那麼我們不能刪除該Backlog Item • 若一個Sprint含有Backlog Item,那麼我們不能刪除該Sprint • 若一個Release中含有Backlog

    Item,那麼我們不能刪除該Release • 若有一個Backlog Item位於Release中,那麼我們不能刪除該Backlog Item 利用客戶所提的不可變(Invariant)規則進行建模
  7. 大聚合 Product BacklogItem Release Sprint <<Entity>> <<Entity>> <<Entity>> <<Aggregate Root>>

    1 0…* 0…* 0…* 這個大聚合可能會發生什麼問題?
  8. 小聚合 BacklogItem <<Aggregate Root>> Product Id <<Value Object>> Release <<Aggregate

    Root>> Sprint <<Aggregate Root>> Product <<Aggregate Root>>
  9. 簡單一點的方法- 統一語言 A Car where the encapsulated domain objects might

    be Engine, Wheels, BodyColor and Lights Car Engine Wheel BodyColor Light 先摘出名詞
  10. 簡單一點的方法- 統一語言模型法 A Car where the encapsulated domain objects might

    be Engine, Wheels, BodyColor and Lights Car Engine Wheel BodyColor Light 找出關係
  11. 找小聚合關鍵 BacklogItem <<Aggregate Root>> Product Id <<Value Object>> Release <<Aggregate

    Root>> Sprint <<Aggregate Root>> Product <<Aggregate Root>> • 每個切出去的聚合要確認其包含的屬性是一個交易內要保持 一致性的 • 不要擔心拆太小,因為多聚合的交易一致性可以使用 最終一致性 的方式來達成。 • 可以借用值物件的Conceptual Whole設計手法
  12. 考量的維度 儲存成本 同步操作 衝突 可能性 大聚合, 完整引用另一個 聚合 大聚合, 完整引用另一個

    聚合 小聚合, 完整引用另一個 聚合 小聚合, 僅參考另一個聚合的 識別碼
  13. 資源庫(Repository)的概念 “Methods for retrieving domain objects should delegate to a

    specialized Repository object such that alternative storage implementations may be easily interchanged.” -- wikipedia • 檢索出領域物件(domain object) – 聚合根 • 可適應各種不同儲存的實現 • 可以輕鬆被替換