Contents
Domain-Driven Design (DDD) is an architectural and design approach that puts the domain at the center of the software development process. It emphasizes understanding the business domain, modeling it effectively, and creating a flexible, maintainable, and scalable software solution.
Key Concepts of Domain-Driven Design
- Ubiquitous Language:
- Shared language and glossary used by domain experts and developers to eliminate communication gaps.
- It creates a common understanding of the domain model and fosters collaboration.
- Bounded Context:
- Defines a clear boundary within which a specific model, language, and context apply.
- Helps manage complex domains by breaking them down into smaller, cohesive contexts.
- Aggregates:
- Clusters of related objects treated as a single unit.
- They ensure consistency and encapsulate business rules.
- Entities:
- Objects with a unique identity that persists over time.
- They have attributes and behaviors relevant to the domain.
- Value Objects:
- Objects without an identity, defined solely by their attributes.
- They represent concepts in the domain and are immutable.
Strategic Design Patterns
DDD provides strategic design patterns that help shape the architecture and organization of software projects.
Some key strategic design patterns include:
Context Mapping
Context Maps help in understanding the whole project, being able to show the relationships between the different
Bounded Contexts
It is extremely important to understand the relationship between Bounded Contexts so that you can build a domain model correctly.
Shared Kernel
A shared context between two or more teams, which reduces duplication of code, however, any changes must be combined and notified between teams.
Customer/Supplier
It is a relationship between client (downstream) and server (upstream), where the teams are in continuous integration.
Conformist
It is the scenario that involves the upstream and downstream teams, but in this model the upstream team has no motivation to meet the needs of the downstream team.
Anti-Corruption Layer
It is the scenario where the client (downstream) creates an intermediate layer that communicates with the upstream context, to meet its own domain model.
Domain Event
A domain event is, something that happened in the domain that you want other parts of the same domain (in-process) to be aware of. The notified parts usually react somehow to the events.
Tactical Design Patterns
In addition to strategic patterns, DDD also offers tactical design patterns to handle the internal structure of the domain model.
Some common tactical design patterns include:
Aggregate
It is one of the most important and complex patterns of Tactical Design, Aggregates are based on two other Tactical Standards, which are Entities and Value Objects. An Aggregate is a Cluster of one or more Entities, and may also contain Value Objects. The Parent Entity of this Cluster receives the name of Aggregate Root.
Repository
Repositories are mainly used to deal with storage, they abstract concerns about data storage. They are responsible for persisting Aggregates.
Factory
Factories are used to provide an abstraction in the construction of an Object, and can return an Aggregate root, an Entity, or an Value Object. Factories are an alternative for building objects that have complexity in building via the constructor method.
Domain Service
Services are stateless objects that perform some logic that do not fit with an operation on an Entity or Value Object. They perform domain-specific operations, which can involve multiple domain objects.
Value Object
Value Objects are immutable and do not have a unique identity, are defined only by the values of their attributes. The consequence of this immutability is that in order to update a Value Object, you must create a new instance to replace the old one.
Domain Event
Events indicate significant occurrences that have occurred in the domain and need to be reported to other stakeholders belonging to the domain. It is common for Aggregates to publish events.
Benefits of Domain-Driven Design
- Improved Collaboration:
- DDD fosters collaboration between domain experts and developers through the use of a shared ubiquitous language.
- It bridges the gap between technical and business stakeholders.
- Enhanced Domain Understanding:
- By focusing on the domain, DDD improves the understanding of complex business domains.
- It helps in capturing and modeling complex business rules and processes effectively.
- Maintainable and Evolvable Codebase:
- DDD’s emphasis on bounded contexts, aggregates, and domain-driven architecture leads to a modular and maintainable codebase.
- It enables easier refactoring, extension, and evolution of the software solution over time.