PluginBench
Skill
Pass
Audit score 90

clean-ddd-hexagonal

ccheney/robust-skills

DDD + Clean Architecture + Hexagonal patterns for maintainable, testable backend services.

What is clean-ddd-hexagonal?

A language-agnostic architectural synthesis combining Domain-Driven Design tactical patterns, Clean Architecture dependency rules, and Hexagonal ports/adapters. Use when designing complex business domains, microservices, or systems requiring high testability and infrastructure flexibility across Go, Rust, Python, TypeScript, Java, or C#.

  • Apply DDD building blocks (entities, value objects, aggregates, domain events, repositories) with clear domain-centric layering
  • Enforce inward-only dependency flow: Infrastructure → Application → Domain core
  • Define ports (interfaces) and adapters to decouple business logic from external systems (databases, APIs, message brokers)
  • Structure use cases as application services that orchestrate domain logic and coordinate side effects
  • Identify aggregate boundaries and transaction consistency requirements using decision trees
  • Detect and prevent anti-patterns: anemic models, leaking infrastructure, god aggregates, premature CQRS

How to install clean-ddd-hexagonal

npx skills add https://github.com/ccheney/robust-skills --skill clean-ddd-hexagonal
Claude Code
Cursor
Windsurf
Cline

How to use clean-ddd-hexagonal

  1. 1.Identify your core business domain and define a ubiquitous language with stakeholders
  2. 2.Map aggregate boundaries using the decision tree: what must be consistent in a single transaction?
  3. 3.Create domain/ folder with aggregates (entities, value objects, events, repository interfaces) with zero external dependencies
  4. 4.Define application/ use cases as orchestrators that call domain logic and coordinate infrastructure adapters
  5. 5.Implement infrastructure/ adapters (database, HTTP, messaging) that depend on domain and application ports
  6. 6.Use the dependency rule validation: ensure your domain logic runs in tests without any infrastructure
  7. 7.Apply the decision trees to place new code (domain vs. application vs. infrastructure) and determine entity vs. value object types
  8. 8.Review against anti-patterns list (anemic models, leaking infrastructure, god aggregates) during code review

Use cases

Good for
  • Designing a multi-tenant SaaS backend with complex order/billing domain and multiple entry points (REST API, CLI, event consumers)
  • Refactoring a monolith to support swappable persistence (PostgreSQL → MongoDB) and message brokers without touching domain logic
  • Building a microservice with strict test coverage where domain logic runs independently of HTTP and database frameworks
  • Establishing bounded contexts and anti-corruption layers when integrating legacy systems with new services
  • Modeling event-sourced workflows (audit trails, temporal queries, replayable operations) with clear event and aggregate design
Who it's for
  • Backend engineers designing APIs or microservices
  • Teams of 5+ developers maintaining long-lived systems
  • Architects defining infrastructure-independent domain models
  • QA and test engineers requiring high testability and clear boundaries
  • Teams adopting DDD, Clean Architecture, or Hexagonal Architecture patterns

clean-ddd-hexagonal FAQ

When should I use CQRS or Event Sourcing?

Only when reads and writes have divergent models or you need temporal queries/audit trails. Start with simple read/write and evolve only if needed. Most systems don't require either by default.

Can I use this with a small team or solo project?

This architecture is optimized for teams of 5+ and long-lived systems. For prototypes, MVPs, or solo work with simple CRUD, start simpler and evolve complexity only when needed.

How do I handle cross-aggregate consistency?

Use domain events for eventual consistency. One aggregate per transaction; if you need multiple aggregates in one transaction, reconsider your aggregate boundaries.

Where should I put repository interfaces—domain or application?

This skill defaults to domain/ (DDD-centered). A stricter Hexagonal layout may place driven ports under application/ports/driven/. Pick one convention per codebase and keep the dependency rule intact.

How do I know if my boundaries are correct?

Validate with Cockburn's test: 'Can your application work without a UI or database?' If domain logic runs in tests with no infrastructure, your boundaries are correct.

