ArchitectureDesign Patterns

Hexagonal Architecture: Ports and Adapters

TT
TopicTrick Team
Hexagonal Architecture: Ports and Adapters

Hexagonal Architecture: Ports and Adapters

"Your business logic shouldn't care if it's talking to an AWS Database or a text file on a floppy disk."

Most software is built like an "Onion" where the core is trapped. If you want to change your Database from MySQL to PostgreSQL, you have to rewrite 50% of your logic because the SQL strings are mixed into the business code.

Hexagonal Architecture (Ports and Adapters) solves this by putting a "Shield" around your logic. This 1,500+ word guide explores the "Plug-and-Play" future of software where your core remains pure while the world around it changes.


1. The Core: The "Pure" Business Logic

At the center of the Hexagon is your Domain.

  • No Frameworks (No Spring, No Express).
  • No Databases (No SQL, No ORM).
  • Just raw Zig or Java objects that describe your business rules (e.g., "A user must be 18 to join").

2. Ports: The "Plug" Sockets

Your core defines Ports. A Port is an Interface.

  • "I need something that can 'SaveUser' and 'FindUser'."
  • The Core doesn't know HOW users are saved. It just knows it needs that power.

3. Adapters: The "Power Cables"

An Adapter is the implementation of the Port.

  • PostgresAdapter: Code that writes the SQL.
  • MockDBAdapter: Code that saves to RAM (for fast testing).
  • The Magic: You can swap the Adapters without changing a single line of your "Core" logic.

4. The Benefits: "Evolutionary" Architecture

  1. Unit Testing is Instant: You don't need a real database to test your login logic. You just plug in a "Fake" adapter.
  2. No Vendor Lock-in: If AWS becomes too expensive, you write a "GCP Adapter" and move the company in one deploy.
  3. Parallel Teams: One team builds the "Website Adapter" while another team builds the "Mobile App Adapter" simultaneously.

5. Hexagonal vs. Clean Architecture

They are brothers.

  • Hexagonal: Focuses on the "Pipes" (Internal vs External).
  • Clean: Focuses on the "Layers" (Entity -> UseCase -> Controller). The Professional Rule: In $2026$, we use both. We use Hexagonal for the Connections and Clean for the Organization.

Summary: The Hexagonal Checklist

  1. Pure Core: Ensure your business rules have zero import statements for databases or web frameworks.
  2. Interface Ports: define your external needs as small, focused interfaces.
  3. Dependency Injection: Use a "Main" file to plug the Adapters into the Ports at startup.
  4. No Leaks: Ensure your Core never sees an "SQL Error" or an "HTTP 404." Wrap those into "Business Errors" (e.g., UserNotFound).
  5. Test the Adapters: Use "Integration Tests" only for the Adapters; use "Unit Tests" for the Core.

Hexagonal Architecture is the "Insurance" of your software. By mastering the isolation of logic and the flexibility of adapters, you gain the ability to build systems that never become "Legacy" because they can evolve forever. You graduate from "Building a tool" to "Architecting a Platform."


Part of the Architecture Masterclass — engineering the isolation.