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.
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.
When to Use
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.
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).
Related Skills
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.