This document summarizes a presentation about aggregates, entities, and value objects. It begins with introductions and definitions of these terms. It then discusses the nature of methods, models, and aggregates. Specifically, it defines aggregates as clusters of associated objects treated as a unit for data changes. It suggests modeling aggregates as actors with an incoming message queue to handle changes asynchronously. This improves concurrency and models aggregates as consistency and concurrency boundaries.
2. SOFTWARE INNOVATORS 2
Agenda
Introduction
Definitions
… on the nature of Methods
… on the nature of Models
… on the nature of Aggregates
End
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
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
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
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)
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
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