SlideShare una empresa de Scribd logo
1 de 43
Devnology Community Day
Aggregates, Entities
and Value objects
Rick van der Arend – rvdarend@sogyo.nl
SOFTWARE INNOVATORS 2
Agenda
 Introduction
 Definitions
 … on the nature of Methods
 … on the nature of Models
 … on the nature of Aggregates
 End
SOFTWARE INNOVATORS 3
Introduction
SOFTWARE INNOVATORS 4
Introducing me
 Rick van der Arend
 Sogyo Consultancy
▫ Architect / Developer
▫ Consultant
▫ Coach / Trainer
 Interests:
▫ CQRS / Event Driven
▫ Actor Model
▫ Functional
SOFTWARE INNOVATORS 5
Definitions
Value Objects
 Many objects are a description of characteristics
 When (one of) the characteristics changes, it
becomes another (value) object
SOFTWARE INNOVATORS 6
Entities
 Some objects are not defined by their attributes
 They represent a thread of identity
SOFTWARE INNOVATORS 7
Aggregates
SOFTWARE INNOVATORS 8
Aggregates: definitions
An Aggregate is a collection
of items that are gathered
together to form a total
quantity
http://en.wikipedia.org/wiki/Aggregate
SOFTWARE INNOVATORS 9
Aggregates: definitions
An Aggregate is a cluster of
associated objects that we
treat as a unit for the
purpose of data changes
Domain Driven Design, Eric Evans (DDD-EE)
SOFTWARE INNOVATORS 10
Breakdown of this definition
1. A cluster of associated objects
2. That we treat as a unit
3. For the purpose of data changes
SOFTWARE INNOVATORS 11
C
Things to notice
 We‟re talking objects here, not classes
 It‟s for a certain purpose: data changes
▫ This means that for other purposes (like viewing),
we might not treat them as a unit
 This is all in one context & model
▫ A car engineer will have a very different model of
your car than you will have as a driver
SOFTWARE INNOVATORS 12
Implementation
 From the cluster, choose a root entity with global
identity – this will be the aggregate root
 Nothing from the outside can hold a reference to
anything inside, only to the root
 Inner objects may be handed over transiently
 All objects in the cluster may hold references
 Data changes on the aggregate should be ACID
 The aggregate (root) maintains its invariants
SOFTWARE INNOVATORS 13
But that sounds a lot like…
 An object
 A component
 A service
 An actor
 An agent
“High internal cohesion, Low external coupling”
SOFTWARE INNOVATORS 14
What are the advantages?
 These intra-aggregate links can be modelled as
messages
 A message is to be interpreted by the receiver,
in its own time
This means the receiver:
 Could be switched for another type
 Could be dormant and awakened on receipt
 Could be halfway around the world
SOFTWARE INNOVATORS 15
And the alternative?
 You could try to make a fully acid model, living in
/ supported by a relational database
 And use replication
 The problem is that this model cannot deal with
inconsistencies
 So it can only compromise availability when
there‟s a separation
SOFTWARE INNOVATORS 16
And there’s more
 A model that can deal with inconsistencies is
more robust
 Such a model has lower coupling with external
entities and can be understood more easily
 You are likelier to have a better model that can
deal with changes in the future
SOFTWARE INNOVATORS 17
SOFTWARE INNOVATORS 18
On the nature of Methods
What is a method?
A method is a combination of the following:
1. Checking of input parameters
2. Checking of caller rights
3. Checking of appropriateness of current state
4. Reading parts of the state
5. Performing calculations
6. Setting certain fields to a new value
7. Calling methods on certain fields
8. Returning a value
SOFTWARE INNOVATORS 19
What is a method?
A method is a combination of the following:
1. Checking of input parameters
2. Checking of caller rights
3. Checking of appropriateness of current state
4. Reading parts of the state
5. Performing calculations
6. Setting certain fields to a new value
7. Calling methods on certain fields
8. Returning a value
SOFTWARE INNOVATORS 20
This should be pushed out of the method: by modelling or code contracts
This is a cross cutting concern: use annotations
Tell, don‟t Ask: no can‟t do / deal with it && business rules as annotations
Do you really need this? Remember Tell, don‟t Ask
Ok, but when too complex, move this out to a service
Ok, setting a new state
Ok, relaying the command
Ok, querying
Option1: Calculation
SOFTWARE INNOVATORS 21
MyCalculatingMethod(param1, param2)
{
return param1 + param2;
}
This could be a static method!
Option 2: Relaying a call
MyRelayingMethod(param1, param2)
{
field1.HisMethod(param1, param2, field2);
}
SOFTWARE INNOVATORS 22
Option 3: Setting a field
MySettingValueMethod(param1)
{
field1 = field1.Add(param1);
}
MySettingEntityMethod(param1)
{
field1 = param1;
}
SOFTWARE INNOVATORS 23
Option 4: Querying
SOFTWARE INNOVATORS 24
MyQueryingMethod()
{
return field1;
}
This could be a static method! (That‟s why I said
this could/should be moved out to a service)
Single Responsibility Principle
 There is something that is called the Single
