ccheney

clean-ddd-hexagonal

2
0
# Install this skill:
npx skills add ccheney/robust-skills --skill "clean-ddd-hexagonal"

Install specific skill from multi-skill repository

# Description

>

# SKILL.md


name: clean-ddd-hexagonal
description: >
Apply Clean Architecture + DDD + Hexagonal patterns to backend services.
Use when designing APIs, microservices, domain models, aggregates, repositories,
bounded contexts, or scalable backend structure. Triggers on DDD, Clean Architecture,
Hexagonal, ports and adapters, entities, value objects, domain events, CQRS,
event sourcing, repository pattern, use cases, onion architecture, outbox pattern,
aggregate root, anti-corruption layer. Language-agnostic (Go, Rust, Python, TypeScript, Java, C#).


Clean Architecture + DDD + Hexagonal

Backend architecture combining DDD tactical patterns, Clean Architecture dependency rules, and Hexagonal ports/adapters for maintainable, testable systems.

When to Use (and When NOT to)

Use When Skip When
Complex business domain with many rules Simple CRUD, few business rules
Long-lived system (years of maintenance) Prototype, MVP, throwaway code
Team of 5+ developers Solo developer or small team (1-2)
Multiple entry points (API, CLI, events) Single entry point, simple API
Need to swap infrastructure (DB, broker) Fixed infrastructure, unlikely to change
High test coverage required Quick scripts, internal tools

Start simple. Evolve complexity only when needed. Most systems don't need full CQRS or Event Sourcing.

CRITICAL: The Dependency Rule

Dependencies point inward only. Outer layers depend on inner layers, never the reverse.

Infrastructure β†’ Application β†’ Domain
   (adapters)     (use cases)    (core)

Violations to catch:
- Domain importing database/HTTP libraries
- Controllers calling repositories directly (bypassing use cases)
- Entities depending on application services

Design validation: "Create your application to work without either a UI or a database" β€” Alistair Cockburn. If you can run your domain logic from tests with no infrastructure, your boundaries are correct.

Quick Decision Trees

"Where does this code go?"

Where does it go?
β”œβ”€ Pure business logic, no I/O           β†’ domain/
β”œβ”€ Orchestrates domain + has side effects β†’ application/
β”œβ”€ Talks to external systems              β†’ infrastructure/
β”œβ”€ Defines HOW to interact (interface)    β†’ port (domain or application)
└─ Implements a port                      β†’ adapter (infrastructure)

"Is this an Entity or Value Object?"

Entity or Value Object?
β”œβ”€ Has unique identity that persists β†’ Entity
β”œβ”€ Defined only by its attributes    β†’ Value Object
β”œβ”€ "Is this THE same thing?"         β†’ Entity (identity comparison)
└─ "Does this have the same value?"  β†’ Value Object (structural equality)

"Should this be its own Aggregate?"

Aggregate boundaries?
β”œβ”€ Must be consistent together in a transaction β†’ Same aggregate
β”œβ”€ Can be eventually consistent                 β†’ Separate aggregates
β”œβ”€ Referenced by ID only                        β†’ Separate aggregates
└─ >10 entities in aggregate                    β†’ Split it

Rule: One aggregate per transaction. Cross-aggregate consistency via domain events (eventual consistency).

Directory Structure

src/
β”œβ”€β”€ domain/                    # Core business logic (NO external dependencies)
β”‚   β”œβ”€β”€ {aggregate}/
β”‚   β”‚   β”œβ”€β”€ entity              # Aggregate root + child entities
β”‚   β”‚   β”œβ”€β”€ value_objects       # Immutable value types
β”‚   β”‚   β”œβ”€β”€ events              # Domain events
β”‚   β”‚   β”œβ”€β”€ repository          # Repository interface (DRIVEN PORT)
β”‚   β”‚   └── services            # Domain services (stateless logic)
β”‚   └── shared/
β”‚       └── errors              # Domain errors
β”œβ”€β”€ application/               # Use cases / Application services
β”‚   β”œβ”€β”€ {use-case}/
β”‚   β”‚   β”œβ”€β”€ command             # Command/Query DTOs
β”‚   β”‚   β”œβ”€β”€ handler             # Use case implementation
β”‚   β”‚   └── port                # Driver port interface
β”‚   └── shared/
β”‚       └── unit_of_work        # Transaction abstraction
β”œβ”€β”€ infrastructure/            # Adapters (external concerns)
β”‚   β”œβ”€β”€ persistence/           # Database adapters
β”‚   β”œβ”€β”€ messaging/             # Message broker adapters
β”‚   β”œβ”€β”€ http/                  # REST/GraphQL adapters (DRIVER)
β”‚   └── config/
β”‚       └── di                  # Dependency injection / composition root
└── main                        # Bootstrap / entry point

DDD Building Blocks

Pattern Purpose Layer Key Rule
Entity Identity + behavior Domain Equality by ID
Value Object Immutable data Domain Equality by value, no setters
Aggregate Consistency boundary Domain Only root is referenced externally
Domain Event Record of change Domain Past tense naming (OrderPlaced)
Repository Persistence abstraction Domain (port) Per aggregate, not per table
Domain Service Stateless logic Domain When logic doesn't fit an entity
Application Service Orchestration Application Coordinates domain + infra

Anti-Patterns (CRITICAL)

Anti-Pattern Problem Fix
Anemic Domain Model Entities are data bags, logic in services Move behavior INTO entities
Repository per Entity Breaks aggregate boundaries One repository per AGGREGATE
Leaking Infrastructure Domain imports DB/HTTP libs Domain has ZERO external deps
God Aggregate Too many entities, slow transactions Split into smaller aggregates
Skipping Ports Controllers β†’ Repositories directly Always go through application layer
CRUD Thinking Modeling data, not behavior Model business operations
Premature CQRS Adding complexity before needed Start with simple read/write, evolve
Cross-Aggregate TX Multiple aggregates in one transaction Use domain events for consistency

Implementation Order

  1. Discover the Domain β€” Event Storming, conversations with domain experts
  2. Model the Domain β€” Entities, value objects, aggregates (no infra)
  3. Define Ports β€” Repository interfaces, external service interfaces
  4. Implement Use Cases β€” Application services coordinating domain
  5. Add Adapters last β€” HTTP, database, messaging implementations

DDD is collaborative. Modeling sessions with domain experts are as important as the code patterns.

Reference Documentation

File Purpose
references/LAYERS.md Complete layer specifications
references/DDD-STRATEGIC.md Bounded contexts, context mapping
references/DDD-TACTICAL.md Entities, value objects, aggregates (pseudocode)
references/HEXAGONAL.md Ports, adapters, naming
references/CQRS-EVENTS.md Command/query separation, events
references/TESTING.md Unit, integration, architecture tests
references/CHEATSHEET.md Quick decision guide

Sources

Primary Sources

Pattern References

Implementation Guides

# Supported AI Coding Agents

This skill is compatible with the SKILL.md standard and works with all major AI coding agents:

Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.