Professional Documents
Culture Documents
02 DDD
02 DDD
02 DDD
Driven Design
Maurício Aniche
Software Architecture
March/2019
Delft University of Technology
Cool! Let’s start with a cool No-
SQL database, and Redis for
caching, and Kafka for streaming,
and Play! Framework, and REST
APIs, and …
Manufacturing
Billing / Payments
E-learning
”Yet the most significant
complexity of many
applications is not technical. It
is in the domain itself, [...]”
Eric Evans
Tackling complexity in the
heart
of the software
What is a model
?
Both are good
models…
They just model
different things.
You were just
hired
as Software Architect by
AwesomeStuff, B.V.!
AS
challenging .
Let’s talk about four things:
1) We should better understand
the business.
2) Different concepts require
different modelling.
3) We have “multiple domains in
one”.
4) The importance of a common
language between devs and
business experts.
It’s hard to model the “now”.
We don’t see the
action/reaction!
generates
Order Payment
What if we think about
business events?
Order Payment
placed completed
Item
removed
from
Past tense!
inventory
Order Perform
placed Payment
Reactions!
Payment Payment
refused completed
“This happens whenever that happens!”
Reaction! Event!
Order Perform
placed Payment
Payment
External systems!
Gateway
Submit Commands!
order
Order Perform
placed Payment
Payment
Gateway
Event Storming
Figure: https://blog.redelastic.com/corporate-arts-crafts-modelling-reactive-systems-with-event-storming-73c6236f5dd7
“Things happen. Not all of them are
interesting, some may be worth
recording but don’t provoke a
reaction. The most interesting ones
cause a reaction. Many systems
need to react to interesting events.
Often you need to know why a
system reacts in the way it did.”
https://martinfowler.com/eaaDev/DomainEvent.html
“Things happen. Not all of them are
interesting, some may be worth
recording but don’t provoke a
reaction. The most interesting ones
cause a reaction. Many systems
need to react to interesting events.
Often you need to know why a
system reacts in the way it did.”
business term
for that!
Code is more
domain-driven .
User Interface Layer
Application Layer
Domain Layer
Infrastructure Layer
Can you see
advantages (or
disadvantages)
Of a Layered
Architecture?
An isolated domain layer enables us to:
• Better test the model
• Reuse the model
• Reduce problems with external
infrastructure
• Easy to find ”where to code”
• Less expensive to maintain because
they tend to evolve at different paces
and respond to different needs
(Fowler, 1996)
Separate infrastructure from
the domain is the challenge!
(Your framework should help you, and not
distract you from developing a good
model.)
DDD patterns understand that
systems rely on (external)
infrastructure!
Not every element in our
model is the same. Different
elements require different
treatment.
Order has an
identity and should
be the same
throughout the
business life cycle.
Order is an
Entity!
Entities have
actions.
Actions change
the state of the
Entity.
It needs an Identity
• Avoid pointer
comparison
• Is there a true key?
• ISBN for books
• If not, create one.
• When distributing,
make sure the ID is
unique among systems.
You should avoid
anemic models
(behavior and state in different places)
The challenge is to
find the right entity
that the behavior belongs to
Is this an
Entity?
• ”Tracking the identity of ENTITIES is
essential, but attaching identity to other
objects can hurt system performance,
add analytical work, and muddle the
model by making all objects look the
same.”
• ”Software design is a constant battle
with complexity. We must make
distinctions so that special handling is
applied only where necessary.”
This is a
Value Object!
Is Address always a Value
Object?
It depends… ;)
No bi-directional relationship
with VOs and Entities!
1
User Address
Payment#perform(Order) Order#pay()
”When a significant process or
transformation in the domain is
not a natural responsibility of
an ENTITY or VALUE OBJECT,
add an operation to the model
as a standalone interface
declared as a SERVICE. […],
make sure the operation name
is part of the UBIQUITOUS
LANGUAGE.”
• The operation relates to a domain
concept that is not natural part of
an Entity.
• The interface is defined in terms of
other elements of the domain
model.
• The operation is stateless.
This is a PerformPayment
class PerformPayment {
public PerformPayment(AdyenWebService) { /* … */ }
Item Discount
- product - apply()
- quantity
- price
… In complex domains,
Entities have
complex associations
Order
- totalAmount
- amountLimit
Has many Has many
Item Discount
- product - apply()
- quantity
- price
Who makes sure that
… the amountLimit is
respected?
<< aggregate root>>
Its root!
Order
- totalAmount
Any party - amountLimit
Has many Has many
Item Discount
- product - apply()
- quantity
- price
This is an
Aggregate!
• The root has global identity.
Boundaries have local identity.
• Root is ultimately responsible for
checking invariants.
• Nothing outside the Aggregate
boundary can hold a reference to
anything, except to the root.
• Objects within the Aggregate can
hold references to other Aggregate
Roots.
• A delete operation must remove
everything within the Aggregate
boundary at once.
• When a change to any object
within the Aggregate boundary is
committed, all invariants of the
whole Aggregate must be satisfied.
• Avoid bi-directional associations.
• Maurício: They are messy in ORMs
• Impose a traversal direction.
• Remove non-essential associations.
• Remember: the model should not
match reality.
• Constrain relationships as much as
possible.
• Be simple.
We will
eventually need
to persist our
data
Our
database
Order
Repository consumes
Our
- Add(Order) database
- List<Order>
allOrders(Customer)
Customer Customer
Customer
Customer
Multiple models are in play on any large
project. Yet when code based on distinct
models is combined, software becomes
buggy, unreliable, and difficult to
understand. Communication among team
members becomes confused. It is often
unclear in what context a model should
not be applied.
We may have different bounded contexts!
Figure: https://martinfowler.com/bliki/BoundedContext.html
• Explicitly define the context within which a
model applies.
• Explicitly set boundaries in terms of team
organization, usage within specific parts of
the application, and physical
manifestations such as code bases and
database schemas.
• Keep the model strictly consistent within
these bounds, don't be distracted or
confused by issues outside.
How can I integrate
with a (legacy) When a new system is
system that has a being built that must have a
large interface with
bad design model? another, the difficulty of
relating the two models can
eventually overwhelm the
intent of the new model
altogether.
Thanks,
Anti corruption layer!
Your legacy
shall not pass!
Give a decent interface to
your anticorruption layer
so that in the future it can
be simply removed from
the system.
• The book is full of other
patterns, both technical
(Factories, Specifications,
…) and strategical.
• Event-driven architectures
and CQRS are getting
popular.
The “how” is tricky!!
…
Aniche, Maurício. What do we (not) know about DDD? https://codeburst.io/what-do-we-not-know-about-ddd-93727bc5908c, 2017.
Tackling complexity in the
heart
of the software
Further reading
• Alberto Brandolini (inventor of Event Storming):
https://www.youtube.com/watch?v=1i6QYvYhlYQ.
• Microservices or Modules?: https://www.oreilly.com/ideas/modules-vs-
microservices
• Anemic Domain Models:
https://martinfowler.com/bliki/AnemicDomainModel.html.
• Context Mapping: https://www.infoq.com/articles/ddd-contextmapping
• Eric Evans about Microservices:
https://www.youtube.com/watch?v=yPvef9R3k-M.
• Emergent Boundaries, from Mathias Verraes:
https://www.youtube.com/watch?v=ECM1rPYxvD4.
• Towards Modelling Processes, from Mathias Verraes:
http://verraes.net/2015/05/towards-modelling-processes/.
• Modelling reactive systems with event storming:
https://blog.redelastic.com/corporate-arts-crafts-modelling-reactive-
systems-with-event-storming-73c6236f5dd7.
License
• You can use and share any of my material (lecture
slides, website).
• You always have to give credits to the original
author.
• You agree not to sell it or make profit in any way
with this.