Hexagonal Architecture
Ports and Adapters: business logic independent from frameworks, transport, and persistence. Detalhes em references/.
When to Use
- Building new features where long-term maintainability and testability matter
- Refactoring layered or framework-heavy code with domain logic mixed into I/O
- Supporting multiple interfaces for one use case (HTTP, CLI, queue, cron)
- Replacing infrastructure (DB, APIs, bus) without rewriting business rules
- Requests involving boundaries, decoupling, or domain-centric design
Core Principles
- Dependencies point inward — adapters → application/domain; domain → nothing
- Ports model capabilities —
OrderRepositoryPort, not PostgresClient
- Use cases orchestrate only — mapping stays in adapters at the edge
- One composition root — centralized wiring, no hidden service locators
- Migrate slice-by-slice — strangler + characterization tests, never big-bang
Example
export class CreateOrderUseCase {
constructor(
private readonly orders: OrderRepositoryPort,
private readonly payments: PaymentGatewayPort,
) {}
async execute(input: CreateOrderInput): Promise<CreateOrderOutput> {
const order = Order.create(input);
const auth = await this.payments.authorize(order);
await this.orders.save(order.markAuthorized(auth.authorizationId));
return { orderId: order.id, authorizationId: auth.authorizationId };
}
}
References
references/boundaries.md — concepts, dependency direction, 6 steps, diagram, module layout
references/typescript-example.md — ports, use case, outbound adapter, composition root
references/multilang-migration.md — TS/Java/Kotlin/Go mapping, anti-patterns, migration, testing, practices
Checklist
1---2name: hexagonal-architecture3description: "Use when design, implement, and refactor Ports & Adapters systems with clear domain boundaries, dependency inversion, and testable use-case orchestration across TypeScript, Java, Kotlin, and Go services. Triggers on \"hexagonal-architecture\", \"hexagonal architecture\", \"architecture\"."4metadata:5 source: https://skillmd.com/skills/majinmagros/hexagonal-architecture6 origin: ECC7---89# Hexagonal Architecture1011Ports and Adapters: business logic independent from frameworks, transport, and persistence. Detalhes em `references/`.1213## When to Use1415- Building new features where long-term maintainability and testability matter16- Refactoring layered or framework-heavy code with domain logic mixed into I/O17- Supporting multiple interfaces for one use case (HTTP, CLI, queue, cron)18- Replacing infrastructure (DB, APIs, bus) without rewriting business rules19- Requests involving boundaries, decoupling, or domain-centric design2021## Core Principles22231. **Dependencies point inward** — adapters → application/domain; domain → nothing242. **Ports model capabilities** — `OrderRepositoryPort`, not `PostgresClient`253. **Use cases orchestrate only** — mapping stays in adapters at the edge264. **One composition root** — centralized wiring, no hidden service locators275. **Migrate slice-by-slice** — strangler + characterization tests, never big-bang2829## Example3031```typescript32export class CreateOrderUseCase {33 constructor(34 private readonly orders: OrderRepositoryPort,35 private readonly payments: PaymentGatewayPort,36 ) {}37 async execute(input: CreateOrderInput): Promise<CreateOrderOutput> {38 const order = Order.create(input);39 const auth = await this.payments.authorize(order);40 await this.orders.save(order.markAuthorized(auth.authorizationId));41 return { orderId: order.id, authorizationId: auth.authorizationId };42 }43}44```4546## References4748- `references/boundaries.md` — concepts, dependency direction, 6 steps, diagram, module layout49- `references/typescript-example.md` — ports, use case, outbound adapter, composition root50- `references/multilang-migration.md` — TS/Java/Kotlin/Go mapping, anti-patterns, migration, testing, practices5152## Checklist5354- [ ] Domain imports nothing external; use cases take ports via constructor55- [ ] Every side effect behind an outbound port; mapping in adapters56- [ ] Explicit composition root, auditable in one place57- [ ] Use cases unit-tested with fakes; adapters integration-tested58- [ ] Refactor started from one high-churn, low-blast-radius slice