Responsibility Principle
 Could this mean that a method should only do
one of the previous things?
SOFTWARE INNOVATORS 25
Single Responsibility Principle
 There is something that is called the Single
Responsibility Principle
 Could this mean that a method should only do
one of the previous things?
 Well, I think a method can perform multiple
relays, if they combine to form one responsibility
▫ But only if they do this exhaustively
SOFTWARE INNOVATORS 26
SOFTWARE INNOVATORS 27
On the nature of Models
A domain model as a graph
SOFTWARE INNOVATORS 28
V
V
V
V
S
A Value (object)
An Entity
A Static object
(entry point)
A repository
SOFTWARE INNOVATORS 29
S
V
V
V
V
C
An object that has a collection in it, pointing to several entities
(a collection is special, it can add and remove references)
An Aggregate
SOFTWARE INNOVATORS 30
V
V
V
V
S
A couple of objects with high internal cohesion, low external coupling
Changes are reference re-routes
SOFTWARE INNOVATORS 31
V
V
V
S
V
A field is changed to point to another entity
Changes are reference re-routes
SOFTWARE INNOVATORS 32
V
V
V
S
V
A field is changed to point to another value (object)
Changes are reference re-routes
SOFTWARE INNOVATORS 33
V
V
V
S
V
A field is changed to point to another entity that is supplied as parameter
Changes are reference re-routes
SOFTWARE INNOVATORS 34
V
V
V
S
V
A field is changed to point to another value that is supplied as parameter
Just wondering...
 Wouldn‟t it be possible to make every method
call change only one such references?
 And wouldn‟t it be possible to make such a
change an event?
 Then there would be a uniform way of making
model changes into events.
SOFTWARE INNOVATORS 35
Identifying the event
SOFTWARE INNOVATORS 36
V
VV
S
V
A field is changed to point to another entity that is supplied as parameter
Reason
(Command?)
Path
new Value
Source
1
2
Time
Version
SOFTWARE INNOVATORS 37
On the nature of
Aggregates
…or should I say Actors?
 Incoming message queue
 Asynchronous message loop
 All mutable state internally
 All shared state is immutable
 An „independent‟ object
Let’s take an Actor
38SOFTWARE INNOVATORS
Actor
 Incoming message queue
 Asynchronous message loop
 Aggregate
What do we have then?
 An actor with a incoming, async message queue
 And a coherent, self-supporting model in it
And put an aggregate in it
39SOFTWARE INNOVATORS
 This is a combination of Actors & OO modelling
 The aggregate not only becomes a consistency
boundary, but a concurrency boundary too
 This Aggregate-as-an-Actor model is very close
to the one that is normally described in CQRS
 That one is just missing the queue and the loop
The Aggregate-as-an-Actor model
40SOFTWARE INNOVATORS
SOFTWARE INNOVATORS 41
~ The end ~
SOFTWARE INNOVATORS 42
42
Questions?
SOFTWARE INNOVATORS 43
Contact info
Rick van der Arend
rvdarend@sogyo.nl
030 - 220 22 16
Web: www.sogyo.nl
Blog: www.software–innovators.nl

Más contenido relacionado

Destacado

Programa de Inteligencia Sostenible
Programa de Inteligencia SosteniblePrograma de Inteligencia Sostenible
Programa de Inteligencia Sostenible
Grup Pitagora
 

Destacado (19)

Make yourself replaceable at DevOpsCon 2016 Berlin
Make yourself replaceable at DevOpsCon 2016 BerlinMake yourself replaceable at DevOpsCon 2016 Berlin
Make yourself replaceable at DevOpsCon 2016 Berlin
 