Full instructions (SKILL.md)

Source of truth, from ccheney/robust-skills.


name: clean-ddd-hexagonal description: Proactively apply when designing APIs, microservices, 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. Use when working with domain models, aggregates, repositories, or bounded contexts. Clean Architecture + DDD + Hexagonal patterns for backend services, 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.

This skill is an opinionated synthesis of several related architecture traditions. It is not a single canonical architecture model. Use the original source that matches the design question you are answering: DDD for domain modeling, Hexagonal Architecture for ports/adapters, Clean Architecture for dependency direction, Onion Architecture for domain-centered layering, and CQRS/Event Sourcing only for specific read/write or temporal requirements.

When to Use (and When NOT to)

Use WhenSkip When
Complex business domain with many rulesSimple CRUD, few business rules
Long-lived system (years of maintenance)Prototype, MVP, throwaway code
Team of 5+ developersSolo 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 requiredQuick scripts, internal tools

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

Pattern Boundaries

PatternPrimary QuestionUse It ForDo Not Treat As
DDDHow do we model a complex business domain?Ubiquitous language, bounded contexts, aggregates, value objectsA folder structure by itself
Hexagonal ArchitectureHow does the application interact with the outside world?Ports, driver adapters, driven adapters, testable application coreA mandate for six sides or one exact package layout
Clean ArchitectureWhich direction should dependencies point?Inward dependency rule, use case boundaries, framework independenceA universal four-folder template
Onion ArchitectureHow do we keep the domain model central?Domain-centered layers and dependency inversionA separate requirement when Clean/Hexagonal already solve the local problem
CQRSDo reads and writes need different models?Bounded contexts with divergent read/write workloadsA default application architecture
Event SourcingDo we need state from a complete event history?Audit, temporal queries, replayable workflowsA persistence default for CRUD systems

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
  • In this architecture style, controllers calling repositories directly instead of application 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          # DDD 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

Port placement: This skill defaults to a DDD-centered layout where aggregate repository interfaces live beside the aggregate in domain/. A stricter Hexagonal layout may instead put driven ports under application/ports/driven/. Pick one convention per codebase and keep the dependency rule intact.

DDD Building Blocks

PatternPurposeLayerKey Rule
EntityIdentity + behaviorDomainEquality by ID
Value ObjectImmutable dataDomainEquality by value, no setters
AggregateConsistency boundaryDomainOnly root is referenced externally
Domain EventRecord of changeDomainPast tense naming (OrderPlaced)
RepositoryPersistence abstractionDomain (port)Per aggregate, not per table
Domain ServiceStateless logicDomainWhen logic doesn't fit an entity
Application ServiceOrchestrationApplicationCoordinates domain + infra

Anti-Patterns (CRITICAL)

Anti-PatternProblemFix
Anemic Domain ModelEntities are data bags, logic in servicesMove behavior INTO entities
Repository per EntityBreaks aggregate boundariesOne repository per AGGREGATE
Leaking InfrastructureDomain imports DB/HTTP libsDomain has ZERO external deps
God AggregateToo many entities, slow transactionsSplit into smaller aggregates
Skipping Use CasesControllers call repositories directly in a use-case architectureRoute through application use cases
CRUD ThinkingModeling data, not behaviorModel business operations
Premature CQRSAdding complexity before neededStart with simple read/write, evolve
Cross-Aggregate TXMultiple aggregates in one transactionUse 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

FilePurpose
references/LAYERS.mdComplete layer specifications
references/DDD-STRATEGIC.mdBounded contexts, context mapping
references/DDD-TACTICAL.mdEntities, value objects, aggregates (pseudocode)
references/HEXAGONAL.mdPorts, adapters, naming
references/CQRS-EVENTS.mdCommand/query separation, events
references/TESTING.mdUnit, integration, architecture tests
references/CHEATSHEET.mdQuick decision guide

Sources

Primary Sources

Primary Pattern References

Implementation Guides

Supplemental Syntheses

clean-ddd-hexagonal — AI Skill | PluginBench