Domain Driven Design

The Blue Book

Tackling Complexity in the Heart of Software

Eric Evans, 2003

blue book

DDD Quickly

What’s DDD ?

  • 👥 Domain model oriented approach from the domain expert inputs

  • 🛠 Toolbox of tactical and strategic patterns

  • 🔫 Not a silver bullet

DDD vs CRUD

twitter cyriux

Why DDD ?

  • 🤝 Better understand and communicate with your business experts

  • 🤯 Decrease accidental complexity to focus on business complexity

  • 😎 Master the complexity and improve maintenability

  • ✂️ Separate code that changes frequently from code that is stable

Complexity

Strategic & Tactical patterns

patterns

Ubiquitous Language

  • 🤝 Build a common and rigorous language between developers and business experts

  • 🥸 As the team better understands their business, it could evolve

  • 😇 You should align your code naming (class, variables, …​) with the ubiquitous Language

Domain model

  • 🤯 The domain is the problem

  • 😎 The model is the solution

  • ❌ Not a database model, not a UML model

  • 📚 Object oriented model based on Entity, ValueObject and Aggregate

Entity

  • Is an object defined by its identity

  • Has a lifecycle (created, updated, deleted)

  • Has attributes that change over time

Entity - Example

Let’s define a Contract

entity

Value Object

  • Is an object with no identity

  • Is defined by its immutable attributes

  • Can be shared between identities

  • 🤫 It’s an immutable dataclass

Value Object - Example

Let’s introduce a Period and use it in Contract

value object

Entity vs Value Object

entity vs value object

Aggregate

  • Is a cluster of domain objects that can be treated as a single unit (= same transaction)

  • Has one root entity

  • All references from other aggregates should reference the root, not the sub entities.

Aggregates

aggregates

Value Aggregates - Example 1

aggregate 1

Value Aggregates - Example 2

aggregate 2

Repository

  • Represents a persistent collection of entities

  • One repository per root aggregate

  • Is not a DAO (Data Access Object)

Repository - Example

class Companies:
    def get_company(company_id) -> Optional[Company]:
        pass

    def add_company(company) -> uuid:
        pass

    def remove_company(company_id):
        pass

Show me some code

One Aggregate
def add_contract_to_company(
        company_id, reference, start_date) -> Contract:
    company = companies.get_company(company_id)
    contract = company.add_contract(reference, start_date)
    return contract
Two Aggregates
def create_contract(
        company_id, reference, start_date) -> Contract:
    contract = Contract(company_id, reference, start_date)
    contracts.add_contract(contract)
    return contract

Scaling DDD - #15430

bounded context

DDD & Architecture

architecture

Hexagonal Architecture

Alistair Cockburn, 2005

ddd hexagonal

Hexagonal dependencies

hexagonal

I can talk DDD for hours…​🤣

  • 🗣 Ubiquitous Langage

  • 👨‍🏫 Domain Model: Entity, Value Object organized by Aggregates

  • 🧳 Repository to handle collection of root Aggregates

  • Hexagonal architecture to isolate domain model from technical stuff

Alan & DDD

🍿 DDD and 13280 - Moving towards a product architecture

❓ Do you think we can use some DDD patterns for Alan codebase?