Professional Documents
Culture Documents
DESIGN
A pragmatic approach
EBOOK
by Eduard Ghergu
INTRODUCTION ........................................................... 04
01
WHY DOMAIN-DRIVEN DESIGN? ..................................................... 05
02
WHAT IS DOMAIN-DRIVEN DESIGN? ............................................... 08
03 12
04
Ubiquitous Language ....................................................................................................
20
22
05 24
06
Introduction .................................................................................................................
30
33
07
CONCLUSIONS ................................................................................ 38
08
REFERENCES ................................................................................... 40
FIGURES
Domain-Driven Design 04
Why Domain-Driven
. Design?
Because we need a way to eliminate
the following:
Domain-Driven Design 06
As long the application logic is implemented
in more than one functional component
(a violation of the Single Responsibility
Principle - SRP [1]), any change to the
functionality is risky without a clear
5 understanding of the business requirement.
Hard-to-fix, time- Also, if the model that is going to be changed
consuming bugs is used by many different modules (for
example the same Customer is used in
Orders, Payments, Shipping, etc.), any new
requested functionality for a module will
have a direct impact on the others sharing
the same model or functionality.
Domain-Driven Design 07
What is
Domain-Driven
. Design?
Domain-Driven Design 08
It is a design methodology defined by Eric Evans in his seminal book,
Domain-Driven Design: Tackling Complexity in the Heart of Software
(Addison-Wesley Professional, 2003). This approach to software
development enables teams to effectively manage software construction
and maintenance for complex problem domains. It also emphasizes the
need to understand and match the business domain for the application
that is going to be implemented.
However, despite its obvious strong advantages, it’s not a silver bullet,
and it cannot be used everywhere or under any condition. It is, mainly,
a set of recommendations, best practices, building blocks, and patterns
that can be used to overcome some of the development problems that
usually arise in application construction.
Domain-Driven Design 09
Consider an ERP system: how can we model and implement such a
system as best as possible? We may have several components that
collaborate, such as customers, production, warehousing, and a variety
of other concerns that we must deal with in some way. As a result, the
goal is to break things down into smaller “pieces” and treat them as
independently as possible. However, not all sub-domains are equally
important; for the important ones (the core), it is necessary to invest
more time and effort to understand them correctly. The others are just
details, and they will not necessarily be implemented by us. In this case,
it will be sufficient for a sample to acquire a solution that already exists
on the market that can solve a smaller problem or a problem that is not
very important for the core of the business or for the core of the system
we are dealing with. The core, which is the main area of the business, is
what differentiates the company from the competition. Without it, the
company will be pushed out of the market.
If we take a step back and think from the developer perspective, the
team usually includes a business analyst, or, if the project is an agile one,
a product owner who acts as an intermediary between the business and
the development team. In this case, all communication usually passes
through this business analyst or product owner, but it’s not always the
best approach given the gaps between the business world and the
development world. The most important aspect of domain-driven design
is communication, and—this is what people should take note of—requires
greater cooperation between domain specialists, those who understand
how the company operates, and the development team. Businesses
will communicate in their own terminology, such as accounts, debiting,
crediting, etc., while developers will understand something different.
Therefore, it’s critical to establish a common understanding and,
ultimately, a common language in which to communicate.
Domain-Driven Design 10
To conclude, DDD’s key objectives are:
• Centering the project’s focus on the business domain and its logic.
Domain-Driven Design 12
The primary advantages:
1
Helps the teams create a common model.
The business and developer teams can then
use this model to communicate the business
Eases
Communication requirements, data entities, and process
models.
Domain-Driven Design 13
Gives software developers the principles
and patterns to solve tough problems in
software and, at times, in business. Here,
it’s also important to understand that an
approach based on Domain-Driven Design
will help the synchronization between
business and development. That means that
the model created by developers will reflect
3
the business domain, but also that this
model may influence the business and its
inner behavior.
Improved
As developers or technical experts, we’ll
Patterns
start by analyzing the business domain and
trying to model it.
Because the business team may not have
a clear picture of all the details up-front,
the feedback that they receive from the
technical team will help them in improving
their understanding of how business works
and, maybe, identifying any gaps or
difficult-to-solve impediments.
Domain-Driven Design 14
Nothing is 100% suitable for every
single use-case, and, of course,
there are also some disadvantages:
Domain-Driven Design 15
If not available ‘in-house’, they are often
expensive to hire since they hold valuable
knowledge.
Domain experts are required for a complex
business, so we will need a team of them
who can contribute to knowledge sharing
and explain concepts to the technical team
that will use the gathered knowledge to 2
create good business models. This is an
issue, though, because domain experts Requires
are usually busy people who aren’t always Domain Experts
available, and getting everyone in the same
room is difficult. So, keep this in mind when
estimating the time required to gather
knowledge from various domain experts
if you’re working on creating or upgrading
an existing system with complex business
needs.
Domain-Driven Design 16
Domain-Driven
Design Building
. Blocks
Domain-Driven Design 17
The main Domain-Driven Design
concepts (or building blocks) are [4]:
Bounded
This concept was introduced to
Context / isolate specific areas of the business
Context from each other.
Maps
There are just a few principles, but the domain-driven design is more
difficult to grasp since it involves a lot of storytelling, philosophy, and
case studies of various real-world challenges to provide some sort of
solution to them.
Domain-Driven Design 18
To allow the fluent sharing of knowledge, Domain-Driven Design calls
for the cultivation of a shared, business-oriented language: Ubiquitous
Language. This language should resemble the business domain and its
terms, entities, and processes—the communication vehicle mentioned
above.
Defining a Ubiquitous Language is not a trivial thing because the defined
terms can have different meanings in different contexts. To overcome this
hurdle, another Domain-Driven Design concept
is employed: the Bounded Context.
Domain
Ubiquitous
Sales Context
Language
Bounded
Context
Domain-Driven Design 19
The Bounded Context concept was introduced to isolate specific areas
of the business from each other, all together constituting what is called a
Domain. So, if we’re dealing with accounting, we’re going to have concepts
like Customer, Product, and so on. Even though a Customer could be found
in Orders or Sales, the details related to it in these contexts may differ from
the details of the Customer in the Accounting context. In conclusion, there
are some differences, and it’s important to isolate specific parts of the
functionality in what is called, maybe in a not-so-self-explanatory manner,
Bounded Context.
So, think of Bounded Context as an isolated sandbox that will contain
specific parts of the domain knowledge and that will not interfere with
other sandboxes. Within this sandbox, language specific to that particular
business area is defined—a specific Ubiquitous Language. Outside of this
sandbox, the Ubiquitous Language’s terms may have different meanings.
Context Maps are basically a way of describing how information can be
exchanged between two sandboxes, such as Accounting and Sales, that
both refer to a specific Customer. For example, because the two definitions
of the same concept are incompatible, we would not be able to access a
Sales Customer with different attributes than an Accounting Customer.
Context Maps serve to create some adapters on both sides while keeping
the Customer concept unchanged on both sandboxes.
Domain-Driven Design 20
Domain
Sales Bounded
Context Context
ap Upstream
t M
tex
n
Downstream Co
Bounded Customer
Context Context
We’ll almost certainly need some kind of safeguard for the downstream
context in order to maintain its conceptual integrity against changes in
the upstream one. DDD identifies a number of organizational patterns
that can be used to describe and/or manage the interactions between
various contexts. The best pattern for this is called “Anti-Corruption
Layer” (ACL) [6], and it requires an explicit translation between the
two contexts at the code level, or better yet, at the context’s external
boundary [7].
Domain-Driven Design 21
Domain-Driven Design revolves around the idea of solving an
organization’s problems through code. This is achieved by focusing
the investment of resources at the heart of the business logic of the
application.
The Domain refers to the world of the business we’re working with and
the issues they’re trying to solve. This usually entails rules, processes,
and existing systems that must be integrated into your solution. So, to put
it another way, the Domain is the problem we’ll have to solve.
Within the context of most businesses, there will be terms that have
specific meanings. They’ll almost certainly have metrics, goals, and
objectives that are specific to their industry. The Domain of your project
will be made up of all of your knowledge about the company and how it
operates.
Domain-Driven Design 22
The Model of a Domain-Driven Designed project is your solution to a
specific problem; it’s how we’d approach what the business is trying to
solve by hiring us as technical experts and developers.
So, after discussion rounds, after we’ve defined the common language (or
ubiquitous language, as it’s known in Domain-Driven Design), and after
we’ve gained enough knowledge about the specific domain area to be
able to implement what the business requires, the Model is what we’ll
come up with.
Domain-Driven Design 23
Domain-Driven
Design Patterns
• Value Object – an object that is defined only by its state, and has
no identity. Such objects should be treated as immutable.
Domain-Driven Design 25
The diagram below provides an overview of the key
domain-driven design concepts:
Repositories
Domain Domain
Services Events access with access with
encapsulate with
act as root of
Entities
express model with Aggregates
express model with
express model with encapsulate with
encapsulate with
Model-Driven express model with Value
Design Objects encapsulate with
keep model
names unified by Continuous
enter
Integration Tactical Design
Bounded
Context
overlap allied
context through
Shared
Kernel
assess / overview
relationships with relate allied
context as
Context Customer /
Map overlap Supplier Teams
unilaterally as
Strategic Design
segregate the
conceptual messes
Ways Language
Layer
Domain-Driven Design 26
The E-Commerce
Domain-Driven
Designed Sample
Domain-Driven Design 27
In this chapter, we’ll provide a high-level overview of the concepts behind
a sample e-commerce solution. Before applying Domain-Driven Design,
the Domain of such a system might look like this UML Package diagram
[10, 11]:
Domain-Driven Design 28
Let’s start applying Domain-Driven Design to this sample Domain:
1. Identify the possible sub-domains inside a Domain (the “problem” to
solve). A Domain has its own strategic challenges which can be seen as
sub-domains that can be classified as Core, Support and Generic.
2. Split the Domain in sub-domains. It is a good practice to set a
Bounded Context for each sub-domain.
The Support Bounded Contexts are auxiliary ones. They will support the
Core, but they will have their own model.
Generic Bounded Contexts are similar to Support ones, but they have a
strong particularity: they are pretty generic so that they can be used not
only in the domain where they were created, but they can be used by
other domains, too.
Domain-Driven Design 29
• Identify User Stories – identify the user stories (the backlog
grooming) following the role-feature-benefit pattern (or template) [13]:
As a [type of user], I want [an action], so that [a benefit/value]
• Identify the Nouns in the user stories -> Entities – the nouns
will become the main objects, not attributes. Just to enumerate few of
them in the context of the E-Commerce sample: Customer, Item, Order,
Shopping Cart
• Identify the Verbs in the user stories -> Behavior – in the same
context, these can be Add items to the Shopping Cart, Specify delivery
address, and many others
Domain-Driven Design 30
Figure 6 The object interaction diagram
• Identify object responsibilities (UML diagram) [14]
Domain-Driven Design 31
• Create the UML class diagram (only the main relationships)
Domain-Driven Design 32
The proposed architectural pattern for this type of solution is called
“Clean Architecture”. This name was coined by Robert “Uncle Bob”
Martin in his book “Clean Architecture: A Craftsman’s Guide to Software
Structure and Design”. This pattern is not totally new, it has evolved
over time from several other architectural patterns like Hexagonal
Architecture (or ports and adapters pattern) proposed by Alistair
Cockburn in 2005, and Onion Architecture created by Jeffrey Palermo in
2008.
Domain-Driven Design 33
Clean Architecture Building Blocks [15]
Domain-Driven Design 34
Figure 10 An alternative view
Domain-Driven Design 35
The above views are pretty abstract, and it’s hard to figure out how, as
developers, this architectural pattern can be applied to real-life projects.
That’s why a more down-to-earth approach really helps to get a better
understanding of what to do [17]:
Domain-Driven Design 36
Let’s see what each layer is responsible for:
• Application Layer
o Implements use cases
o High-level application logic
o Knows about Domain, but no other layers
o Implemented using the CQS [18] pattern for simple scenarios; for
complex ones, the CQRS [19] pattern can be used
• Domain Layer
o Contains the Domain core Model
o No knowledge of other layers
o Independent of frameworks (no attributes or decorations)
o Beware of Anemic Domain Model antipattern [20]
• Persistence Layer
o Refers to Repository [21] pattern incarnations; the data source can
be anything from a relational database to a REST API
o Depends on Domain and is invoked by Application only
o Acts as a Mediator [22] between Domain and storage-specific entities
• Infrastructure Layer
o Facilitates the interaction with external systems and/or services
o Is invoked by Application only
o Can have a dependency on Domain, and can act as a Mediator [22]
between Domain and infrastructure specific entities
• Cross-Cutting Concerns
o Contains functionality used by more than one layer like Logging,
Exception Handling, Services Events, etc.
Domain-Driven Design 37
Conclusions
Patterns: Domain-Driven Design gives software developers the
principles and patterns to solve tough problems in software and, at times,
in business
Domain-Driven Design 39
References
1. https://en.wikipedia.org/wiki/Single-responsibility_principle
2. https://en.wikipedia.org/wiki/Anemic_domain_model
3. https://en.wikipedia.org/wiki/Separation_of_concerns
4. https://en.wikipedia.org/wiki/Domain-driven_design
5. https://thedomaindrivendesign.io/
6. https://dev.to/asarnaout/the-anti-corruption-layer-pattern-pcd
7. https://www.infoq.com/articles/ddd-contextmapping/
8. https://www.culttt.com/2014/11/12/domain-model-domain-driven-
design/
9. https://blog.jannikwempe.com/domain-driven-design-entities-value-
objects
10. https://www.uml-diagrams.org/package-diagrams-overview.html
11. http://www.fabriciosuarte.com/2016/02/domain-driven-design-
hands-on-example.html
12. http://www.zankavtaskin.com/2014/12/applied-domain-driven-
design-ddd-part-0.html
13. https://www.digite.com/agile/user-stories/
14. https://www.tutorialspoint.com/uml/uml_object_diagram.htm
15. https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-
architecture.html
16. https://www.freecodecamp.org/news/a-quick-introduction-to-clean-
architecture-990c014448d2/
17. https://matthewrenze.com/courses/clean-architecture-patterns-
practices-and-principles/
18. https://en.wikipedia.org/wiki/Command–query_separation
19. https://en.wikipedia.org/wiki/Command–query_
separation#Command_Query_Responsibility_Separation
20. https://www.martinfowler.com/bliki/AnemicDomainModel.html
21. https://www.martinfowler.com/eaaCatalog/repository.html
22. https://en.wikipedia.org/wiki/Mediator_pattern
Domain-Driven Design 41
+33 970754570
Pentalog, 1 rue des Hauts
45380 La Chapelle Saint-Mesmin
PENTALOG.COM