Skip to content
Natalie Perret
Go back

DDD (Domain-Driven Design) Basics

Deep Dive into Domain-Driven Design (DDD)

Table of contents

Open Table of contents

Why DDD Matters

DDD addresses a common problem in software development: miscommunication between technical teams and business stakeholders. By centering the design process around the domain, DDD ensures that software reflects real-world business processes, leading to more effective, maintainable, and scalable solutions.

Key Concepts of DDD

Domain

Definition

The domain is the problem space or business area the software addresses. It includes the processes, rules, and entities that define the business’s operations. Understanding the domain is foundational to DDD, as it shapes the software’s purpose and scope.

Example

For an e-commerce platform, the domain encompasses product management, customer interactions, order processing, payments, and shipping logistics.

Ubiquitous Language

Definition

The ubiquitous language is a common vocabulary shared by developers, domain experts, and other stakeholders. It ensures clarity and consistency across conversations, documentation, and code, reducing misunderstandings and aligning the team with the domain.

Example

In an e-commerce platform, terms like “Product,” “Customer,” “Order,” “Cart,” “Checkout,” “Payment,” and “Shipping” form the ubiquitous language, used everywhere from meetings to method names.

Bounded Context

Definition

A bounded context divides the domain into smaller, manageable parts, each with its own model and language. This separation prevents overlap and ambiguity, allowing teams to work independently while maintaining system coherence.

Example

An e-commerce platform might have:

Each context has its own model and language, tailored to its specific needs.

Entities

Definition

Entities are objects with a unique identity that persists over time, distinct from their attributes. They represent core domain concepts that require tracking and differentiation.

Example

Value Objects

Definition

Value objects are objects defined by their attributes, lacking a unique identity. They are immutable and used to describe properties or behaviors within the domain.

Example

Aggregates

Definition

An aggregate is a group of related objects treated as a single unit, managed by an aggregate root, the only externally accessible object. Aggregates enforce consistency and business rules (invariants) within their boundaries.

Example

Repositories

Definition

Repositories provide a collection-like interface for accessing and persisting aggregates, abstracting the underlying storage mechanism (e.g., database). They keep domain logic separate from infrastructure concerns.

Example

Services

Definition

Services handle operations that don’t naturally belong to entities or value objects, often coordinating multiple aggregates or external systems. DDD distinguishes between:

Example

Factories

Definition

Factories encapsulate the creation of complex objects or aggregates, ensuring they are instantiated in a valid state. They simplify construction logic and uphold domain integrity.

Example

Domain Events

Definition

Domain events represent significant occurrences in the domain that trigger reactions or updates. They enable loose coupling and support event-driven architectures.

Example

Integration Events

Definition

Integration events are messages published when a significant change occurs in one bounded context, allowing other contexts or external systems to react. They facilitate communication across bounded contexts, ensuring eventual consistency and loose coupling.

Example

When an order is placed in the Sales Context, an OrderPlacedEvent (a Domain Event) is published within the context to handle internal actions like updating the order status. To notify the Shipping Context, an OrderCreatedIntegrationEvent is published. The Shipping Context subscribes to this event and reacts by creating a shipping order. Integration Events are often managed via message brokers or event buses, decoupling the contexts and enhancing scalability.

Modules

Definition

Modules organize the domain model into cohesive groups, mirroring the domain’s structure. They improve readability and maintainability by grouping related concepts.

Example

In an e-commerce system, code might be organized into modules like Catalog, Sales, and Shipping, each containing relevant entities, aggregates, and services.

Strategic Design

Strategic design focuses on the big picture, defining how bounded contexts relate and integrate.

Context Mapping

Definition

Context mapping outlines relationships between bounded contexts (e.g., partnerships, dependencies), ensuring clear boundaries and interactions.

Example

In the e-commerce platform:

A context map visually represents these dependencies, helping teams understand how changes in one context might impact others and ensuring proper communication protocols.

Anti-Corruption Layer

Definition

An anti-corruption layer (ACL) isolates a bounded context from external or legacy systems by translating their data into the domain model, preserving its integrity.

Example

If the e-commerce platform integrates with a legacy inventory system using different terminology (e.g., “item_code” instead of “product_id”), the ACL translates this data into the domain’s ubiquitous language, preventing the legacy system’s complexities from affecting the core domain.

Tactical Design

Tactical design dives into the details of modeling the domain with entities, aggregates, and repositories.

Aggregate Design

Definition

Aggregate design involves defining aggregate boundaries and roots to enforce business rules effectively.

Example

For the Order Aggregate:

Repository Implementation

Definition

Repository implementation focuses on building repositories with methods tailored to domain needs, hiding persistence details.

Example

The OrderRepository might include:

Internally, it might use SQL queries (e.g., SELECT * FROM orders WHERE id = ?), but the domain layer remains unaware of these details, ensuring separation of concerns.

Benefits of DDD

Challenges of DDD

Key Takeaways

DDD is a powerful framework for building software that reflects complex business domains. By prioritizing the domain, fostering collaboration, and using proven patterns like entities, aggregates, and domain events, developers can create robust, adaptable systems. Yes, domain events, repositories, services, and factories are core to DDD, they enable precise domain modeling and implementation. For further exploration, start with Eric Evans’ book and tap into the vibrant DDD community.


Share this post on:

Previous Post
Learning Neovim > 4: Search & Replace