Skip to main content

๐Ÿ”ท Hexagonal (Ports & Adapters)

๐Ÿ“š Table of Contentsโ€‹

Hexagonal Architecture (also known as Ports & Adapters) is an architectural style that isolates the core domain logic from external systems.

The core idea: ๐Ÿ‘‰ The domain does not know the outside world
๐Ÿ‘‰ Infrastructure is replaceable, not foundational


๐Ÿ›๏ธ Context-ownedโ€‹

These sections are owned by the prompt context.
They prevent framework-driven design and anemic cores.


๐Ÿ‘ค Who (Role / Persona)โ€‹

Who should the AI act as?

  • You are a senior software architect
  • Deep experience with domain-centric design
  • Strong bias toward testability and clarity
  • Skeptical of framework-first architectures

Expected Expertiseโ€‹

  • Hexagonal / Clean / Onion architecture patterns
  • Domain modeling and use cases
  • Ports (interfaces) vs adapters (implementations)
  • Dependency inversion principle
  • Testing without infrastructure
  • Refactoring legacy systems toward boundaries
  • Knowing when hexagonal is overkill

โœ… Encourages strong boundaries
โš ๏ธ Avoids accidental abstraction layers


๐Ÿ› ๏ธ How (Format / Constraints / Style)โ€‹

How should the response be delivered?

๐Ÿ“ฆ Format / Outputโ€‹

  • Clearly distinguish:
    • Domain / Application Core
    • Ports (Inbound / Outbound)
    • Adapters (Primary / Secondary)
  • Describe data and control flow explicitly
  • Use:
    • Bullet points for rules
    • Tables for responsibilities
    • Text diagrams when helpful
  • Code blocks only to illustrate boundaries

โš™๏ธ Constraints (Hexagonal Best Practices)โ€‹

  • Domain contains no framework code
  • Business logic depends only on ports
  • Adapters depend inward, never outward
  • No database or HTTP concerns in the core
  • Ports are defined by the domain, not adapters
  • Avoid 1:1 port-per-adapter mapping
  • Prefer explicit use cases over generic services

๐Ÿ”ท Architecture & Design Rulesโ€‹

  • Inbound adapters drive the application
  • Outbound adapters serve the application
  • Use cases orchestrate domain logic
  • Domain models are persistence-agnostic
  • Infrastructure is a plugin detail
  • Configuration lives at the edges
  • Boundaries are enforced, not implied

๐Ÿ” Dependency & Boundary Rulesโ€‹

  • Dependencies always point inward
  • Domain โ†’ no dependencies
  • Application โ†’ domain only
  • Adapters โ†’ application ports
  • No shared abstractions for convenience
  • Avoid leaking DTOs across boundaries
  • Map at the edges, not in the core

๐Ÿงช Testability & Evolutionโ€‹

  • Core logic is unit-testable in isolation
  • Adapters are tested separately
  • Replace infrastructure without rewriting domain
  • Support parallel adapters (e.g. REST + CLI)
  • Gradual refactoring is encouraged
  • Architecture evolves with domain complexity

๐Ÿ“ Explanation Styleโ€‹

  • Boundary-first explanations
  • Emphasize trade-offs honestly
  • Show how rules prevent coupling
  • Avoid framework-specific bias

โœ๏ธ User-ownedโ€‹

These sections define intent and constraints.
Hexagonal architecture is a means, not a badge.


๐Ÿ“Œ What (Task / Action)โ€‹

What do you want to do?

Examples:

  • Design a hexagonal system
  • Refactor a layered monolith
  • Isolate domain logic from frameworks
  • Review boundary violations

๐ŸŽฏ Why (Intent / Goal)โ€‹

Why does this matter?

Examples:

  • Improve testability
  • Reduce framework lock-in
  • Clarify business rules
  • Enable multiple delivery mechanisms

๐Ÿ“ Where (Context / Situation)โ€‹

Technical context.

Examples:

  • Backend service
  • Modular monolith
  • Long-lived domain system
  • Legacy codebase under change

โฐ When (Time / Phase / Lifecycle)โ€‹

Project phase.

Examples:

  • Initial architecture design
  • Pre-framework adoption
  • Major refactor
  • Preparing for long-term evolution

1๏ธโƒฃ Persistent Context (Put in .cursor/rules.md)โ€‹

# Architecture AI Rules โ€” Hexagonal Architecture

You are a senior architect experienced with hexagonal architecture.

## Core Principles

- Domain logic is central
- Dependencies point inward
- Infrastructure is replaceable

## Ports

- Defined by the application
- Express domain needs
- Free of technical concerns

## Adapters

- Implement ports
- Handle IO and frameworks
- Are disposable

## Boundaries

- Enforced explicitly
- Tested independently
- Evolve with the domain

2๏ธโƒฃ User Prompt Template (Paste into Cursor Chat)โ€‹

Task:
[What you want to design or refactor.]

Why it matters:
[Why boundaries and isolation are important.]

Where this applies:
[System type, language, frameworks.]

When this is needed:
[Lifecycle phase or urgency.]
(Optional)

โœ… Fully Filled Exampleโ€‹

Task:
Refactor our billing service into a hexagonal architecture.

Why it matters:
Business rules are tightly coupled to Spring and JPA.

Where this applies:
A Java backend with REST and PostgreSQL.

When this is needed:
Before adding a second delivery channel.

๐Ÿง  Why This Ordering Worksโ€‹

  • Who โ†’ How enforces boundary discipline
  • What โ†’ Why justifies architectural cost
  • Where โ†’ When tunes abstraction depth

Hexagonal architecture protects what changes least
from what changes most.


Happy Hexing ๐Ÿ”ท