Skip to content

DDD (Domain-Driven Design)

DDD (Domain-Driven Design)

Domain-Driven Design

1. Overview of DDD: A Methodology for Designing Complex Software Around the Business Domain

    flowchart LR
    A["Technology/DB-centered design<br/>(technical debt, distorted domain)"] --"Shift to a<br/>domain-model-centered approach"--> B["Ubiquitous Language<br/>+ Bounded Context"] --"Apply strategic<br/>& tactical design"--> C["Build a maintainable<br/>complex system"]

    style A fill:#FFEBEE,stroke:#D32F2F,color:#000
    style B fill:#E3F2FD,stroke:#1976D2,color:#000
    style C fill:#E8F5E9,stroke:#388E3C,color:#000
  

Definition: A methodology for solving complex business problems by placing the model and logic of the business domain — rather than the technical implementation — at the center of design, developing software based on a common language (Ubiquitous Language) shared between domain experts and developers.

Characteristics: (Ubiquitous Language) Domain experts and developers use the same terminology consistently in code, documentation, and conversation. (Strategic design) Bounded Contexts and Context Maps separate a complex domain into independent boundaries. (Tactical design) Aggregates, Entities, Value Objects, and Domain Events precisely implement the domain model.


2. Core Structure of DDD

a. Strategic Design — Bounded Context and Context Map

    flowchart TD
    subgraph R1["​"]
        direction LR
        BC1["Order Bounded Context<br/>order, payment, and delivery flow<br/>Order, Payment, Delivery"]
        BC2["Product Bounded Context<br/>product, inventory, and catalog<br/>Product, Stock, Catalog"]
        BC3["Customer Bounded Context<br/>membership, auth, and grade<br/>Customer, Auth, Grade"]
    end
    subgraph R2["​"]
        direction LR
        UBIQLANG["Ubiquitous Language<br/>a shared vocabulary between<br/>domain experts and developers"]
        CMAP["Context Map<br/>defines relationships<br/>between Bounded Contexts<br/>ACL, OHS, Conformist"]
    end

    style BC1 fill:#E3F2FD,stroke:#1976D2,color:#000
    style BC2 fill:#F3E5F5,stroke:#7B1FA2,color:#000
    style BC3 fill:#FFF3E0,stroke:#F57C00,color:#000
    style UBIQLANG fill:#E8F5E9,stroke:#388E3C,color:#000
    style CMAP fill:#FFEBEE,stroke:#D32F2F,color:#000
    style R1 fill:none,stroke:none
    style R2 fill:none,stroke:none
  
ConceptDefinitionLink to MSA
Bounded ContextThe boundary within which a single domain model applies consistentlyCriterion for separating microservice boundaries in MSA
Ubiquitous LanguageA common vocabulary used consistently by everyone within a Bounded ContextUnifies code, API, and DB column names around domain language
Context MapDefines the relationships and integration methods between Bounded ContextsACL (Anti-Corruption Layer), OHS (Open Host Service), Conformist
Shared KernelA common model area shared by two Bounded ContextsSeparated and managed as a shared domain library

b. Tactical Design — Aggregate, Entity, Value Object

    flowchart TD
    subgraph AGG["Aggregate"]
        direction TB
        AE["Aggregate Root<br/>Order"]
        E1["Entity<br/>OrderLine<br/>has a unique identifier"]
        E2["Entity<br/>ShippingInfo<br/>has a unique identifier"]
        VO["Value Object<br/>Money<br/>immutable, no identifier"]
        AE --> E1
        AE --> E2
        AE --> VO
    end

    DE["Domain Event<br/>OrderPlaced, OrderCancelled<br/>publishes the fact of a state change"]
    REP["Repository<br/>interface for persisting<br/>and retrieving an Aggregate"]
    SVC["Domain Service<br/>domain logic that doesn't<br/>belong to a single Entity"]

    AGG --> DE
    REP --> AGG
    AGG --> SVC

    style AGG fill:#E3F2FD,stroke:#1976D2,color:#1E3A5F
    style DE  fill:#FFF3E0,stroke:#F57C00,color:#000
    style REP fill:#E8F5E9,stroke:#388E3C,color:#000
    style SVC fill:#F3E5F5,stroke:#7B1FA2,color:#000
  
Building BlockDefinitionCharacteristics
AggregateA cluster of related objects grouped as a unit of data changeInternal access allowed only through the Aggregate Root
Aggregate RootThe entry point of an Aggregate, responsible for guaranteeing consistencyThe only object referenceable from outside
EntityHas a unique identifier (ID) and changes state over its lifecycleThe same ID means the same Entity even if attributes differ
Value ObjectAn immutable object defined only by its attribute values, with no identifierMoney, Address, DateRange, etc.
Domain EventA significant occurrence in the domain, named in the past tenseOrderPlaced, PaymentCompleted
RepositoryAn interface that abstracts storing and retrieving an AggregateIsolates the domain from persistence technology (DB)
Domain ServiceDomain logic that doesn’t belong to a specific EntityLogic spanning multiple Aggregates, such as a Transfer

3. Expected Benefits and Application of DDD

CategoryExpected BenefitsApplication and Practical Use
MSA designClear service boundaries based on Bounded ContextsAgree on domain boundaries via Event Storming workshops, then split MSA services
MaintainabilitySeparating domain logic from infrastructure technology minimizes the impact of changeCombine with Clean Architecture to apply Dependency Inversion (DIP) for external dependencies
Collaboration efficiencyUbiquitous Language closes the communication gap between domain experts and developersUnify API specs, DB schema, and code variable names around domain language
Complexity managementAggregate boundaries clarify transaction scope and consistency responsibilityCombine with the Saga pattern to implement eventual consistency in distributed environments