Aggregate Rules of Thumb (Aggregate Rules of Thumb)

เรามาพิจารณา four basic rules ของ Aggregate design กันต่อไป:

1. ปกป้อง business invariants ภายใน Aggregate boundaries.

2. ออกแบบเล็กๆ Aggregates.

3. Reference อื่นๆ Aggregates ด้วย identity เท่านั้น

4. Update อื่นๆ Aggregates ใช้ eventual consistency.

แน่นอน rules เหล่านี้ไม่ได้ถูก enforce อย่างเคร่งครัด "DDD police." ใด ๆ พวกมันเป็น sound guidance ดังนั้นเมื่อคิดอย่างรอบคอบ พวกมันจะช่วยคุณออกแบบ Aggregates ที่ทำงานได้อย่างมีประสิทธิภาพ ด้วยเหตุนั้น เรากำลังจะขุดเจาะเข้าไปในแต่ละ rule เหล่านี้เพื่อดู วิธีการที่พวกมันควรจะถูก apply ไปทั่วไป

Image

Rule 1: Protect Business Invariants inside Aggregate Boundaries (Rule 1: ปกป้อง Business Invariants ภายในขอบเขต Aggregate)

Rule 1 หมายความว่า business ควรกำหนด Aggregate compositions ขึ้นมาบน สิ่งที่จะต้องสอดคล้องเมื่อ transaction ถูก commit ตามตัวอย่างบนหน้า 81 , Product ถูกออกแบบให้เมื่อสิ้นสุด transaction ทั้งหมด ProductBacklogItem instances ต้องถูกบัญชี และ สอดคล้องกับ Product root นอกจากนี้ Sprint ถูกออกแบบให้เมื่อสิ้นสุด transaction ทั้งหมด CommittedBacklogItem instances ต้องถูกบัญชี และ สอดคล้องกับ Sprint root

Image

Rule 1 กลายเป็น clearer ด้วย example อื่น นี่คือ BacklogItem Aggregate. มี business rule ว่า "เมื่อทั้งหมด Task instances มี hoursRemaining เป็นศูนย์ BacklogItem status ต้องถูกเซต เป็น DONE ." ดังนั้น ที่ สิ้นสุด transaction นี้ business invariant เฉพาะเจาะจงมากนี้จะต้องถูก meet ธุรกิจต้องการมัน

Image

Rule 2: Design Small Aggregates (Rule 2: ออกแบบเล็กๆ Aggregates)

Rule นี้เน้นว่า memory footprint และ transactional scope ของแต่ละ Aggregate ควรจะเป็นขนาดเล็กๆ ในแผนภาพก่อนหน้านี้ Aggregate ที่แสดงไม่เล็ก ที่นี่ Product ตามจริง มี potentially ที่ very large collection ของ BacklogItem instances ขนาดใหญ่ของ Release instances และขนาดใหญ่ของ Sprint instances เมื่อเวลาผ่านไป collections เหล่านี้อาจเติบโตจนกลายเป็น quite large พร้อมพัน ๆ BacklogItem instances และบางทีหลายร้อย Release และ Sprint instances การออกแบบวิธีการนี้โดยทั่วไป เป็นทางเลือกที่ very poor

Image

อย่างไรก็ตาม ถ้าเราแยก Product Aggregate เพื่อสร้าง Aggregates แยกต่างหากสี่ตัว นี่คือสิ่งที่เราได้: เล็ก Product Aggregate เล็ก BacklogItem Aggregate เล็ก Release Aggregate , และเล็ก Sprint Aggregate. พวกมันโหลดอย่างรวดเร็ว ใช้ memory น้อยกว่า และมีความเร็วมากกว่าสำหรับ garbage collect บางทีที่สำคัญที่สุด Aggregates เหล่านี้จะมี transactional success บ่อยกว่า previous large-cluster Product Aggregate.

ต่อไปนี้ rule นี้มี added benefit ว่าแต่ละ Aggregate จะเป็นเรื่องที่ easier ที่ต้องทำงาน เพราะแต่ละ task ที่เกี่ยวข้องสามารถถูกจัดการ ได้โดยหนึ่ง developer นี่หมายความว่า Aggregate จะเป็นเรื่องที่ easier ที่ต้องทดสอบ

อีกสิ่งหนึ่งที่ต้องจำ เมื่อออกแบบ Aggregates คือ Single Responsibility Principle (SRP) ถ้า Aggregate ของคุณพยายามทำสิ่งต่างๆ มากมายเกินไป มันไม่ได้ตาม SRP และนี่น่าจะบอกอะไรในขนาดของมัน ถามตัวเอง ตัวอย่างเช่น ว่า Product ของคุณเป็น very focused implementation ของ Scrum product หรือ ถ้ามันพยายามจะเป็นสิ่งอื่น ๆ ด้วย เหตุผลที่จะ change Product : เพื่อทำให้มันเป็น better Scrum product หรือ เพื่อ manage backlog items releases และ sprints? คุณควร change Product เพียง เพื่อทำให้มันเป็น better Scrum product

Image

Rule 3: Reference Other Aggregates by Identity Only (Rule 3: Reference อื่น ๆ Aggregates ด้วย Identity Only)