SOLID Principles of Refactoring Presentation - Inland Empire User Group
SOLID Principles of Refactoring Presentation - Inland Empire User GroupSOLID Principles of Refactoring Presentation - Inland Empire User Group
SOLID Principles of Refactoring Presentation - Inland Empire User Group
 
#pugMi - DDD - Value objects
#pugMi - DDD - Value objects#pugMi - DDD - Value objects
#pugMi - DDD - Value objects
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
 
Value objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressValue objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progress
 
Introduction to SOLID Principles
Introduction to SOLID PrinciplesIntroduction to SOLID Principles
Introduction to SOLID Principles
 
Refactoring Applications using SOLID Principles
Refactoring Applications using SOLID PrinciplesRefactoring Applications using SOLID Principles
Refactoring Applications using SOLID Principles
 
Success by Challenging Assumptions (Part I)
Success by Challenging Assumptions (Part I)Success by Challenging Assumptions (Part I)
Success by Challenging Assumptions (Part I)
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principles
 
SOLID Principles part 2
SOLID Principles part 2SOLID Principles part 2
SOLID Principles part 2
 
SOLID Principles part 1
SOLID Principles part 1SOLID Principles part 1
SOLID Principles part 1
 
Persisting Value Objects
Persisting Value ObjectsPersisting Value Objects
Persisting Value Objects
 
A System Is Not a Tree
A System Is Not a TreeA System Is Not a Tree
A System Is Not a Tree
 
The SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design PatternsThe SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design Patterns
 
How to prepare for an oral presentation
How to prepare for an oral presentationHow to prepare for an oral presentation
How to prepare for an oral presentation
 
Quiz finals
Quiz finalsQuiz finals
Quiz finals
 
OpenAIRE factsheet: Open Access in Horizon 2020 (for Research Administrators)
OpenAIRE factsheet: Open Access in Horizon 2020 (for Research Administrators)OpenAIRE factsheet: Open Access in Horizon 2020 (for Research Administrators)
OpenAIRE factsheet: Open Access in Horizon 2020 (for Research Administrators)
 
Programa de Inteligencia Sostenible
Programa de Inteligencia SosteniblePrograma de Inteligencia Sostenible
Programa de Inteligencia Sostenible
 
PO Service Provider Profile Compendium
PO Service Provider Profile CompendiumPO Service Provider Profile Compendium
PO Service Provider Profile Compendium
 

Similar a Aggregates, Entities and Value objects - Devnology 2010 community day

C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
Hammad Rajjoub
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The Enterprise
Daniel Egan
 
A Separation of Concerns: Clean Architecture on Android
A Separation of Concerns: Clean Architecture on AndroidA Separation of Concerns: Clean Architecture on Android
A Separation of Concerns: Clean Architecture on Android
Outware Mobile
 
Linq 1224887336792847 9
Linq 1224887336792847 9Linq 1224887336792847 9
Linq 1224887336792847 9
google
 

Similar a Aggregates, Entities and Value objects - Devnology 2010 community day (20)

L05 Design Patterns
L05 Design PatternsL05 Design Patterns
L05 Design Patterns
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The Enterprise
 
Lecture: Refactoring
Lecture: RefactoringLecture: Refactoring
Lecture: Refactoring
 
Exploiting the Data / Code Duality with Dali
Exploiting the Data / Code Duality with DaliExploiting the Data / Code Duality with Dali
Exploiting the Data / Code Duality with Dali
 
Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Combating software entropy 2-roc1-
Combating software entropy 2-roc1-
 
MineDB Mineral Resource Evaluation White Paper
MineDB Mineral Resource Evaluation White PaperMineDB Mineral Resource Evaluation White Paper
MineDB Mineral Resource Evaluation White Paper
 
Evolve Your Code
Evolve Your CodeEvolve Your Code
Evolve Your Code
 
L03 Design Patterns
L03 Design PatternsL03 Design Patterns
L03 Design Patterns
 
A Separation of Concerns: Clean Architecture on Android
A Separation of Concerns: Clean Architecture on AndroidA Separation of Concerns: Clean Architecture on Android
A Separation of Concerns: Clean Architecture on Android
 
Architecture principles in relation to TYPO3
Architecture principles in relation to TYPO3Architecture principles in relation to TYPO3
Architecture principles in relation to TYPO3
 
React gsg presentation with ryan jung & elias malik
React   gsg presentation with ryan jung & elias malikReact   gsg presentation with ryan jung & elias malik
React gsg presentation with ryan jung & elias malik
 
