agentleFS
kayamankayaman/skills/hexagonal-architecture/SKILL.md

hexagonal-architecture

Skill3 starsChanged 8 months agoCC0-1.0View on GitHub

Enforces Hexagonal Architecture (Ports and Adapters) principles including driving and driven ports, adapter separation, and technology-agnostic core design. Use when structuring application boundaries, defining port interfaces, implementing adapters for databases or external APIs, testing business logic in isolation, or comparing hexagonal with onion and clean architecture approaches.

24 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use layered-architecture.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • dkprogSkill

    layered-architecture

    Apply when structuring a backend service, creating new modules, or deciding where to place business logic. Ensures separation into transport, domain, and data access layers with correct dependency direction.

    Also in 3 projects that use this

The file

Show its rules and text

We split this skill into the rules it gives an agent

21 rules. 0 are common, 17 are distinctive, and 4 are very specific to this project.

  • 0

    Common

    Most of the 23 edited versions we read keep these.

  • 17

    Distinctive

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    SHOULD be named by capability: ForPlacingOrders, ForManagingInventory, ForQueryingReports

    line 41
  2. Very specific:

    SHOULD be named by need: ForStoringOrders, ForSendingNotifications, ForFetchingExchangeRates

    line 49
  3. Distinctive:

    MUST define port interfaces in the application core, not in the adapter layer

    line 53
  4. Very specific:

    MUST express ports in domain language, not technology language (ForStoringOrders not DatabasePort)

    line 54
  5. Distinctive:

    MUST NOT include technology-specific types in port signatures (no SQL types, HTTP objects, or ORM entities)

    line 55
  6. Distinctive:

    SHOULD keep ports focused — one port per actor interaction concern

    line 56
  7. Distinctive:

    MUST implement exactly one port interface per adapter (adapters are single-purpose)

    line 90
  8. Distinctive:

    MUST NOT contain business logic — only translation and delegation

    line 91
  9. Distinctive:

    MUST NOT be referenced by the application core — the core knows only the port interface

    line 92
  10. Very specific:

    SHOULD be swappable: replacing PostgresOrderRepository with MongoOrderRepository requires no core changes

    line 93
  11. Distinctive:

    MUST NOT import any framework, database, or HTTP library in the core

    line 113
  12. Distinctive:

    MUST NOT reference any adapter class from within the core

    line 114
  13. Distinctive:

    SHOULD separate domain model from application services (domain contains pure business rules.

    line 115
  14. Distinctive:

    MUST write tests against port interfaces, not adapter implementations

    line 132
  15. Distinctive:

    SHOULD start with in-memory adapters during development.

    line 133
  16. Distinctive:

    SHOULD use the test harness as a first-class driving adapter — it validates the entire core without infrastructure

    line 134
  17. Distinctive:

    All three share the same DNA: business logic at center, dependencies point inward, DIP at architectural level.

    line 140
  18. Distinctive:

    SHOULD choose hexagonal when the team values simplicity and flexibility in internal core organization

    line 149
  19. Distinctive:

    SHOULD choose clean architecture when the team benefits from prescriptive layer naming and component principles

    line 150
  20. Distinctive:

    All three are valid; the important invariant is dependencies point inward

    line 151
  21. Distinctive:

    When designing or reviewing a hexagonal architecture, verify:

    line 170

“Common” is measured: kept by at least 6 in 10 of the 23 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Hexagonal Architecture (Ports and Adapters)

Reference: Hexagonal Architecture Explained (Cockburn & Garrido de Paz), Get Your Hands Dirty on Clean Architecture (Hombergs)

When to Apply

  • Structuring an application so business logic is independent of delivery mechanism and infrastructure
  • Defining port interfaces for external system interactions
  • Implementing adapters for databases, message queues, REST APIs, or CLI
  • Testing business logic in isolation from infrastructure
  • Deferring technology decisions (database, framework, messaging) to the last reasonable moment
  • Migrating a tightly coupled application toward decoupled architecture

Core Concept

Alistair Cockburn published the pattern in 2005 (roots back to 1994). The hexagon shape was chosen to break away from layered stack diagrams — it is not about the number six.

Core intent: "Allow an application to equally be driven by users, programs, automated tests, or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases."

Fundamental rule: The application core defines ALL port interfaces. Adapters depend on ports. The core MUST NOT depend on adapters.


Ports

Ports are interfaces belonging to the application core — they are NOT a separate layer.

Driving (Primary) Ports

Interfaces the application exposes for external actors to call INTO the application.

  • Define the application's capabilities from the outside world's perspective
  • Connected to primary actors: users, other systems, tests, batch jobs
  • SHOULD be named by capability: ForPlacingOrders, ForManagingInventory, ForQueryingReports

Driven (Secondary) Ports

Interfaces the application requires from external systems — what the application needs but does not implement.

  • Define what services the application expects the outside world to provide
  • Connected to secondary actors: databases, message queues, external APIs, file systems
  • SHOULD be named by need: ForStoringOrders, ForSendingNotifications, ForFetchingExchangeRates

Port Design Rules

  • MUST define port interfaces in the application core, not in the adapter layer
  • MUST express ports in domain language, not technology language (ForStoringOrders not DatabasePort)
  • MUST NOT include technology-specific types in port signatures (no SQL types, HTTP objects, or ORM entities)
  • SHOULD keep ports focused — one port per actor interaction concern

Adapters

Adapters are concrete implementations that connect ports to the outside world.

Primary (Driving) Adapters

Translate external input into calls on driving ports. They call INTO the application.

Adapter TypeExampleTranslates From
REST ControllerOrderControllerHTTP request → PlaceOrderCommand
CLI HandlerImportCliCommand-line args → ImportDataCommand
Message ConsumerOrderEventListenerQueue message → ProcessPaymentCommand
Test HarnessOrderAcceptanceTestTest setup → direct port method calls
GraphQL ResolverOrderResolverGraphQL query → port method call

Secondary (Driven) Adapters

Implement driven port interfaces. The application calls THROUGH them.

Adapter TypeExampleConnects To
SQL RepositoryPostgresOrderRepositoryDatabase via SQL/ORM
REST ClientExchangeRateApiClientExternal HTTP API
Message PublisherRabbitMqNotificationSenderMessage broker
File StorageS3DocumentStoreObject storage
In-Memory StubInMemoryOrderRepositoryHashMap (for tests)

Adapter Rules

  • MUST implement exactly one port interface per adapter (adapters are single-purpose)
  • MUST NOT contain business logic — only translation and delegation
  • MUST NOT be referenced by the application core — the core knows only the port interface
  • SHOULD be swappable: replacing PostgresOrderRepository with MongoOrderRepository requires no core changes

Application Core Structure

The hexagon's interior contains all business logic, free from infrastructure concerns:

core/
  domain/
    model/          # Entities, Value Objects, Aggregates
    service/        # Domain services (stateless business rules)
  application/
    port/
      driving/      # Driving port interfaces (ForPlacingOrders)
      driven/       # Driven port interfaces (ForStoringOrders)
    service/        # Application services (use case orchestration)
  • MUST NOT import any framework, database, or HTTP library in the core
  • MUST NOT reference any adapter class from within the core
  • SHOULD separate domain model from application services (domain contains pure business rules; application coordinates use cases)

Testing Strategy

The hexagonal architecture's primary advantage is testability through adapter substitution.

Test Progression

PhaseDriving AdapterDriven AdapterTests
1. Unit/DomainTest harnessIn-memory stubsBusiness rules in isolation
2. IntegrationTest harnessReal infrastructurePort contract verification
3. AcceptanceHTTP/CLI test clientIn-memory stubsEnd-to-end behavior without infrastructure
4. SystemHTTP/CLI test clientReal infrastructureFull stack verification
  • MUST write tests against port interfaces, not adapter implementations
  • SHOULD start with in-memory adapters during development; add real adapters incrementally
  • SHOULD use the test harness as a first-class driving adapter — it validates the entire core without infrastructure

Relationship to Other Architectures

All three share the same DNA: business logic at center, dependencies point inward, DIP at architectural level.

AspectHexagonalOnion (Palermo, 2008)Clean (Martin, 2012)
Internal structureLeast prescriptive — core is a single hexagonExplicit concentric layers: Domain Model → Domain Services → Application ServicesFour named layers with defined responsibilities
TerminologyPorts and AdaptersLayers and InterfacesEntities, Use Cases, Interface Adapters, Frameworks
Unique contributionSymmetry between driving and driven sides; ports as first-class conceptExplicit domain service layer separationScreaming Architecture, Humble Object, component principles
Best forTeams wanting flexibility in internal organizationTeams wanting explicit domain layer separationTeams wanting comprehensive architectural rules
  • SHOULD choose hexagonal when the team values simplicity and flexibility in internal core organization
  • SHOULD choose clean architecture when the team benefits from prescriptive layer naming and component principles
  • All three are valid; the important invariant is dependencies point inward

Common Pitfalls

PitfallRemedy
Ports as a separate layerPorts belong to the core — they ARE the core's boundary, not a layer between core and adapters
Adapter with business logicMove logic to domain or application service; adapter does translation only
Core importing adapterInvert the dependency — core defines the interface, adapter implements it
Technology in port signaturesExpress ports in domain language; convert technology types in the adapter
One giant port per sideSplit into focused ports by actor/concern; one port per interaction boundary
Over-engineering simple appsHexagonal adds value when multiple adapters or testability are genuinely needed

Checklist

When designing or reviewing a hexagonal architecture, verify:

  • The application core defines all port interfaces — no port is defined in an adapter
  • Driving ports express application capabilities in domain language
  • Driven ports express application needs in domain language — no technology types
  • Primary adapters translate external input and call driving ports
  • Secondary adapters implement driven ports and connect to external systems
  • The core has zero imports from frameworks, databases, or HTTP libraries
  • No adapter is referenced from within the core
  • Business logic lives in the core, not in adapters
  • Tests run against ports using in-memory adapters for fast feedback
  • Adapters are swappable without modifying core code

Key References

BookAuthor(s)PublisherYear
Hexagonal Architecture ExplainedAlistair Cockburn, Juan Manuel Garrido de PazAddison-Wesley2024
Get Your Hands Dirty on Clean Architecture (2nd ed.)Tom HombergsPackt2023
Fundamentals of Software ArchitectureMark Richards, Neal FordO'Reilly2020
Software Architecture: The Hard PartsFord, Richards, Sadalage, DehghaniO'Reilly2021

Online References

  • Alistair Cockburn's original article: alistair.cockburn.us/hexagonal-architecture
  • Netflix Tech Blog: "Ready for Changes with Hexagonal Architecture" (2020)
  • AWS Prescriptive Guidance: "Building Hexagonal Architectures on AWS" (2023)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.