Get the FREE Ultimate OpenClaw Setup Guide →

architecture-paradigm-hexagonal

npx machina-cli add skill athola/claude-night-market/architecture-paradigm-hexagonal --openclaw
Files (1)
SKILL.md
4.1 KB

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

Source

git clone https://github.com/athola/claude-night-market/blob/master/plugins/archetypes/skills/architecture-paradigm-hexagonal/SKILL.mdView on GitHub

Overview

Hexagonal (Ports & Adapters) isolates core domain logic from infrastructure, enabling swappable adapters and robust testability. It keeps business rules stable as databases, frameworks, or UIs evolve, by defining clear inbound and outbound ports. Adapters live at the edge and never burden the domain with technology details.

How This Skill Works

Define domain ports as formal interfaces: inbound driver ports for actions initiated by users or jobs, and outbound driven ports for services the app consumes. Implement edge adapters that realize these ports for each external technology, keeping the core domain ignorant of frameworks. Enforce dependency rules so only adapters depend on external tech, validated by contract tests to ensure port-adapter compatibility.

When to Use It

  • Isolating business logic from external dependencies
  • Systems needing swappable adapters for testing
  • Anticipating frequent changes to databases, frameworks, or user interfaces while keeping core domain stable
  • Core testing requiring mocking of complex or slow infrastructure components
  • Need for clear inbound and outbound interfaces for third-party integrations

Quick Start

  1. Step 1: Define Domain Ports: identify inbound driver ports and outbound driven ports as formal interfaces
  2. Step 2: Implement Edge Adapters: create adapters for each external technology that implement the ports
  3. Step 3: Organize Use Cases and Enforce Rules: group functionality into domain services and add contract tests to verify adapters

Best Practices

  • Define Domain Ports up front and express them as formal interfaces
  • Keep the core domain ignorant of any specific frameworks or libraries used in adapters
  • Aggregate related functionality into edge-oriented use cases that orchestrate port calls
  • Implement contract tests to verify each adapter honors its port's expectations
  • Enforce that only adapters may depend on external frameworks, with automated checks or architecture tests

Example Use Cases

  • Swapping a database backend (e.g., SQL to NoSQL) without touching domain logic via driven ports and adapters
  • Replacing a web UI with a CLI or API gateway while the domain remains unchanged
  • Introducing a message bus or external API through dedicated adapters validated by contract tests
  • Mocking slow infrastructure components during unit or integration tests using defined ports
  • Maintaining an ADR (Architecture Decision Record) that names ports, adapters, and dependency policies

Frequently Asked Questions

Add this skill to your agents

Related Skills

Sponsor this space

Reach thousands of developers