Hexagonal Architecture
- The term
hexagondoes not make sense at all, it doesn't need to have 6 ports - The idea is the have the business logic (the domain) isolated from the external world
- Business Logic can never import from an adapter (only the other way)
Flow of information
- Flow
- Driving adapters (wire in - entrypoints) - http in, kafka consumers, sqs consumers, cli, test scripts
- Input Ports: how BL receives info from external deps
- Business Logic (domain)
- Output Ports: how BL interacts with external deps - db, kafka producers, http client
- Driven adapters (wire out)
Primary / Driving Adapters Secondary / Driven Adapters
┌──────────────────────────────────┐
┌─────────────┐ │ HEXAGON │ ┌───────────────┐
│ API / UI │ ──────▶ │ Input Port Output Port │ ──────▶ │ Database │
└─────────────┘ │ │ └───────────────┘
│ ┌────────────────┐ │
┌─────────────┐ │ │ │ │ ┌───────────────┐
│ Console │ ──────▶ │ │ Business Logic │ │ ──────▶ │ Message Queue │
└─────────────┘ │ │ │ │ └───────────────┘
│ └────────────────┘ │
┌─────────────┐ │ │ ┌───────────────┐
│ Test Scripts│ ──────▶ │ Input Port Output Port │ ──────▶ │ HTTP │
└─────────────┘ │ │ └───────────────┘
└──────────────────────────────────┘
Influences
Dr. Ivar Jacobson- Created UML and RUP
-
Created EIC (Entity-Interface-Control) -> ECB (Entity-Control-Boundary) in 1992
-
Dr. Alistair Cockburn - Signed the Agile Manifesto
-
Created the Hexagonal Architecture (Ports and Adapters) in 2005
-
Jeffrey Palermo - Microsoft MVP
-
Created Onion Architecture (2008) - very similar to Hexagonal!
-
Robert C. Martin(Uncle Bob) - Signed the Agile Manifesto
- Created SOLID (2000)
- Created Clean Code (2009)
- Created Clean Architecture (2017)
- Entity: core and business rules
- Use cases: flow for treating the business rules
Testing
- Hexagonal architecture helps isolating the Business Logic, with that:
- You can use
Fixturesto test theInbound Ports -
You can use
Mocksto test theOutbound Ports -
Domain -
Unit tests
-
Application - Integration tests
- Uses real domains
-
Uses mocks for external dependencies
-
Framework - Tests with the external world.
- E.g., API REST requests, CLI requests, etc