Modeling Aggregates (การสร้างโมเดล Aggregates)
มี hooks ไม่กี่อันที่รอคุณ เมื่อคุณทำงานบน domain model ของคุณ implementing Aggregates. อันหนึ่ง big nasty hook คือ Anemic Domain Model [IDDD] นี่คือ ที่ คุณกำลังใช้ object-oriented domain model และ ทั้งหมดของ Aggregates ของคุณ มี เพียงแค่ public accessors (getters และ setters) แต่ ไม่มี real business behavior นี้ มีแนวโน้ม เกิดขึ้น เมื่อ มี technical rather than business focus ระหว่าง modeling Designing Anemic Domain Model ต้องการให้คุณ take บน ทั้งหมด overhead ของ domain model โดยไม่ realize any ของ benefits ของมัน อย่าไป ต่อมัน!
นอกจากนี้ watch out สำหรับ leaking business logic ลงใน Application Services ด้านบนของ domain model ของคุณ มันสามารถ happen undetected เช่น physical anemia Delegating business logic จาก services ไป helper/utility classes ไม่ใช่ ไปทำงานออก well ด้วย Service utilities เสมอ exhibit ของ identity crisis และ สามารถ never keep stories ของพวกมัน straight Place business logic ของคุณ ใน domain model ของคุณ หรือ suffer bugs sponsored โดย Anemic Domain Model.
What about Functional Programming? (เกี่ยวกับ Functional Programming ล่ะ?)
เมื่อใช้ functional programming rules เปลี่ยน considerably ในขณะที่ an Anemic Domain Model ไม่ใช่ดี idea เมื่อใช้ object-oriented programming มันเป็นบาง what ของ norm เมื่อ applying functional programming นี่เพราะว่า functional programming promotes ของ separation ของ data และ behavior ของคุณ data ถูกออกแบบเป็น immutable data structures หรือ record types และ behavior ของคุณ ถูก implemented เป็น pure functions ที่ operate บน immutable records ของ specific types rather than modifying ของ data ที่ functions ได้รับเป็น arguments functions return new values new values เหล่านี้ อาจจะ new state ของ Aggregate หรือ Domain Event ที่ represents transition ใน Aggregate 's state
ผมได้ largely addressed ของ object-oriented approach ใน chapter นี้ เพราะว่า มัน still ที่สุด widely used และ well understood อย่างไรก็ตาม ถ้าคุณ employing functional language และ approach ไป DDD be aware ว่า some ของ guidance นี้ ไม่ใช่ applicable หรือ at least subject ไป overriding rules
Next ผมกำลังจะ show คุณ some ของ technical components คุณจะ need ไปใช้ basic Aggregate design ผมสมมติ คุณกำลังใช้ Scala C# Java หรือ another object-oriented programming language ต่อไปต่อมานี้ examples อยู่ใน C# แต่ อยู่ very understandable โดย Scala F# Java Ruby Python และ other programmers เหมือนกัน
first thing คุณต้อง do คือ create class สำหรับ Aggregate Root Entity. นี่คือ UML (Unified Modeling Language) representation ของ Product Root Entity. Included เป็น also ของ Product class ใน C# ซึ่ง extends base class ชื่อ Entity base class นี้ just takes care ของ standard Entity kinds ของ things ดู Implementing Domain-Driven Design [IDDD] สำหรับ exhaustive discussions บน ทั้ง Entity และ Aggregate design และ implementation
every Aggregate Root Entity ต้อง have globally unique identity Product ใน Agile Project Management Context actually have two forms ของ globally unique identity TenantId scopes ของ Root Entity ข้างใน given subscriber organization (every organization ที่ subscribes ไป offered services เป็น known เป็น tenant และ thus have unique identity สำหรับ that) second identity ซึ่ง เป็น also globally unique คือ ProductId second identity นี้ sets Product apart จาก ทั้งหมด others ภายในใน same tenant นอกจากนี้ included คือ C# code ที่ declares two identities ข้างใน Product
Use of Value Objects (การใช้ Value Objects)
นี่ ทั้ง TenantId และ ProductId ถูกสร้างโมเดล เป็น immutable Value Objects.
next คุณ capture any intrinsic attributes หรือ fields ที่ necessary สำหรับ finding Aggregate. ใน case ของ Product มี ทั้ง description และ name users สามารถ search one หรือ ทั้ง สอง ของ เหล่านี้ ไปหา each Product ผมยัง provide C# code ที่ declares two intrinsic attributes เหล่านี้
ของ course คุณสามารถ add simple behavior เช่น read accessors (getters) สำหรับ intrinsic attributes ใน C# นี่น่าจะเป็น done ใช้ public property getters อย่างไรก็ตาม คุณ may ไม่ want ไปexpose setters เป็น public โดยไม่ public setters วิธีการ do property/attribute values change? เมื่อใช้ object-oriented approach (C# Scala และ Java) คุณ change internal state ใช้ behavioral methods ถ้าใช้ functional approach (F# Scala และ Clojure) functions จะ return new values ที่ different จาก values ผ่าน เป็น arguments
คุณควร be บน mission ไปต่อสู้ out Anemic Domain Model [IDDD] ถ้าคุณ expose public setter methods มันอาจจะ quickly lead ไป anemia เพราะว่า logic สำหรับ setting values บน Product น่าจะถูก implemented นอก model Think hard before doing นี้ และ keep warning นี้ใน mind ของคุณ
finally คุณ add in any complex behavior นี่เราได้ four new methods: PlanBacklogItem() , PlannedProductBacklogItem() , ScheduleRelease() , และ ScheduleSprint() C# code สำหรับ แต่ละ ของ methods เหล่านี้ควร be added ไป class
Remember เมื่อใช้ DDD เราก็ always ของ modeling Ubiquitous Language ข้างใน Bounded Context. Thus ทั้งหมด parts ของ Product Aggregate ถูก modeled per Ubiquitous Language. คุณ don't just make up these composed parts everything แสดง harmony ระหว่าง Domain Experts และ developers ของ close-knit team ของคุณ
Choose Your Abstractions Carefully (เลือก Abstractions ของคุณ อย่างระมัดระวัง)
effective software model เสมอ ขึ้น อยู่กับ set ของ abstractions ที่ address ของ business way ของ doing things มี อย่างไรก็ตาม ของ need ไป choose appropriate level ของ abstraction สำหรับ แต่ละ concept ถูก modeled
ถ้าคุณ follow ของ direction ของ Ubiquitous Language คุณ จะ generally create ของ proper abstractions มัน's much easier ไป model ของ abstractions อย่างถูกต้อง เพราะว่า มัน คือ Domain Experts ที่ convey at least ของ genesis ของ modeling language ของคุณ still บางทีครั้ง software developers ที่ overzealous สำหรับ solving ของ wrong problems จะ try ไป force ใน abstractions ที่ are well too abstract
ตัวอย่าง ใน Agile Project Management Context เรา're dealing กับ Scrum มัน makes sense ไป model Product , BacklogItem , Release , และ Sprint concepts ว่า we've been discussing even so what ถ้า software developers อยู่ less concerned about modeling ของ Ubiquitous Language ของ Scrum และ more interested ใน modeling solution ไป ทั้งหมด current และ future Scrum concepts?
ถ้า angle นี้ were pursued developers จะ probably come up กับ abstractions เช่น ScrumElement และ ScrumElementContainer ScrumElement สามารถ fill ของ current need สำหรับ Product และ Backlog-Item , และ ScrumElementContainer สามารถ represent ของ obviously more explicit concepts ของ Release และ Sprint ScrumElement จะ have typeName property และ มัน จะ be set ไป "Product" หรือ "BacklogItem" ใน appropriate cases เราสามารถ design ของ same kind ของ typeName property สำหรับ ScrumElementContainer และ allow ของ values "Release" หรือ "Sprint" ไป be set บน มัน
คุณ see ของ problems กับ approach นี้? มี more than few แต่ consider ของ following:
• ของ language ของ software model ไม่ match ของ mental model ของ Domain Experts.
• ของ level ของ abstraction นั้น too high และ คุณ จะ get ลงไป deep trouble เมื่อ คุณ start ไป model ของ details ของ each ของ individual types
• นี่ จะ lead ไป creating special cases ใน each ของ classes และ likely result ใน complex class hierarchy กับ general approaches ไป explicit problems
• คุณ จะ have much more code than คุณ need เพราะว่า คุณ are trying ไป solve unsolvable problem ที่ should ไม่ matter ใน first place
• often ของ language ของ wrong abstractions จะ find way ของมัน even ลงไป user interface ซึ่ง จะ cause confusion สำหรับ users
• คุณ จะ waste considerable time และ money
• คุณ จะ never be able ไป address ทั้งหมด future needs up front ซึ่ง means ถ้า new Scrum concepts ever added ใน future ของ existing model ของคุณ จะ prove ไป be failure ใน foreseeing those needs
following path นั้น may seem strange ไป some แต่ นี่ incorrect level ของ abstractions นั้น used often ใน technically-inspired implementations
don't get taken ใน โดย นี่ alluring highly abstract implementation trap model Ubiquitous Language explicitly according ไป ของ mental model ของ Domain Experts ที่ is refined โดย team ของคุณ โดย modeling สิ่ง ที่ ของ business needs today คุณ จะ save considerable ของ amount ของ time budget code และ embarrassment more still คุณ จะ do ของ business great service โดย modeling accurate และ useful Bounded Context ที่ reflects effective design
Right-Sizing Aggregates (Right-Sizing Aggregates)
คุณ may be wondering how you สามารถ determine ของ boundaries ของ Aggregates และ prevent ของ design ของ large-cluster แต่ still maintain ของ consistency boundaries ที่ จะ protect true business invariants นี่ผม have provided worthy design approach ถ้าคุณ have already created large-cluster Aggregates คุณสามารถ use นี่ approach ไป refactor ลงไป smaller ones แต่ผม am ไม่ going ไป start จาก ว่า perspective
Consider นี้ design steps ที่ จะ help คุณ reach ของ consistency boundary goals:
1. Put ของ first focus บน ของ second rule ของ Aggregate design "Design small Aggregates. " start โดย creating every Aggregate กับ just one Entity ซึ่ง จะ serve เป็น Aggregate Root. don't even dare ไป place สอง Entities ใน single boundary ว่า opportunity จะ come soon enough populate each ของ Entities กับ ของ fields/attributes/properties ที่ คุณ believe อยู่ most closely associated กับ ของ single Root Entity. one big hint นี่ เพื่อ define every field/attribute/property ที่ required ไป identify และ find ของ Aggregate , เป็น well เป็น any additional intrinsic fields/attributes/properties ที่ required สำหรับ Aggregate ไป be constructed และ left ใน valid initial state
2. now place ของ focus ของคุณ บน ของ first rule ของ Aggregate design "Protect business invariants ข้างใน Aggregate boundaries" คุณ have already asserted โดย ของ previous step ที่ at minimum ทั้งหมด ของ intrinsic fields/attributes must be up-to-date เมื่อ ของ single- Entity Aggregate ถูก persisted แต่ now คุณ need ไป look at each ของ Aggregates ของคุณ one at a time เช่นเดียวกับ คุณ do so สำหรับ Aggregate A1 ask ของ Domain Experts ถ้า any อื่น ๆ Aggregates คุณ have defined must be updated ใน reaction ไป changes made ไป Aggregate A1 make a list ของ each ของ Aggregates และ ของ consistency rules ของพวกมัน ซึ่ง จะ indicate ของ time frames สำหรับ ทั้งหมด reaction-based updates ใน other words Aggregate A1" น่าจะ be ของ heading ของ one list และ อื่น ๆ Aggregate types น่าจะ be listed ข้างใต้ A1 ถ้า พวกมัน จะ be updated ใน reaction ไป A1 updates
3. now ask ของ Domain Experts how much time may elapse จนกว่า each ของ ของ reaction-based updates may take place นี่ จะ lead ไป two kinds ของ specifications: (a) immediately และ (b) ภายใน N seconds/minutes/hours/days one possible way ไป find ของ correct business threshold คือ โดย presenting เป็น exaggerated time frame (เช่น weeks หรือ months) ที่ is obviously unacceptable นี่ จะ likely cause business experts ไป respond กับ acceptable time frame
4. สำหรับ each ของ ของ immediate time frames (3a) คุณ should strongly consider composing those สอง Entities ภายใน same Aggregate boundary ว่า means สำหรับ example ว่า Aggregate A1 และ Aggregate A2 จะ actually be composed ลงไป new Aggregate A[1,2] now Aggregates A1 และ A2 เช่นเดียวกับ พวกมัน were previously defined จะ ไม่อีก exist มี เพียงแค่ Aggregate A[1,2]
5. สำหรับ each ของ ของ reacting Aggregates ที่ สามารถ be updated following a given elapsed time (3b) คุณ จะ update these ใช้ ของ fourth rule ของ Aggregate design "Update อื่น ๆ Aggregates ใช้ eventual consistency"
ใน figure นี้ ของ focus ของเรา อยู่ บน modeling Aggregate A1 note จาก ของ A1 list ของ consistency rules ว่า A2 has immediate time frame ในขณะที่ C14 has eventual (30 seconds) time frame ผลจาก นี้ A1 และ A2 ถูก modeled ลงไป single Aggregate A[1,2] during runtime Aggregate A[1,2] publishes Domain Event ที่ causes Aggregate C14 ไป be updated eventually
be careful ว่า ของ business doesn't insist ว่า every Aggregate fall ภายใน ของ 3a specification (immediate consistency) มันสามารถ be especially strong tendency เมื่อ many ใน ของ design session อยู่ influenced โดย database design และ data modeling those stakeholders จะ have very transaction-centered point ของ view อย่างไรก็ตาม มันเป็น very unlikely ว่า ของ business really needs immediate consistency ใน every case ไป change นี่ thinking คุณ น่าจะ have ไป spend time proving how transactions จะ fail due ไป concurrent updates โดย multiple users across different composed parts ของ (now) large-cluster Aggregates. furthermore คุณสามารถ point out how much memory overhead there is กับ such large-cluster designs obviously these kinds ของ problems อยู่ ว่า we're trying ไป avoid ใน first place
นี้ exercise indicates ว่า eventual consistency is business driven ไม่ technically driven ของ course คุณ จะ have ไป find way ไป technically cause eventual updates ระหว่าง multiple Aggregates , เช่นเดียวกับ discussed ใน ของ previous chapter บน Context Mapping. even so มันเป็น เพียงแค่ ของ business ที่สามารถ determine ของ acceptable time frame สำหรับ updates ไป occur ระหว่าง various Entities. some อยู่ immediate หรือ transactional ซึ่ง means พวกมัน must be managed โดย ของ same Aggregate. some อยู่ eventual ซึ่ง means พวกมัน may be managed through Domain Events และ messaging สำหรับ example considering ว่า ของ business น่าจะ have ไป do ถ้า มันเป็น run operations เพียง โดย means ของ paper systems สามารถ provide some worthwhile insights ลงไป how various domain-driven operations should work ภายใน software model ของ ของ business operations
Testable Units (Testable Units)
คุณ should also design ของ Aggregates ของคุณ ไป be sound encapsulation สำหรับ unit testing complex Aggregates อยู่ hard ไป test following ของ previous design guidance จะ help คุณ model testable Aggregates.
unit testing is different จาก validating business specifications (acceptance tests) เช่นเดียวกับ discussed ใน Chapter 2 , " Strategic Design with Bounded Contexts and the Ubiquitous Language ," และ Chapter 7 , " Acceleration and Management Tools ." development ของ ของ unit tests จะ follow ของ creation ของ scenario specification acceptance tests what we're concerned กับ นี่ คือ testing ว่า Aggregate correctly does ว่า it's supposed ไป do คุณ want ไป push บน ทั้งหมด ของ operations ไป ensure ของ correctness quality และ stability ของ Aggregates. คุณสามารถ use unit testing framework สำหรับ นี้ และ there's much literature available บน how ไป effectively unit test these unit tests จะ be directly associated กับ Bounded Context ของคุณ และ kept กับ ของ source code repository ของมัน