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 ...
SOFTWARE INNOVATORS 3
Introduction
SOFTWARE INNOVATORS 4
Introducing me
 Rick van der Arend
 Sogyo Consultancy
▫ Architect / Developer
▫ Consultant
▫ Coach...
SOFTWARE INNOVATORS 5
Definitions
Value Objects
 Many objects are a description of characteristics
 When (one of) the characteristics changes, it
becomes ...
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://e...
Aggregates: definitions
An Aggregate is a cluster of
associated objects that we
treat as a unit for the
purpose of data ch...
Breakdown of this definition
1. A cluster of associated objects
2. That we treat as a unit
3. For the purpose of data chan...
Things to notice
 We‟re talking objects here, not classes
 It‟s for a certain purpose: data changes
▫ This means that fo...
Implementation
 From the cluster, choose a root entity with global
identity – this will be the aggregate root
 Nothing f...
But that sounds a lot like…
 An object
 A component
 A service
 An actor
 An agent
“High internal cohesion, Low exter...
What are the advantages?
 These intra-aggregate links can be modelled as
messages
 A message is to be interpreted by the...
And the alternative?
 You could try to make a fully acid model, living in
/ supported by a relational database
 And use ...
And there’s more
 A model that can deal with inconsistencies is
more robust
 Such a model has lower coupling with extern...
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...
What is a method?
A method is a combination of the following:
1. Checking of input parameters
2. Checking of caller rights...
Option1: Calculation
SOFTWARE INNOVATORS 21
MyCalculatingMethod(param1, param2)
{
return param1 + param2;
}
This could be ...
Option 2: Relaying a call
MyRelayingMethod(param1, param2)
{
field1.HisMethod(param1, param2, field2);
}
SOFTWARE INNOVATO...
Option 3: Setting a field
MySettingValueMethod(param1)
{
field1 = field1.Add(param1);
}
MySettingEntityMethod(param1)
{
fi...
Option 4: Querying
SOFTWARE INNOVATORS 24
MyQueryingMethod()
{
return field1;
}
This could be a static method! (That‟s why...
Single Responsibility Principle
 There is something that is called the Single
Responsibility Principle
 Could this mean ...
Single Responsibility Principle
 There is something that is called the Single
Responsibility Principle
 Could this mean ...
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 col...
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 sup...
Changes are reference re-routes
SOFTWARE INNOVATORS 34
V
V
V
S
V
A field is changed to point to another value that is supp...
Just wondering...
 Wouldn‟t it be possible to make every method
call change only one such references?
 And wouldn‟t it b...
Identifying the event
SOFTWARE INNOVATORS 36
V
VV
S
V
A field is changed to point to another entity that is supplied as pa...
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 „...
 Incoming message queue
 Asynchronous message loop
 Aggregate
What do we have then?
 An actor with a incoming, async m...
 This is a combination of Actors & OO modelling
 The aggregate not only becomes a consistency
boundary, but a concurrenc...
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.softw...
Upcoming SlideShare
Loading in …5
×

Aggregates, Entities and Value objects - Devnology 2010 community day

2,422 views

Published on

Presented at the Devnology 2010 community day - showing some of my thoughts on aggregates, entities, value objects, domain models, methods and actors at the time

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,422
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Aggregates, Entities and Value objects - Devnology 2010 community day

  1. 1. Devnology Community Day Aggregates, Entities and Value objects Rick van der Arend – rvdarend@sogyo.nl
  2. 2. SOFTWARE INNOVATORS 2 Agenda  Introduction  Definitions  … on the nature of Methods  … on the nature of Models  … on the nature of Aggregates  End
  3. 3. SOFTWARE INNOVATORS 3 Introduction
  4. 4. SOFTWARE INNOVATORS 4 Introducing me  Rick van der Arend  Sogyo Consultancy ▫ Architect / Developer ▫ Consultant ▫ Coach / Trainer  Interests: ▫ CQRS / Event Driven ▫ Actor Model ▫ Functional
  5. 5. SOFTWARE INNOVATORS 5 Definitions
  6. 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. 7. Entities  Some objects are not defined by their attributes  They represent a thread of identity SOFTWARE INNOVATORS 7
  8. 8. Aggregates SOFTWARE INNOVATORS 8
  9. 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. 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. 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. 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. 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. 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. 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. 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. 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. 18. SOFTWARE INNOVATORS 18 On the nature of Methods
  19. 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. 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. 21. Option1: Calculation SOFTWARE INNOVATORS 21 MyCalculatingMethod(param1, param2) { return param1 + param2; } This could be a static method!
  22. 22. Option 2: Relaying a call MyRelayingMethod(param1, param2) { field1.HisMethod(param1, param2, field2); } SOFTWARE INNOVATORS 22
  23. 23. Option 3: Setting a field MySettingValueMethod(param1) { field1 = field1.Add(param1); } MySettingEntityMethod(param1) { field1 = param1; } SOFTWARE INNOVATORS 23
  24. 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. 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. 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. 27. SOFTWARE INNOVATORS 27 On the nature of Models
  28. 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. 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. 30. An Aggregate SOFTWARE INNOVATORS 30 V V V V S A couple of objects with high internal cohesion, low external coupling
  31. 31. Changes are reference re-routes SOFTWARE INNOVATORS 31 V V V S V A field is changed to point to another entity
  32. 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. 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. 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. 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. 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. 37. SOFTWARE INNOVATORS 37 On the nature of Aggregates …or should I say Actors?
  38. 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. 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. 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
  41. 41. SOFTWARE INNOVATORS 41 ~ The end ~
  42. 42. SOFTWARE INNOVATORS 42 42 Questions?
  43. 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

×