Clean Architecture + DDD + Hexagonal
Overview
This skill combines three complementary patterns for building maintainable, testable backend systems:
- Domain-Driven Design (DDD) - Strategic and tactical patterns for modeling complex business domains
- Clean Architecture - Dependency rules ensuring business logic independence
- Hexagonal Architecture - Ports & Adapters for external system isolation
Key Sources:
Core Principles
The Dependency Rule
Dependencies point inward only. Outer layers depend on inner layers, never the reverse.
flowchart TB
subgraph Infrastructure["Infrastructure (Adapters)"]
subgraph Application["Application (Use Cases / Ports)"]
subgraph Domain["Domain (Entities, Value Objects, Aggregates)"]
Core[" "]
end
end
end
style Domain fill:#10b981,stroke:#059669,color:white
style Application fill:#3b82f6,stroke:#2563eb,color:white
style Infrastructure fill:#6366f1,stroke:#4f46e5,color:white
style Core fill:#10b981,stroke:#059669
Hexagonal Ports & Adapters
- Driver Ports (Primary/Inbound): How the world uses your application
- Driven Ports (Secondary/Outbound): How your application uses external systems
- Adapters: Concrete implementations of ports
Quick Reference Structure
src/
├── domain/ # Core business logic (no dependencies)
│ ├── {aggregate}/
│ │ ├── entity.ts # Aggregate root + entities
│ │ ├── value_objects.ts # Immutable value types
│ │ ├── events.ts # Domain events
│ │ ├── repository.ts # Repository interface (port)
│ │ └── services.ts # Domain services
│ └── shared/
│ └── errors.ts # Domain errors
├── application/ # Use cases / Application services
│ ├── {use-case}/
│ │ ├── command.ts # Command/Query DTOs
│ │ ├── handler.ts # Use case implementation
│ │ └── port.ts # Driver port interface
│ └── shared/
│ └── unit_of_work.ts # Transaction abstraction
├── infrastructure/ # Adapters (external concerns)
│ ├── persistence/
│ │ ├── postgres/ # Database adapter
│ │ └── in_memory/ # Test adapter
│ ├── messaging/
│ │ └── rabbitmq/ # Message broker adapter
│ ├── http/
│ │ └── rest/ # REST API adapter (driver)
│ └── config/
│ └── di.ts # Dependency injection
└── main.ts # Composition root
DDD Building Blocks
| Pattern |
Purpose |
Layer |
| Entity |
Identity + behavior |
Domain |
| Value Object |
Immutable, equality by value |
Domain |
| Aggregate |
Consistency boundary |
Domain |
| Domain Event |
Record of something that happened |
Domain |
| Repository |
Collection-like persistence abstraction |
Domain (interface), Infra (impl) |
| Domain Service |
Stateless business logic |
Domain |
| Application Service |
Orchestrates use cases |
Application |
When Implementing
- Start with the Domain - Model entities, value objects, and aggregates first
- Define Ports - Interfaces for repositories and external services
- Implement Use Cases - Application services orchestrating domain logic
- Add Adapters Last - HTTP controllers, database implementations, etc.
Reference Documentation
For detailed implementation guidance:
- Layer Structure - Complete layer specifications and responsibilities
- DDD Strategic Patterns - Bounded contexts, context mapping, subdomains
- DDD Tactical Patterns - Entities, value objects, aggregates, repositories
- Hexagonal Patterns - Ports, adapters, naming conventions
- Implementation Examples - Code in Go, Rust, Python, TypeScript
- CQRS & Events - Command/query separation, domain events
- Testing Patterns - Unit, integration, and architecture tests
- Cheatsheet - Quick decision guide and patterns
Key Anti-Patterns to Avoid
- Anemic Domain Model - Entities with only getters/setters, logic in services
- Repository per Entity - Repositories are per Aggregate, not per table
- Leaking Infrastructure - Domain importing database/HTTP libraries
- God Aggregates - Aggregates too large; keep them small and focused
- Skipping Ports - Controllers calling repositories directly
- CRUD Thinking - Modeling around data, not behavior
1---2name: clean-ddd-hexagonal-23description: Apply Clean Architecture with Domain-Driven Design and Hexagonal (Ports & Adapters) patterns to backend services. Use when designing APIs, microservices, domain models, repositories, or when user mentions DDD, Clean Architecture, Hexagonal, ports and adapters, bounded contexts, aggregates, or scalable backend structure. Language-agnostic - works with Go, Rust, Python, TypeScript, Java, and any backend language.4---5
6# Clean Architecture + DDD + Hexagonal
7
8## Overview
9
10This skill combines three complementary patterns for building maintainable, testable backend systems:
11
12- **Domain-Driven Design (DDD)** - Strategic and tactical patterns for modeling complex business domains
13- **Clean Architecture** - Dependency rules ensuring business logic independence
14- **Hexagonal Architecture** - Ports & Adapters for external system isolation
15
16**Key Sources:**
17- Clean Architecture: Robert C. Martin (2012) - https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
18- Hexagonal Architecture: Alistair Cockburn (2005) - https://alistair.cockburn.us/hexagonal-architecture/
19- Domain-Driven Design: Eric Evans (2003) - https://www.domainlanguage.com/ddd/
20
21## Core Principles
22
23### The Dependency Rule
24Dependencies point **inward only**. Outer layers depend on inner layers, never the reverse.
25
26```mermaid
27flowchart TB
28 subgraph Infrastructure["Infrastructure (Adapters)"]
29 subgraph Application["Application (Use Cases / Ports)"]
30 subgraph Domain["Domain (Entities, Value Objects, Aggregates)"]
31 Core[" "]
32 end
33 end
34 end
35
36 style Domain fill:#10b981,stroke:#059669,color:white
37 style Application fill:#3b82f6,stroke:#2563eb,color:white
38 style Infrastructure fill:#6366f1,stroke:#4f46e5,color:white
39 style Core fill:#10b981,stroke:#059669
40```
41
42### Hexagonal Ports & Adapters
43- **Driver Ports** (Primary/Inbound): How the world uses your application
44- **Driven Ports** (Secondary/Outbound): How your application uses external systems
45- **Adapters**: Concrete implementations of ports
46
47## Quick Reference Structure
48
49```
50src/
51├── domain/ # Core business logic (no dependencies)
52│ ├── {aggregate}/
53│ │ ├── entity.ts # Aggregate root + entities
54│ │ ├── value_objects.ts # Immutable value types
55│ │ ├── events.ts # Domain events
56│ │ ├── repository.ts # Repository interface (port)
57│ │ └── services.ts # Domain services
58│ └── shared/
59│ └── errors.ts # Domain errors
60├── application/ # Use cases / Application services
61│ ├── {use-case}/
62│ │ ├── command.ts # Command/Query DTOs
63│ │ ├── handler.ts # Use case implementation
64│ │ └── port.ts # Driver port interface
65│ └── shared/
66│ └── unit_of_work.ts # Transaction abstraction
67├── infrastructure/ # Adapters (external concerns)
68│ ├── persistence/
69│ │ ├── postgres/ # Database adapter
70│ │ └── in_memory/ # Test adapter
71│ ├── messaging/
72│ │ └── rabbitmq/ # Message broker adapter
73│ ├── http/
74│ │ └── rest/ # REST API adapter (driver)
75│ └── config/
76│ └── di.ts # Dependency injection
77└── main.ts # Composition root
78```
79
80## DDD Building Blocks
81
82| Pattern | Purpose | Layer |
83|---------|---------|-------|
84| **Entity** | Identity + behavior | Domain |
85| **Value Object** | Immutable, equality by value | Domain |
86| **Aggregate** | Consistency boundary | Domain |
87| **Domain Event** | Record of something that happened | Domain |
88| **Repository** | Collection-like persistence abstraction | Domain (interface), Infra (impl) |
89| **Domain Service** | Stateless business logic | Domain |
90| **Application Service** | Orchestrates use cases | Application |
91
92## When Implementing
93
941. **Start with the Domain** - Model entities, value objects, and aggregates first
952. **Define Ports** - Interfaces for repositories and external services
963. **Implement Use Cases** - Application services orchestrating domain logic
974. **Add Adapters Last** - HTTP controllers, database implementations, etc.
98
99## Reference Documentation
100
101For detailed implementation guidance:
102
103- **[Layer Structure](references/LAYERS.md)** - Complete layer specifications and responsibilities
104- **[DDD Strategic Patterns](references/DDD-STRATEGIC.md)** - Bounded contexts, context mapping, subdomains
105- **[DDD Tactical Patterns](references/DDD-TACTICAL.md)** - Entities, value objects, aggregates, repositories
106- **[Hexagonal Patterns](references/HEXAGONAL.md)** - Ports, adapters, naming conventions
107- **[Implementation Examples](references/IMPLEMENTATION.md)** - Code in Go, Rust, Python, TypeScript
108- **[CQRS & Events](references/CQRS-EVENTS.md)** - Command/query separation, domain events
109- **[Testing Patterns](references/TESTING.md)** - Unit, integration, and architecture tests
110- **[Cheatsheet](references/CHEATSHEET.md)** - Quick decision guide and patterns
111
112## Key Anti-Patterns to Avoid
113
114- **Anemic Domain Model** - Entities with only getters/setters, logic in services
115- **Repository per Entity** - Repositories are per Aggregate, not per table
116- **Leaking Infrastructure** - Domain importing database/HTTP libraries
117- **God Aggregates** - Aggregates too large; keep them small and focused
118- **Skipping Ports** - Controllers calling repositories directly
119- **CRUD Thinking** - Modeling around data, not behavior