name: architecture-paradigm-hexagonal
description: |
Triggers: hexagonal, architecture, ports-adapters, testability, infrastructure-independence
Decouple domain logic from infrastructure using Hexagonal (Ports & Adapters) pattern.
Triggers: hexagonal, ports-adapters, infrastructure-independence, domain-isolation
Use when: business logic separation, infrastructure changes needed, testability critical
DO NOT use when: selecting paradigms (use architecture-paradigms first), simple CRUD
version: 1.3.7
category: architectural-pattern
tags: [architecture, hexagonal, ports-adapters, infrastructure-independence, testability]
dependencies: []
tools: [boundary-validator, adapter-generator, contract-tester]
usage_patterns:
- paradigm-implementation
- refactoring-guidance
- adr-support
- infrastructure-migration
complexity: intermediate
estimated_tokens: 1200
The Hexagonal (Ports & Adapters) Paradigm
When to Employ This Paradigm
- When you anticipate frequent changes to databases, frameworks, or user interfaces and need the core domain logic to remain stable.
- When testing the core application requires mocking complex or slow infrastructure components.
- When the development team needs to provide clear inbound and outbound interfaces for third-party integrations.
Adoption Steps
- Define Domain Ports: Identify all interactions with the core domain. Define inbound "driver ports" for actors that initiate actions (e.g., UI, CLI, automated jobs) and outbound "driven ports" for services the application consumes (e.g., database, message bus, external APIs). Express these ports as formal interfaces.
- Implement Adapters at the Edge: For each external technology, create an "adapter" that implements a port's interface. Keep the core domain entirely ignorant of the specific frameworks or libraries used in the adapters.
- Aggregate Use Cases: Organize the application's functionality into services that are built around business capabilities. These services orchestrate calls to the domain through the defined ports.
- Implement Contract Testing: validate that each adapter correctly honors the expectations of the port it implements. Use contract tests or consumer-driven contract tests to validate this behavior.
- Enforce Dependency Rules: The most critical rule is that only adapters may have dependencies on external frameworks. Enforce this with automated architecture tests or static analysis rules.
Key Deliverables
- An Architecture Decision Record (ADR) that formally names the ports, their corresponding adapters, and the dependency policies.
- A set of port interface definitions, stored with the core domain module.
- A suite of contract tests for each adapter, alongside unit tests for the domain and application services.
- Architectural diagrams showing the inbound and outbound data flows, for use by operations teams and architecture reviewers.
Risks & Mitigations
- Port/Interface Bloat:
- Mitigation: An excessive number of ports can increase maintenance overhead. Group related operations into more cohesive, higher-level ports, often organized around domain aggregates.
- Leaky Abstractions:
- Mitigation: If a port's interface exposes details about the transport layer (e.g., HTTP headers), it is a "leaky abstraction." Refactor these interfaces to use domain-centric Data Transfer Objects (DTOs) instead.
- Adapter Drift:
- Mitigation: An adapter can become out-of-sync with the external technology it represents (e.g., database schema changes). Schedule regular, automated validation of adapters, such as verifying that SQL migrations still align with the expectations of the persistence port.
Troubleshooting
Common Issues
Command not found
Ensure all dependencies are installed and in PATH
Permission errors
Check file permissions and run with appropriate privileges
Unexpected behavior
Enable verbose logging with --verbose flag
1---2name: architecture-paradigm-hexagonal3description: - When you anticipate frequent changes to databases, frameworks, or user interfaces and need the core domain logic to remain stable.4---5
6---
7name: architecture-paradigm-hexagonal
8description: |
9
10Triggers: hexagonal, architecture, ports-adapters, testability, infrastructure-independence
11 Decouple domain logic from infrastructure using Hexagonal (Ports & Adapters) pattern.
12
13 Triggers: hexagonal, ports-adapters, infrastructure-independence, domain-isolation
14
15 Use when: business logic separation, infrastructure changes needed, testability critical
16 DO NOT use when: selecting paradigms (use architecture-paradigms first), simple CRUD
17version: 1.3.7
18category: architectural-pattern
19tags: [architecture, hexagonal, ports-adapters, infrastructure-independence, testability]
20dependencies: []
21tools: [boundary-validator, adapter-generator, contract-tester]
22usage_patterns:
23 - paradigm-implementation
24 - refactoring-guidance
25 - adr-support
26 - infrastructure-migration
27complexity: intermediate
28estimated_tokens: 1200
29---
30
31# The Hexagonal (Ports & Adapters) Paradigm
32
33## When to Employ This Paradigm
34- When you anticipate frequent changes to databases, frameworks, or user interfaces and need the core domain logic to remain stable.
35- When testing the core application requires mocking complex or slow infrastructure components.
36- When the development team needs to provide clear inbound and outbound interfaces for third-party integrations.
37
38## Adoption Steps
391. **Define Domain Ports**: Identify all interactions with the core domain. Define inbound "driver ports" for actors that initiate actions (e.g., UI, CLI, automated jobs) and outbound "driven ports" for services the application consumes (e.g., database, message bus, external APIs). Express these ports as formal interfaces.
402. **Implement Adapters at the Edge**: For each external technology, create an "adapter" that implements a port's interface. Keep the core domain entirely ignorant of the specific frameworks or libraries used in the adapters.
413. **Aggregate Use Cases**: Organize the application's functionality into services that are built around business capabilities. These services orchestrate calls to the domain through the defined ports.
424. **Implement Contract Testing**: validate that each adapter correctly honors the expectations of the port it implements. Use contract tests or consumer-driven contract tests to validate this behavior.
435. **Enforce Dependency Rules**: The most critical rule is that only adapters may have dependencies on external frameworks. Enforce this with automated architecture tests or static analysis rules.
44
45## Key Deliverables
46- An Architecture Decision Record (ADR) that formally names the ports, their corresponding adapters, and the dependency policies.
47- A set of port interface definitions, stored with the core domain module.
48- A suite of contract tests for each adapter, alongside unit tests for the domain and application services.
49- Architectural diagrams showing the inbound and outbound data flows, for use by operations teams and architecture reviewers.
50
51## Risks & Mitigations
52- **Port/Interface Bloat**:
53 - **Mitigation**: An excessive number of ports can increase maintenance overhead. Group related operations into more cohesive, higher-level ports, often organized around domain aggregates.
54- **Leaky Abstractions**:
55 - **Mitigation**: If a port's interface exposes details about the transport layer (e.g., HTTP headers), it is a "leaky abstraction." Refactor these interfaces to use domain-centric Data Transfer Objects (DTOs) instead.
56- **Adapter Drift**:
57 - **Mitigation**: An adapter can become out-of-sync with the external technology it represents (e.g., database schema changes). Schedule regular, automated validation of adapters, such as verifying that SQL migrations still align with the expectations of the persistence port.
58## Troubleshooting
59
60### Common Issues
61
62**Command not found**
63Ensure all dependencies are installed and in PATH
64
65**Permission errors**
66Check file permissions and run with appropriate privileges
67
68**Unexpected behavior**
69Enable verbose logging with `--verbose` flag