The Clean Architecture
The Clean ArchitectureThe Clean Architecture
The Clean Architecture
 
Dicoding Developer Coaching #31: Android | Menerapkan Clean Architecture di A...
Dicoding Developer Coaching #31: Android | Menerapkan Clean Architecture di A...Dicoding Developer Coaching #31: Android | Menerapkan Clean Architecture di A...
Dicoding Developer Coaching #31: Android | Menerapkan Clean Architecture di A...
 
Linq 1224887336792847 9
Linq 1224887336792847 9Linq 1224887336792847 9
Linq 1224887336792847 9
 
Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2
 
Agile Development in .NET
Agile Development in .NETAgile Development in .NET
Agile Development in .NET
 
solidity programming solidity programming
solidity programming solidity programmingsolidity programming solidity programming
solidity programming solidity programming
 

Aggregates, Entities and Value objects - Devnology 2010 community day

  • 1. Devnology Community Day Aggregates, Entities and Value objects Rick van der Arend – rvdarend@sogyo.nl
  • 2. SOFTWARE INNOVATORS 2 Agenda  Introduction  Definitions  … on the nature of Methods  … on the nature of Models  … on the nature of Aggregates  End
  • 4. SOFTWARE INNOVATORS 4 Introducing me  Rick van der Arend  Sogyo Consultancy ▫ Architect / Developer ▫ Consultant ▫ Coach / Trainer  Interests: ▫ CQRS / Event Driven ▫ Actor Model ▫ Functional
  • 6. Value Objects  Many objects are a description of characteristics  When (one of) the characteristics changes, it becomes another (value) object SOFTWARE INNOVATORS 6
  • 7. Entities  Some objects are not defined by their attributes  They represent a thread of identity SOFTWARE INNOVATORS 7
  • 9. Aggregates: definitions An Aggregate is a collection of items that are gathered together to form a total quantity http://en.wikipedia.org/wiki/Aggregate SOFTWARE INNOVATORS 9
  • 10. Aggregates: definitions An Aggregate is a cluster of associated objects that we treat as a unit for the purpose of data changes Domain Driven Design, Eric Evans (DDD-EE) SOFTWARE INNOVATORS 10
  • 11. Breakdown of this definition 1. A cluster of associated objects 2. That we treat as a unit 3. For the purpose of data changes SOFTWARE INNOVATORS 11 C
  • 12. Things to notice  We‟re talking objects here, not classes  It‟s for a certain purpose: data changes ▫ This means that for other purposes (like viewing), we might not treat them as a unit  This is all in one context & model ▫ A car engineer will have a very different model of your car than you will have as a driver SOFTWARE INNOVATORS 12
  • 13. Implementation  From the cluster, choose a root entity with global identity – this will be the aggregate root  Nothing from the outside can hold a reference to anything inside, only to the root  Inner objects may be handed over transiently  All objects in the cluster may hold references  Data changes on the aggregate should be ACID  The aggregate (root) maintains its invariants SOFTWARE INNOVATORS 13
  • 14. But that sounds a lot like…  An object  A component  A service  An actor  An agent “High internal cohesion, Low external coupling” SOFTWARE INNOVATORS 14
  • 15. What are the advantages?  These intra-aggregate links can be modelled as messages  A message is to be interpreted by the receiver, in its own time This means the receiver:  Could be switched for another type  Could be dormant and awakened on receipt  Could be halfway around the world SOFTWARE INNOVATORS 15
  • 16. And the alternative?  You could try to make a fully acid model, living in / supported by a relational database  And use replication  The problem is that this model cannot deal with inconsistencies  So it can only compromise availability when there‟s a separation SOFTWARE INNOVATORS 16
  • 17. And there’s more  A model that can deal with inconsistencies is more robust  Such a model has lower coupling with external entities and can be understood more easily  You are likelier to have a better model that can deal with changes in the future SOFTWARE INNOVATORS 17
  • 18. SOFTWARE INNOVATORS 18 On the nature of Methods
  • 19. What is a method? A method is a combination of the following: 1. Checking of input parameters 2. Checking of caller rights 3. Checking of appropriateness of current state 4. Reading parts of the state 5. Performing calculations 6. Setting certain fields to a new value 7. Calling methods on certain fields 8. Returning a value SOFTWARE INNOVATORS 19
  • 20. What is a method? A method is a combination of the following: 1. Checking of input parameters 2. Checking of caller rights 3. Checking of appropriateness of current state 4. Reading parts of the state 5. Performing calculations 6. Setting certain fields to a new value 7. Calling methods on certain fields 8. Returning a value SOFTWARE INNOVATORS 20 This should be pushed out of the method: by modelling or code contracts This is a cross cutting concern: use annotations Tell, don‟t Ask: no can‟t do / deal with it && business rules as annotations Do you really need this? Remember Tell, don‟t Ask Ok, but when too complex, move this out to a service Ok, setting a new state Ok, relaying the command Ok, querying
  • 21. Option1: Calculation SOFTWARE INNOVATORS 21 MyCalculatingMethod(param1, param2) { return param1 + param2; } This could be a static method!
  • 22. Option 2: Relaying a call MyRelayingMethod(param1, param2) { field1.HisMethod(param1, param2, field2); } SOFTWARE INNOVATORS 22
  • 23. Option 3: Setting a field MySettingValueMethod(param1) { field1 = field1.Add(param1); } MySettingEntityMethod(param1) { field1 = param1; } SOFTWARE INNOVATORS 23
  • 24. Option 4: Querying SOFTWARE INNOVATORS 24 MyQueryingMethod() { return field1; } This could be a static method! (That‟s why I said this could/should be moved out to a service)
  • 25. Single Responsibility Principle  There is something that is called the Single Responsibility Principle  Could this mean that a method should only do one of the previous things? SOFTWARE INNOVATORS 25
  • 26. Single Responsibility Principle  There is something that is called the Single Responsibility Principle  Could this mean that a method should only do one of the previous things?  Well, I think a method can perform multiple relays, if they combine to form one responsibility ▫ But only if they do this exhaustively SOFTWARE INNOVATORS 26
  • 27. SOFTWARE INNOVATORS 27 On the nature of Models
  • 28. A domain model as a graph SOFTWARE INNOVATORS 28 V V V V S A Value (object) An Entity A Static object (entry point)
  • 29. A repository SOFTWARE INNOVATORS 29 S V V V V C An object that has a collection in it, pointing to several entities (a collection is special, it can add and remove references)
  • 30. An Aggregate SOFTWARE INNOVATORS 30 V V V V S A couple of objects with high internal cohesion, low external coupling
  • 31. Changes are reference re-routes SOFTWARE INNOVATORS 31 V V V S V A field is changed to point to another entity
  • 32. Changes are reference re-routes SOFTWARE INNOVATORS 32 V V V S V A field is changed to point to another value (object)
  • 33. Changes are reference re-routes SOFTWARE INNOVATORS 33 V V V S V A field is changed to point to another entity that is supplied as parameter
  • 34. Changes are reference re-routes SOFTWARE INNOVATORS 34 V V V S V A field is changed to point to another value that is supplied as parameter
  • 35. Just wondering...  Wouldn‟t it be possible to make every method call change only one such references?  And wouldn‟t it be possible to make such a change an event?  Then there would be a uniform way of making model changes into events. SOFTWARE INNOVATORS 35
  • 36. Identifying the event SOFTWARE INNOVATORS 36 V VV S V A field is changed to point to another entity that is supplied as parameter Reason (Command?) Path new Value Source 1 2 Time Version
  • 37. SOFTWARE INNOVATORS 37 On the nature of Aggregates …or should I say Actors?
  • 38.  Incoming message queue  Asynchronous message loop  All mutable state internally  All shared state is immutable  An „independent‟ object Let’s take an Actor 38SOFTWARE INNOVATORS Actor
  • 39.  Incoming message queue  Asynchronous message loop  Aggregate What do we have then?  An actor with a incoming, async message queue  And a coherent, self-supporting model in it And put an aggregate in it 39SOFTWARE INNOVATORS
  • 40.  This is a combination of Actors & OO modelling  The aggregate not only becomes a consistency boundary, but a concurrency boundary too  This Aggregate-as-an-Actor model is very close to the one that is normally described in CQRS  That one is just missing the queue and the loop The Aggregate-as-an-Actor model 40SOFTWARE INNOVATORS
  • 43. SOFTWARE INNOVATORS 43 Contact info Rick van der Arend rvdarend@sogyo.nl 030 - 220 22 16 Web: www.sogyo.nl Blog: www.software–innovators.nl