ตอนนี้ที่เราแยก large-cluster Product ออกเป็น four smaller Aggregates , แต่ละอันควรจะ reference อื่น ๆ ที่ต้องการที่ไหน? ที่นี่เราตาม Rule 3 "Reference อื่น ๆ Aggregates ด้วย identity เท่านั้น" ในตัวอย่างนี้เราเห็นว่า BacklogItem , Release , และ Sprint ทั้งหมด reference Product โดยการ hold ProductId นี่ช่วย keep Aggregates เล็ก และ prevents reaching out เพื่อ modify หลายแบบ Aggregates ในหนึ่ง transaction

นี่ช่วยให้ต่อไป keep Aggregate design เล็ก และ efficient ทำให้เกิด lower memory requirements และ quicker loading จาก persistence store นอกจากนี้ มันยังช่วย enforce rule ไม่ modify อื่น ๆ Aggregate instances ภายในหนึ่ง transaction ด้วย identities เพียงของอื่น ๆ Aggregates ไม่มี easy way ที่จะได้รับ direct object reference ไปยังพวกมัน

อีกหนึ่ง benefit ไปยังการใช้ reference ด้วย identity เพียง คือว่า Aggregates ของคุณสามารถถูกเก็บไว้อย่างง่าย ในเกือบ any kind ของ persistence mechanism เช่น relational database document database key-value store และ data grids/fabrics นี่หมายความว่า คุณมี options ที่จะใช้ MySQL relational table JSON-based store เช่น PostgreSQL หรือ MongoDB GemFire/Geode Coherence และ GigaSpaces

Image

Rule 4: Update Other Aggregates Using Eventual Consistency (Rule 4: Update อื่น ๆ Aggregates ใช้ Eventual Consistency)

ที่นี่ BacklogItem ถูก commit ไปยัง Sprint ทั้งคู่ BacklogItem และ Sprint ต้อง react ไปยังสิ่งนี้ มันเป็นครั้งแรกที่ BacklogItem รู้ว่า มันถูก commit ไปยัง Sprint นี่ถูกจัดการใน one transaction เมื่อ state ของ BacklogItem ถูกแก้ไขเพื่อ contain SprintId ของ Sprint ซึ่ง มันถูก committed ไปยัง ดังนั้น เราทำให้แน่ใจได้อย่างไรว่า Sprint ยังถูก update ด้วย BacklogItemId ของ newly committed BacklogItem ?

Image

เป็นส่วนหนึ่งของ BacklogItem Aggregate 's transaction มัน publishes Domain Event ชื่อ BacklogItemCommitted . BacklogItem transaction เสร็จสิ้น และ state ของมัน ถูก persisted พร้อมกับ Backlog-ItemCommitted Domain Event. เมื่อ BacklogItemCommitted ทำวิธีของมันไป local subscriber transaction ถูกเริ่มต้น และ state ของ Sprint ถูกแก้ไขเพื่อ hold BacklogItemId ของ committed BacklogItem . นั่น Sprint hold BacklogItemId ภายใน new CommittedBacklogItem Entity.

Image

เรียกคืน ตอนนี้สิ่งที่คุณเรียนรู้ใน Chapter 4 , " Strategic Design with Context Mapping ." Domain Events ถูก publish โดย Aggregate และ subscribed โดย interested Bounded Context. messaging mechanism นั้น deliver Domain Events ไป interested parties ด้วยวิธีของ subscriptions interested Bounded Context สามารถเป็นอันเดียวกันจากที่ Domain Event ถูก publish หรือ มันอาจจะเป็น different Bounded Contexts.

Image

มันเป็นเพียงแค่ว่าในกรณีของ BacklogItem Aggregate และ Sprint Aggregate , publisher และ subscriber อยู่ใน same Bounded Context. คุณไม่ได้ absolutely ต้องใช้ full-blown messaging middleware product สำหรับ case นี้ แต่มันง่ายในการทำ เพราะคุณแล้ว ใช้มันสำหรับ publishing ไป Bounded Contexts อื่น ๆ


If Eventual Consistency Seems Scary (ถ้า Eventual Consistency ดูเหมือนน่ากลัว)

ไม่มี อะไร incredibly difficult เกี่ยวกับการใช้ eventual consistency มาก อย่างไรก็ตาม จนถึง คุณสามารถได้รับ some experience คุณอาจจะเป็นห่วงเรื่องการใช้มัน ถ้าเป็นเช่นนั้น คุณยังควร partition โมเดลของคุณลงใน Aggregates ตามธุรกิจ-defined transactional boundaries อย่างไรก็ตาม ไม่มี อะไรที่ prevent คุณจาก committing modifications ไป two หรือ more Aggregates ใน single atomic database transaction คุณอาจจะเลือก ใช้ approach นี้ในกรณีที่คุณรู้ว่าจะ succeed แต่ ใช้ eventual consistency สำหรับ ทั้งหมด อื่น ๆ นี่จะอนุญาต คุณในการ get ใช้กับ techniques ไม่ใช้ too big ของ initial step เพียงแค่ เข้าใจว่า นี่ไม่ใช่ primary วิธี ที่ Aggregates นั้น meant ที่จะ ถูกใช้ และ คุณอาจจะ experience transactional failures เป็นผล


Image