Hexagonal Architecture
Hexagonal architecture (Ports and Adapters) keeps business logic independent from frameworks, transport, and persistence
details. The core app depends on abstract ports, and adapters implement those ports at the edges.
This skill is the architecture authority the coding-standards hub defers to for its Architecture section.
Language baselines the examples assume, current as of September 2026: TypeScript 5 on Node.js 24 LTS, Java 21 LTS,
Kotlin 2.2, and Go 1.25.
When to activate
- Building new features where long-term maintainability and testability matter.
- Refactoring layered or framework-heavy code where domain logic is mixed with I/O concerns.
- Supporting multiple interfaces for the same use case (HTTP, CLI, queue workers, cron jobs).
- Replacing infrastructure (database, external APIs, message bus) without rewriting business rules.
Use this skill when the request involves boundaries, domain-centric design, refactoring tightly coupled services, or
decoupling application logic from specific libraries.
When not to activate
- The HTTP or GraphQL contract at the edge: use
api-design.
- Runtime-specific handler, service, and repository code: use
node-backend-patterns or springboot-patterns.
- Language-neutral service concerns such as idempotency, retries, and outbox: use
backend-patterns.
- Schema, index, and query work behind the outbound adapter: use
postgres-patterns or springboot-patterns.
- The shared engineering floor of SOLID, naming, and error handling: use
coding-standards.
- A one-off script or a prototype whose whole lifetime is a week. The indirection costs more than it returns.
Reference map
| Task |
Open |
| A complete vertical slice in TypeScript, from port to composition root |
references/typescript-example.md |
Core Concepts
- Domain model: Business rules and entities/value objects. No framework imports.
- Use cases (application layer): Orchestrate domain behavior and workflow steps.
- Inbound ports: Contracts describing what the application can do (commands/queries/use-case interfaces).
- Outbound ports: Contracts for dependencies the application needs (repositories, gateways, event publishers, clock,
UUID, etc.).
- Adapters: Infrastructure and delivery implementations of ports (HTTP controllers, DB repositories, queue consumers,
SDK wrappers).
- Composition root: Single wiring location where concrete adapters are bound to use cases.
Outbound port interfaces usually live in the application layer (or in domain only when the abstraction is truly
domain-level), while infrastructure adapters implement them.
Dependency direction is always inward:
- Adapters -> application/domain
- Application -> port interfaces (inbound/outbound contracts)
- Domain -> domain-only abstractions (no framework or infrastructure dependencies)
- Domain -> nothing external
How It Works
Step 1: Model a use case boundary
Define a single use case with a clear input and output DTO. Keep transport details (Express req, GraphQL context,
job payload wrappers) outside this boundary.
Step 2: Define outbound ports first
Identify every side effect as a port:
- persistence (
UserRepositoryPort)
- external calls (
BillingGatewayPort)
- cross-cutting (
LoggerPort, ClockPort)
Ports should model capabilities, not technologies.
Step 3: Implement the use case with pure orchestration
Use case class/function receives ports via constructor/arguments. It validates application-level invariants, coordinates
domain rules, and returns plain data structures.
Step 4: Build adapters at the edge
- Inbound adapter converts protocol input to use-case input.
- Outbound adapter maps app contracts to concrete APIs/ORM/query builders.
- Mapping stays in adapters, not inside use cases.
Step 5: Wire everything in a composition root
Instantiate adapters, then inject them into use cases. Keep this wiring centralized to avoid hidden service-locator
behavior.
Step 6: Test per boundary
- Unit test use cases with fake ports.
- Integration test adapters with real infra dependencies.
- E2E test user-facing flows through inbound adapters.
Architecture Diagram
flowchart LR
Client["Client (HTTP/CLI/Worker)"] --> InboundAdapter["Inbound Adapter"]
InboundAdapter -->|"calls"| UseCase["UseCase (Application Layer)"]
UseCase -->|"uses"| OutboundPort["OutboundPort (Interface)"]
OutboundAdapter["Outbound Adapter"] -->|"implements"| OutboundPort
OutboundAdapter --> ExternalSystem["DB/API/Queue"]
UseCase --> DomainModel["DomainModel"]
Suggested Module Layout
Use feature-first organization with explicit boundaries:
src/
features/
orders/
domain/
Order.ts
OrderPolicy.ts
application/
ports/
inbound/
CreateOrder.ts
outbound/
OrderRepositoryPort.ts
PaymentGatewayPort.ts
use-cases/
CreateOrderUseCase.ts
adapters/
inbound/
http/
createOrderRoute.ts
outbound/
postgres/
PostgresOrderRepository.ts
stripe/
StripePaymentGateway.ts
composition/
ordersContainer.ts
Multi-Language Mapping
Use the same boundary rules across ecosystems. Only the syntax and the wiring style change.
- TypeScript/JavaScript
- Ports:
application/ports/* as interfaces/types.
- Use cases: classes/functions with constructor/argument injection.
- Adapters:
adapters/inbound/*, adapters/outbound/*.
- Composition: explicit factory/container module (no hidden globals).
- Java
- Packages:
domain, application.port.in, application.port.out, application.usecase, adapter.in,
adapter.out.
- Ports: interfaces in
application.port.*.
- Use cases: plain classes (Spring
@Service is optional, not required).
- Composition: Spring config or a manual wiring class, keeping wiring out of domain and use-case classes.
- Kotlin
- Modules/packages mirror the Java split (
domain, application.port, application.usecase, adapter).
- Ports: Kotlin interfaces.
- Use cases: classes with constructor injection (Koin/Dagger/Spring/manual).
- Composition: module definitions or dedicated composition functions, never a service locator.
- Go
- Packages:
internal/<feature>/domain, application, ports, adapters/inbound, adapters/outbound.
- Ports: small interfaces owned by the consuming application package.
- Use cases: structs with interface fields plus explicit
New... constructors.
- Composition: wire in
cmd/<app>/main.go (or dedicated wiring package), keep constructors explicit.
Anti-Patterns to Avoid
- Domain entities importing ORM models, web framework types, or SDK clients.
- Use cases reading directly from
req, res, or queue metadata.
- Returning database rows directly from use cases without domain/application mapping.
- Letting adapters call each other directly instead of flowing through use-case ports.
- Spreading dependency wiring across many files with hidden global singletons.
Migration Playbook
- Pick one vertical slice (single endpoint/job) with frequent change pain.
- Extract a use-case boundary with explicit input/output types.
- Introduce outbound ports around existing infrastructure calls.
- Move orchestration logic from controllers/services into the use case.
- Keep old adapters, but make them delegate to the new use case.
- Add tests around the new boundary (unit + adapter integration).
- Repeat slice by slice, and avoid full rewrites.
Refactoring Existing Systems
- Strangler approach: keep current endpoints, route one use case at a time through new ports/adapters.
- No big-bang rewrites: migrate per feature slice and preserve behavior with characterization tests.
- Facade first: wrap legacy services behind outbound ports before replacing internals.
- Composition freeze: centralize wiring early so new dependencies do not leak into domain/use-case layers.
- Slice selection rule: prioritize high-churn, low-blast-radius flows first.
- Rollback path: keep a reversible toggle or route switch per migrated slice until production behavior is verified.
Testing Guidance (Same Hexagonal Boundaries)
- Domain tests: test entities/value objects as pure business rules (no mocks, no framework setup).
- Use-case unit tests: test orchestration with fakes or stubs for outbound ports, asserting business outcomes and port
interactions.
- Outbound adapter contract tests: define shared contract suites at port level and run them against each adapter
implementation.
- Inbound adapter tests: verify protocol mapping (HTTP/CLI/queue payload to use-case input and output/error mapping back
to protocol).
- Adapter integration tests: run against real infrastructure (DB/API/queue) for serialization, schema/query behavior,
retries, and timeouts.
- End-to-end tests: cover critical user journeys through inbound adapter -> use case -> outbound adapter.
- Refactor safety: add characterization tests before extraction, and keep them until the new boundary behavior is
equivalent.
Related skills
coding-standards for the shared engineering floor this skill supplies the architecture section of.
api-design for the contract the inbound adapter exposes.
backend-patterns for idempotency, retries, and outbox behaviour inside the application layer.
node-backend-patterns, springboot-patterns, python-patterns, and golang-patterns for the adapter code.
tdd-workflow for the test discipline that makes the boundaries worth having.
Checklist
- Domain and use-case layers import only internal types and ports.
- Every external dependency is represented by an outbound port.
- Validation occurs at boundaries (inbound adapter + use-case invariants).
- Use immutable transformations (return new values/entities instead of mutating shared state).
- Errors are translated across boundaries (infra errors -> application/domain errors).
- Composition root is explicit and easy to audit.
- Use cases are testable with simple in-memory fakes for ports.
- Refactoring starts from one vertical slice with behavior-preserving tests.
- Language/framework specifics stay in adapters, never in domain rules.
1---2name: hexagonal-architecture3description: Ports and adapters design covering domain and use-case boundaries, inbound and outbound ports, dependency inversion, adapters, the composition root, and per-boundary testing in TypeScript, Java, Kotlin and Go. Use when you say "restructure this around ports and adapters", "my domain imports the ORM", "test this use case without a database", or "swap this payment provider". Not for HTTP contract design, use `api-design`.4---56# Hexagonal Architecture78Hexagonal architecture (Ports and Adapters) keeps business logic independent from frameworks, transport, and persistence9details. The core app depends on abstract ports, and adapters implement those ports at the edges.1011This skill is the architecture authority the `coding-standards` hub defers to for its Architecture section.1213Language baselines the examples assume, current as of September 2026: TypeScript 5 on Node.js 24 LTS, Java 21 LTS,14Kotlin 2.2, and Go 1.25.1516---1718### When to activate1920- Building new features where long-term maintainability and testability matter.21- Refactoring layered or framework-heavy code where domain logic is mixed with I/O concerns.22- Supporting multiple interfaces for the same use case (HTTP, CLI, queue workers, cron jobs).23- Replacing infrastructure (database, external APIs, message bus) without rewriting business rules.2425Use this skill when the request involves boundaries, domain-centric design, refactoring tightly coupled services, or26decoupling application logic from specific libraries.2728---2930### When not to activate3132- The HTTP or GraphQL contract at the edge: use `api-design`.33- Runtime-specific handler, service, and repository code: use `node-backend-patterns` or `springboot-patterns`.34- Language-neutral service concerns such as idempotency, retries, and outbox: use `backend-patterns`.35- Schema, index, and query work behind the outbound adapter: use `postgres-patterns` or `springboot-patterns`.36- The shared engineering floor of SOLID, naming, and error handling: use `coding-standards`.37- A one-off script or a prototype whose whole lifetime is a week. The indirection costs more than it returns.3839---4041### Reference map4243| Task | Open |44| --- | --- |45| A complete vertical slice in TypeScript, from port to composition root | [references/typescript-example.md](references/typescript-example.md) |4647---4849### Core Concepts5051- Domain model: Business rules and entities/value objects. No framework imports.52- Use cases (application layer): Orchestrate domain behavior and workflow steps.53- Inbound ports: Contracts describing what the application can do (commands/queries/use-case interfaces).54- Outbound ports: Contracts for dependencies the application needs (repositories, gateways, event publishers, clock,55 UUID, etc.).56- Adapters: Infrastructure and delivery implementations of ports (HTTP controllers, DB repositories, queue consumers,57 SDK wrappers).58- Composition root: Single wiring location where concrete adapters are bound to use cases.5960Outbound port interfaces usually live in the application layer (or in domain only when the abstraction is truly61domain-level), while infrastructure adapters implement them.6263Dependency direction is always inward:6465- Adapters -> application/domain66- Application -> port interfaces (inbound/outbound contracts)67- Domain -> domain-only abstractions (no framework or infrastructure dependencies)68- Domain -> nothing external6970---7172### How It Works7374#### Step 1: Model a use case boundary7576Define a single use case with a clear input and output DTO. Keep transport details (Express `req`, GraphQL `context`,77job payload wrappers) outside this boundary.7879#### Step 2: Define outbound ports first8081Identify every side effect as a port:8283- persistence (`UserRepositoryPort`)84- external calls (`BillingGatewayPort`)85- cross-cutting (`LoggerPort`, `ClockPort`)8687Ports should model capabilities, not technologies.8889#### Step 3: Implement the use case with pure orchestration9091Use case class/function receives ports via constructor/arguments. It validates application-level invariants, coordinates92domain rules, and returns plain data structures.9394#### Step 4: Build adapters at the edge9596- Inbound adapter converts protocol input to use-case input.97- Outbound adapter maps app contracts to concrete APIs/ORM/query builders.98- Mapping stays in adapters, not inside use cases.99100#### Step 5: Wire everything in a composition root101102Instantiate adapters, then inject them into use cases. Keep this wiring centralized to avoid hidden service-locator103behavior.104105#### Step 6: Test per boundary106107- Unit test use cases with fake ports.108- Integration test adapters with real infra dependencies.109- E2E test user-facing flows through inbound adapters.110111---112113### Architecture Diagram114115```mermaid116flowchart LR117 Client["Client (HTTP/CLI/Worker)"] --> InboundAdapter["Inbound Adapter"]118 InboundAdapter -->|"calls"| UseCase["UseCase (Application Layer)"]119 UseCase -->|"uses"| OutboundPort["OutboundPort (Interface)"]120 OutboundAdapter["Outbound Adapter"] -->|"implements"| OutboundPort121 OutboundAdapter --> ExternalSystem["DB/API/Queue"]122 UseCase --> DomainModel["DomainModel"]123```124125---126127### Suggested Module Layout128129Use feature-first organization with explicit boundaries:130131```text132src/133 features/134 orders/135 domain/136 Order.ts137 OrderPolicy.ts138 application/139 ports/140 inbound/141 CreateOrder.ts142 outbound/143 OrderRepositoryPort.ts144 PaymentGatewayPort.ts145 use-cases/146 CreateOrderUseCase.ts147 adapters/148 inbound/149 http/150 createOrderRoute.ts151 outbound/152 postgres/153 PostgresOrderRepository.ts154 stripe/155 StripePaymentGateway.ts156 composition/157 ordersContainer.ts158```159160---161162### Multi-Language Mapping163164Use the same boundary rules across ecosystems. Only the syntax and the wiring style change.165166- TypeScript/JavaScript167 - Ports: `application/ports/*` as interfaces/types.168 - Use cases: classes/functions with constructor/argument injection.169 - Adapters: `adapters/inbound/*`, `adapters/outbound/*`.170 - Composition: explicit factory/container module (no hidden globals).171- Java172 - Packages: `domain`, `application.port.in`, `application.port.out`, `application.usecase`, `adapter.in`,173 `adapter.out`.174 - Ports: interfaces in `application.port.*`.175 - Use cases: plain classes (Spring `@Service` is optional, not required).176 - Composition: Spring config or a manual wiring class, keeping wiring out of domain and use-case classes.177- Kotlin178 - Modules/packages mirror the Java split (`domain`, `application.port`, `application.usecase`, `adapter`).179 - Ports: Kotlin interfaces.180 - Use cases: classes with constructor injection (Koin/Dagger/Spring/manual).181 - Composition: module definitions or dedicated composition functions, never a service locator.182- Go183 - Packages: `internal/<feature>/domain`, `application`, `ports`, `adapters/inbound`, `adapters/outbound`.184 - Ports: small interfaces owned by the consuming application package.185 - Use cases: structs with interface fields plus explicit `New...` constructors.186 - Composition: wire in `cmd/<app>/main.go` (or dedicated wiring package), keep constructors explicit.187188---189190### Anti-Patterns to Avoid191192- Domain entities importing ORM models, web framework types, or SDK clients.193- Use cases reading directly from `req`, `res`, or queue metadata.194- Returning database rows directly from use cases without domain/application mapping.195- Letting adapters call each other directly instead of flowing through use-case ports.196- Spreading dependency wiring across many files with hidden global singletons.197198---199200### Migration Playbook2012021. Pick one vertical slice (single endpoint/job) with frequent change pain.2032. Extract a use-case boundary with explicit input/output types.2043. Introduce outbound ports around existing infrastructure calls.2054. Move orchestration logic from controllers/services into the use case.2065. Keep old adapters, but make them delegate to the new use case.2076. Add tests around the new boundary (unit + adapter integration).2087. Repeat slice by slice, and avoid full rewrites.209210#### Refactoring Existing Systems211212- Strangler approach: keep current endpoints, route one use case at a time through new ports/adapters.213- No big-bang rewrites: migrate per feature slice and preserve behavior with characterization tests.214- Facade first: wrap legacy services behind outbound ports before replacing internals.215- Composition freeze: centralize wiring early so new dependencies do not leak into domain/use-case layers.216- Slice selection rule: prioritize high-churn, low-blast-radius flows first.217- Rollback path: keep a reversible toggle or route switch per migrated slice until production behavior is verified.218219---220221### Testing Guidance (Same Hexagonal Boundaries)222223- Domain tests: test entities/value objects as pure business rules (no mocks, no framework setup).224- Use-case unit tests: test orchestration with fakes or stubs for outbound ports, asserting business outcomes and port225 interactions.226- Outbound adapter contract tests: define shared contract suites at port level and run them against each adapter227 implementation.228- Inbound adapter tests: verify protocol mapping (HTTP/CLI/queue payload to use-case input and output/error mapping back229 to protocol).230- Adapter integration tests: run against real infrastructure (DB/API/queue) for serialization, schema/query behavior,231 retries, and timeouts.232- End-to-end tests: cover critical user journeys through inbound adapter -> use case -> outbound adapter.233- Refactor safety: add characterization tests before extraction, and keep them until the new boundary behavior is234 equivalent.235236---237238### Related skills239240- `coding-standards` for the shared engineering floor this skill supplies the architecture section of.241- `api-design` for the contract the inbound adapter exposes.242- `backend-patterns` for idempotency, retries, and outbox behaviour inside the application layer.243- `node-backend-patterns`, `springboot-patterns`, `python-patterns`, and `golang-patterns` for the adapter code.244- `tdd-workflow` for the test discipline that makes the boundaries worth having.245246---247248### Checklist249250- Domain and use-case layers import only internal types and ports.251- Every external dependency is represented by an outbound port.252- Validation occurs at boundaries (inbound adapter + use-case invariants).253- Use immutable transformations (return new values/entities instead of mutating shared state).254- Errors are translated across boundaries (infra errors -> application/domain errors).255- Composition root is explicit and easy to audit.256- Use cases are testable with simple in-memory fakes for ports.257- Refactoring starts from one vertical slice with behavior-preserving tests.258- Language/framework specifics stay in adapters, never in domain rules.