Domain-driven design (DDD) is a major
software design
Software design is the process of conceptualizing how a software system will work before it is implemented or modified.
Software design also refers to the direct result of the design process the concepts of how the software will work which co ...
approach,
focusing on modeling software to match a
domain according to input from that domain's experts.
DDD is against the idea of having a single unified model; instead it divides a large system into bounded contexts, each of which have their own model.
Under domain-driven design, the structure and language of software code (class names,
class methods,
class variables) should match the business domain. For example: if software processes loan applications, it might have classes like "loan application", "customers", and methods such as "accept offer" and "withdraw".
Domain-driven design is predicated on the following goals:
* placing the project's primary focus on the core
domain and domain logic layer;
* basing complex designs on a model of the domain;
* initiating a creative collaboration between technical and
domain experts to iteratively refine a conceptual model that addresses particular domain problems.
Critics of domain-driven design argue that developers must typically implement a great deal of isolation and encapsulation to maintain the model as a pure and helpful construct. While domain-driven design provides benefits such as maintainability, Microsoft recommends it only for complex domains where the model provides clear benefits in formulating a common understanding of the domain.
The term was coined by
Eric Evans in his book of the same name published in 2003.
Overview
Domain-driven design articulates a number of high-level concepts and practices.
Of primary importance is a ''domain'' of the software, the subject area to which the user applies a program. Software's developers build a ''domain model'': a system of abstractions that describes selected aspects of a domain and can be used to solve problems related to that domain.
These aspects of domain-driven design aim to foster a ''common language'' shared by domain experts, users, and developers—the ''ubiquitous language''. The ubiquitous language is used in the domain model and for describing system requirements.
Ubiquitous language is one of the pillars of DDD together with ''strategic design'' and ''tactical design''.
In domain-driven design, the
domain layer is one of the
common layers in an
object-oriented multilayered architecture.
Kinds of models
Domain-driven design recognizes multiple kinds of models. For example, an ''entity'' is an object defined not by its attributes, but its
identity. As an example, most airlines assign a unique number to seats on every flight: this is the seat's identity. In contrast, a ''
value object'' is an
immutable object that contains attributes but has no conceptual identity. When people exchange business cards, for instance, they only care about the information on the card (its attributes) rather than trying to distinguish between each unique card.
Models can also define ''events'' (something that happened in the past). A domain event is an event that domain experts care about. Models can be bound together by a root entity to become an ''aggregate''. Objects outside the aggregate are allowed to hold references to the root but not to any other object of the aggregate. The aggregate root checks the consistency of changes in the aggregate. Drivers do not have to individually control each wheel of a car, for instance: they simply drive the car. In this context, a car is an aggregate of several other objects (the engine, the brakes, the headlights, etc.).
Working with models
In domain-driven design, an object's creation is often separated from the object itself.
A ''repository'', for instance, is an object with methods for retrieving domain objects from a data store (e.g. a database). Similarly, a
factory
A factory, manufacturing plant or production plant is an industrial facility, often a complex consisting of several buildings filled with machinery, where workers manufacture items or operate machines which process each item into another. Th ...
is an object with methods for directly creating domain objects.
When part of a program's functionality does not conceptually belong to any object, it is typically expressed as a
service.
Event types
There are different types of
events in DDD, and opinions on their classification may vary. According to Yan Cui, there are two key categories of events:
Domain Events
Domain events signify important occurrences within a specific business domain. These events are restricted to a bounded context and are vital for preserving business logic. Typically, domain events have lighter
payloads, containing only the necessary information for processing. This is because event listeners are generally within the same service, where their requirements are more clearly understood.
Integration Events
On the other hand, integration events serve to communicate changes across different bounded contexts. They are crucial for ensuring
data consistency
Data inconsistency refers to whether the same data kept at different places do or do not match.
Point-in-time consistency
Point-in-time consistency is an important property of backup files and a critical objective of software that creates backups. ...
throughout the entire system. Integration events tend to have more complex payloads with additional
attributes, as the needs of potential listeners can differ significantly. This often leads to a more thorough approach to communication, resulting in overcommunication to ensure that all relevant information is effectively shared.
Context Mapping patterns
Context Mapping identifies and defines the boundaries of different domains or subdomains within a larger system. It helps visualize how these contexts interact and relate to each other. Below are some patterns, according to Eric Evans:
* Partnership: ''"forge a partnership between the teams in charge of the two contexts. Institute a process for coordinated planning of development and joint management of integration"'', when ''"teams in two contexts will succeed or fail together"''
* Shared Kernel: ''"Designate with an explicit boundary some subset of the domain model that the teams agree to share. Keep this kernel small."''
* Customer/Supplier Development: ''"Establish a clear customer/supplier relationship between the two teams"'', when ''"two teams are in
upstream-downstream relationship"''
* Conformist: ''"Eliminate the complexity of translation
..choosing conformity enormously simplifies integration"'', when a custom interface for a downstream subsystem isn't likely to happen
* Anticorruption Layer: ''"create an isolating layer to provide your system with functionality of the upstream system in terms of your own domain model"''
* Open-host Service: ''"a protocol that gives access to your subsystem as a set of services"'', in case it's necessary to integrate one subsystem with many others, making custom translations between subsystems infeasible
* Published Language: ''"a well-‐documented shared language that can express the necessary domain information as a common medium of communication"'', e.g.
data interchange standards in various industries
* Separate Ways": ''"a bounded context
ithno connection to the others at all, allowing developers to find simple, specialized solutions within this small scope"''
* Big Ball of Mud: ''"a boundary around the entire mess"'' when there's no real boundaries to be found when surveying an existing system
Relationship to other ideas
Although domain-driven design is not inherently tied to
object-oriented approaches, in practice, it exploits the advantages of such techniques. These include entities/aggregate roots as receivers of commands/method invocations, the encapsulation of state within foremost aggregate roots, and on a higher architectural level, bounded contexts.
As a result, domain-driven design is often associated with
Plain Old Java Objects and
Plain Old CLR Objects, which are technical implementation details, specific to
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
and the
.NET Framework respectively. These terms reflect a growing view that domain objects should be defined purely by the business behavior of the domain, rather than by a more specific technology framework.
Similarly, the
naked objects pattern holds that the user interface can simply be a reflection of a good enough domain model. Requiring the user interface to be a direct reflection of the domain model will force the design of a better domain model.
[.]
Domain-driven design has influenced other approaches to software development.
Domain-specific modeling, for instance, is domain-driven design applied with
domain-specific languages. Domain-driven design does not specifically require the use of a domain-specific language, though it could be used to help define a domain-specific language and support
domain-specific multimodeling.
In turn,
aspect-oriented programming makes it easy to factor out technical concerns (such as security, transaction management, logging) from a domain model, letting them focus purely on the business logic.
Model-driven engineering and architecture
While domain-driven design is compatible with
model-driven engineering
Model-driven engineering (MDE) is a software development methodology that focuses on creating and exploiting domain models, which are conceptual model (computer science), conceptual models of all the topics related to a specific problem. Hence, i ...
and
model-driven architecture, the intent behind the two concepts is different. Model-driven architecture is more concerned with translating a model into code for different technology platforms than defining better domain models.
However, the techniques provided by model-driven engineering (to model domains, to create domain-specific languages to facilitate the communication between domain experts and developers,...) facilitate domain-driven design in practice and help practitioners get more out of their models. Thanks to model-driven engineering's model transformation and code generation techniques, the domain model can be used to generate the actual software system that will manage it.
Command Query Responsibility Segregation
Command Query Responsibility Segregation (CQRS) is an architectural pattern for separating reading data (a 'query') from writing to data (a 'command'). CQRS derives from Command and Query Separation (CQS), coined by
Bertrand Meyer
Bertrand Meyer (; ; born 21 November 1950) is a French academic, author, and consultant in the field of computer languages. He created the Eiffel programming language and the concept of design by contract.
Education and academic career
Meyer ...
.
Commands mutate state and are approximately equivalent to method invocation on aggregate roots or entities. Queries read state but do not mutate it.
While CQRS does not require domain-driven design, it makes the distinction between commands and queries explicit with the concept of an aggregate root. The idea is that a given aggregate root has a method that corresponds to a command and a command handler invokes the method on the aggregate root.
The aggregate root is responsible for performing the logic of the operation and either yielding a failure response or just mutating its own state that can be written to a data store. The command handler pulls in infrastructure concerns related to saving the aggregate root's state and creating needed contexts (e.g., transactions).
Event storming
Event storming
'EventStorming'' is workshop-based methodto quickly find out what is happening in the Domain (software engineering), domain of a software program.
Compared to other methods it is extremely lightweight and intentionally requires no support by a co ...
is a collaborative, workshop-based modeling technique which can be used as a precursor in the context of Domain-Driven Design (DDD) to identify and understand domain events. This interactive discovery process involves stakeholders, domain experts, and developers working together to visualize the flow of domain events, their causes, and their effects, fostering a shared understanding of the domain. The technique often uses color-coded sticky notes to represent different elements, such as domain events, aggregates, and external systems, facilitating a clear and structured exploration of the domain. Event storming can aid in discovering subdomains, bounded contexts, and aggregate boundaries, which are key constructs in DDD. By focusing on 'what happens' in the domain, the technique can help uncover business processes, dependencies, and interactions, providing a foundation for implementing DDD principles and aligning system design with business goals.
Event sourcing
Event sourcing is an architectural pattern in which entities track their internal state not by means of direct serialization or object-relational mapping, but by reading and committing events to an
event store.
When event sourcing is combined with
CQRS and domain-driven design, aggregate roots are responsible for validating and applying commands (often by having their instance methods invoked from a Command Handler), and then publishing events. This is also the foundation upon which the aggregate roots base their logic for dealing with method invocations. Hence, the input is a command and the output is one or many events which are saved to an event store, and then often published on a message broker for those interested (such as an
application's view).
Modeling aggregate roots to output events can isolate internal state even further than when projecting read-data from entities, as in standard ''n''-tier data-passing architectures. One significant benefit is that axiomatic theorem provers (e.g. Microsoft Contracts and CHESS) are easier to apply, as the aggregate root comprehensively hides its internal state. Events are often persisted based on the version of the aggregate root instance, which yields a domain model that synchronizes in distributed systems through
optimistic concurrency.
Mapping Bounded Contexts to Microservices
A bounded context, a fundamental concept in Domain-Driven Design (DDD), defines a specific area within which a domain model is consistent and valid, ensuring clarity and separation of concerns. In
microservices
In software engineering, a microservice architecture is an architectural pattern that organizes an application into a collection of loosely coupled, fine-grained services that communicate through lightweight protocols. This pattern is characterize ...
architecture, a bounded context often maps to a microservice, but this relationship can vary depending on the design approach. A one-to-one relationship, where each bounded context is implemented as a single microservice, is typically ideal as it maintains clear boundaries, reduces coupling, and enables independent deployment and scaling. However, other mappings may also be appropriate: a one-to-many relationship can arise when a bounded context is divided into multiple microservices to address varying scalability or other operational needs, while a many-to-one relationship may consolidate multiple bounded contexts into a single microservice for simplicity or to minimize operational overhead. The choice of relationship should balance the principles of DDD with the system's business goals, technical constraints, and operational requirements.
Notable tools
Although domain-driven design does not depend on any particular tool or framework, notable examples include:
*
Actifsource, a plug-in for
Eclipse
An eclipse is an astronomical event which occurs when an astronomical object or spacecraft is temporarily obscured, by passing into the shadow of another body or by having another body pass between it and the viewer. This alignment of three ...
which enables software development combining DDD with
model-driven engineering
Model-driven engineering (MDE) is a software development methodology that focuses on creating and exploiting domain models, which are conceptual model (computer science), conceptual models of all the topics related to a specific problem. Hence, i ...
and
code generation.
* Context Mapper, a
Domain-specific language and tools for strategic and tactic DDD.
[Stefan Kapferer and Olaf Zimmermann: Domain-driven Service Design - Context Modeling, Model Refactoring and Contract Generation, 14th Symposium and Summer School On Service-Oriented Computing (SommerSoC 202]
/ref>
* CubicWeb, an open source semantic web framework entirely driven by a data model. High-level directives allow to refine the data model iteratively, release after release. Defining the data model is enough to get a functioning web application. Further work is required to define how the data is displayed when the default views are not sufficient.
* OpenMDX, an open-source, Java-based, MDA Framework supporting Java SE, Java EE, and .NET
The .NET platform (pronounced as "''dot net"'') is a free and open-source, managed code, managed computer software framework for Microsoft Windows, Windows, Linux, and macOS operating systems. The project is mainly developed by Microsoft emplo ...
. OpenMDX differs from typical MDA frameworks in that ''"use models to directly drive the runtime behavior of operational systems"''.
* Restful Objects, a standard for mapping a Restful API onto a domain object model (where the domain objects may represent entities, view models, or services). Two open source frameworks (one for Java, one for .NET) can create a Restful Objects API from a domain model automatically, using reflection.
See also
* Data mesh, a domain-oriented data architecture
* Event storming
'EventStorming'' is workshop-based methodto quickly find out what is happening in the Domain (software engineering), domain of a software program.
Compared to other methods it is extremely lightweight and intentionally requires no support by a co ...
* Knowledge representation
Knowledge representation (KR) aims to model information in a structured manner to formally represent it as knowledge in knowledge-based systems whereas knowledge representation and reasoning (KRR, KR&R, or KR²) also aims to understand, reason, and ...
* Ontology (information science)
In information science, an ontology encompasses a representation, formal naming, and definitions of the categories, properties, and relations between the concepts, data, or entities that pertain to one, many, or all domains of discourse. More ...
* Semantic analysis (knowledge representation)
Semantic analysis is a method for eliciting and representing knowledge about organisations. Liu Kecheng, (2000Semiotics in information systems engineering Cambridge University Press.
Initially the problem must be defined by domain experts and pa ...
* Semantic networks
A semantic network, or frame network is a knowledge base that represents semantic relations between concepts in a network. This is often used as a form of knowledge representation. It is a directed or undirected graph consisting of vertices, ...
* Semantics
Semantics is the study of linguistic Meaning (philosophy), meaning. It examines what meaning is, how words get their meaning, and how the meaning of a complex expression depends on its parts. Part of this process involves the distinction betwee ...
* C4 model
* Strongly typed identifier
* Integrated design
* Systems science
Systems science, also referred to as systems research or simply systems, is a transdisciplinary field that is concerned with understanding simple and complex systems in nature and society, which leads to the advancements of formal, natural, socia ...
References
External links
*
DDD Crew on GitHub
Bounded Context Canvas, Aggregate Canvas, Modeling Process and more repositories
* {{Citation , url = http://www.methodsandtools.com/archive/archive.php?id=97 , title = An Introduction to Domain Driven Design , publisher = Methods & tools
Implementing Aggregate root in C# language
Context Mapper: A Modeling Framework for Strategic Domain-driven Design (Tool, Tutorials, DDD Modeling Examples)
an
Software architecture
Software design
Software development philosophies