home / skills / athola / claude-night-market / architecture-paradigm-hexagonal

This skill helps you apply the Hexagonal architecture pattern to isolate domain logic from infrastructure, improving testability and adaptability.

npx playbooks add skill athola/claude-night-market --skill architecture-paradigm-hexagonal

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
4.1 KB
---
name: architecture-paradigm-hexagonal
description: 'Decouple domain logic from infrastructure using Hexagonal (Ports & Adapters)
  pattern.


  Triggers: hexagonal, ports-adapters, infrastructure-independence, domain-isolation,
  testability

  Use when: business logic separation, infrastructure changes needed, testability
  critical

  DO NOT use when: selecting paradigms (use architecture-paradigms first), simple CRUD.'
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 Use

- Isolating business logic from external dependencies
- Systems needing swappable adapters for testing

## When NOT To Use

- Small scripts or utilities without external dependencies
- Prototypes where port/adapter abstraction adds overhead

## 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
1. **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.
2. **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.
3. **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.
4. **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.
5. **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

Overview

This skill helps teams decouple domain logic from infrastructure using the Hexagonal (Ports & Adapters) pattern. It guides defining ports, implementing adapters at the edge, and enforcing dependency rules so business rules remain framework-agnostic. The result is clearer boundaries, easier testing, and swappable infrastructure components.

How this skill works

The skill inspects your application design to identify inbound (driver) and outbound (driven) interactions and recommends explicit port interfaces for the core domain. It suggests adapter placements for each external technology, prescribes contract and unit test strategies, and advises on dependency enforcement and architecture artifacts like ADRs and diagrams. It flags common risks (interface bloat, leaky abstractions, adapter drift) and provides mitigation steps.

When to use it

  • When you need to isolate business logic from databases, UIs, or third-party APIs
  • When you expect frequent infrastructure changes or multiple concrete implementations
  • When testability is critical and you want fast unit tests without heavy integration setup
  • When providing clear inbound/outbound contracts for third-party integrators
  • When building long-lived services where maintainability and evolution matter

Best practices

  • Define inbound (driver) and outbound (driven) ports as small, domain-centric interfaces stored with the core domain
  • Group related operations into cohesive higher-level ports to avoid interface bloat
  • Implement adapters only at the edges; never add framework dependencies to the domain
  • Write contract tests for each adapter to validate it honors the port contract
  • Use automated architecture tests or static analysis to enforce dependency rules
  • Represent data across ports with domain DTOs to prevent transport-specific leaks

Example use cases

  • A marketplace service where payment, search, and persistence implementations may be swapped without touching domain logic
  • Migrating from one database or ORM to another while keeping business rules unchanged
  • Automating tests that mock slow external services (payment gateways, message buses) using driven port fakes
  • Providing a stable API to third-party integrators while evolving transport protocols behind driver ports
  • Creating multiple UI clients (web, mobile, CLI) that reuse the same domain through driver ports

FAQ

Will Hexagonal add overhead to small projects?

Yes. For small scripts or simple CRUD apps, the port/adapter indirection can be unnecessary. Use Hexagonal when infrastructure complexity or longevity justifies the cost.

How do I avoid port/interface bloat?

Group related operations into cohesive ports aligned to domain aggregates, and prefer fewer higher-level contracts over many fine-grained methods. Refactor ports when they start mirroring transport details.