Presented by Sara Pellegrini
https://sara.event-thinking.io/
Hosted by Avanscoperta
https://www.avanscoperta.it/en
Streaming Video
https://www.youtube.com/watch?v=DhhxKoOpJe0
L'aggregato ha sempre rappresentato per me uno degli elementi più controversi e deboli della teoria del DDD.
La sua definizione risulta a mio parere estremamente vaga:
"L'aggregato é un cluster di oggetti associati che devono essere trattati come una sola unità al fine di modificare i dati."
Tentiamo di chiarire meglio il concetto:
"L'aggregato costituisce l'unitá di consistenza del tuo dominio"
Le cose non migliorano di molto...
Durante la mia attività di consulenza, ho potuto verificare che la piena comprensione del concetto di aggregato rimane ostica per molti.
E' facile commettere errori in fase di modellazione ai quali sará molto complesso porre rimedio in fasi piú avanzate dello sviluppo.
Come conseguenza, una certa percentuale di sviluppatori si convince che l'intera architettura sia eccessivamente difficile e complicata.
Il mio obiettivo é proprio quello di mostrarvi una via alternativa, nella quale l'aggregato non esiste.
5. The aggregate
An aggregate is a cluster of associated objects
that we treat as a unit for the purpose of data changes.
From blue book - Domain-Driven Design: Tackling Complexity in the Heart of Software - Eric Evans
7. From theory to practice
Cluster the ENTITIES and VALUE OBJECTS into AGGREGATES and define
boundaries around each.
Choose one ENTITY to be the root of each AGGREGATE, and control all access to
the objects inside the boundary through the root. Allow external objects to hold
reference to the root only.
From blue book - Domain-Driven Design: Tackling Complexity in the Heart of Software - Eric Evans
8. Event Storming
Education domain
1) A course cannot accept more than N students
2) N, the Course Capacity, can change any time
to any positive integer different from the current one
(even if the number of currently subscribed students is larger than the new value)
10. Story Telling
Storytelling is at the heart of human communication.
Storytelling does not require any special competence/skill.
It helps break down the barriers between technicians and business experts.
11. The aggregate is the boundary of consistency
When a change to any object with the aggregate boundary is committed, all
invariants of the whole aggregate must be satisfied.
From blue book
14. Aggregate does not fit with Story Telling
It is not naturally part of the story: the story teller is forced to add this element
Should business experts understand what a consistency boundary is?
Leads back to the old modelling-first/data centric approach.
Moves the focus from the behavior to the model.
21. Event sourced COURSE aggregate
Course Created Event
Student Subscribed to Course
Event
Student Unsubscribed from
Course Event
Course Capacity Changed
Event
Subscribe Student to Course
Command
Unsubscribe Student to Course
Command
Update Course
Capacity Command
22. Event Storming of this problem:
Education domain
1) A course cannot accept more than N students
2) N, the Course Capacity, can change any time
to any positive integer different from the current one
(even if the number of currently subscribed students is larger than the new value)
3) The course title can change any time
to any title different from the current one
33. Event Storming of this problem:
Education domain
1) A course cannot accept more than N students
2) N, the Course Capacity, can change any time
to any positive integer different from the current one
(even if the number of currently subscribed students is larger than the new value)
3) The course title can change any time
to any title different from the current one
4) The student cannot join more than 10 courses
42. It's just a long transaction...
Long transaction are very common when the business rule spans bounded contexts.
But when the rule spans multiple aggregates in the same bounded context?
Is this a necessary complexity or it can be avoided?
43. ● Does not fit the story telling
● Puts focus on model instead of behaviour
● Mixed technical and business aspects
● Can cause unnecessary contention
● Can cause unnecessary complexity
● Hard to refactor
44. Is there a better way to implement the command model?
46. Focus on the decision
Decision block = Message handler
The message handler knows what it needs to make a decision
47. Event sourcing
Event sourcing provides a huge advantage because it decouples
the persistence from the model needed for taking the decision
The message handler can build on the fly any model needed for
taking the decision starting from the correct event stream
58. Limited contention
Before the contention boundaries were those of the aggregate...
Now, the contention boundaries are those of the stream query
59. Does not seem very artificial?
Student
Subscribed
to Course
Event
Student
Unsubscribed
from Course
Event
Subscribe
Student to
Course
Command
Unsubscribe
Student to
Course
Command
Course Course
Student
Subscribed
to Course
Event
Student
Unsubscribed
from Course
Event
Subscribe
Student to
Course
Command
Unsubscribe
Student to
Course
Command
Student Student
61. From the story-telling perspective
Student
Subscribed
to Course
Event
Student
Unsubscribed
from Course
Event
Subscribe
Student to
Course
Command
Unsubscribe
Student to
Course
Command
Decision Decision
66. Student
Subscribed
to Course
Event
Subscribe
Student to
Course
Command
Decision based on the following business rules:
- A course cannot accept more than N students
- The student cannot join more than 10 courses
Course
Created
Student
Created
….
Course
Renamed
Course
Capacity
Changed
….
Course: jdsj4 Student:gfhl3 ---
Course: jdsj4 Course: jdsj4
---
Pure Events
Tags
(Domain
Identifiers)
Student
Subscribed
to Course
Course: jdsj4
Student: gfh3
69. Domain Identifier
KEY: VALUE
The concept in the domain The unique instance identifier
This is just an example,
They can be anything, as far as they are unique inside the bounded context
70. Student
Subscribed
to Course
Event
Subscribe
Student to
Course
Command
Decision based on the following business rules:
- A course cannot accept more than N students
- The student cannot join more than 10 courses
Course
Created
Student
Created
….
Course
Renamed
Course
Capacity
Changed
….
Course: jdsj4 Student:gfh3j ---
Course: jdsj4 Course: jdsj4
---
Pure Events
Tags
(Domain
Identifiers)
Student
Subscribed
to Course
Course: jdsj4
Student: gfh3j
Course
Fully
Booked
Event
Course
Fully
Booked
Course: jdsj4
72. Pure events
An event does NOT belong to an aggregate
An event is just a description of a fact that is important for the business
An event can be related to one or multiple domain concepts, addressed by their
Domain Identifiers
73. Less complexity
No owner of the event
The business decision can easily involve several domain concepts
The decision is taken in one place
74. Saga
Less synchronization needed
Subscribe Student to
Course Command
Student Subscribed
Course Event
Subscribe Student to
Course Command
Student Subscribed
Course Event
Course
revert
Unsubscribe Student
to Course Command
Student Unsubscribed
Course Event
Student
Subscribe Student to
Course Command
Student
75. More flexibility
Event sourcing provides a huge advantage because it decouples the persistence
from the model needed for taking the decision
Refactoring
● Change the StreamQuery
● Add/Remove Domain Identifiers
76. ● Perfect match with story telling
● Focus on behaviour instead of model
● Less contention
● Less complexity
● Simpler to refactor