Hexagonal Architecture
👷 API Construction 🔨
The Cats API
A user can add a cat via the POST /cat endpoint
Every cat must have an owner
The ID of the cat should be returned from POST /cat
Dependencies
Error Handling
Request Validation
Response Formation
Business Logic
Database
New Requirements
The integrations team is working with cat shelters
They will publish new cats to an event bus
These must be stored in the DB
These the same business logic applies
Soultions
Introducing... 🥁
Hexagonal Architecture
Hidden Details
Some types / generics
"Framework" code
Dependency injection
Handlers
Hidden Details
If you want to know more about the details, please talk to us!
What about the new requirements?
The integrations team is working with cat shelters
They will publish new cats to an event bus
These must be stored in the DB
These the same business logic applies
But...
Where are the hexagons? 🤔
Common Pitfalls
Fat controllers
Repository coupling
Over-using inheritance
Common Pitfalls
Fat controllers
Controllers should't handle orchestration
Use of "API services"
Repository coupling
Over-using inheritance
Common Pitfalls
Fat controllers
Repository coupling
Repositories shouldn't be coupled to APIs
Or have "leaky" abstractions
(e.g. DB transactions)
Over-using inheritance
Common Pitfalls
Fat controllers
Repository coupling
Over-using inheritance
Diamond problem
Prefer composition over inheritance
Summary
Separation of concerns
Modular code
Promote code reuse
Improve code readability
Improved testability
Consistency
The same everywhere
Abstract repetitive code (e.g. error handling)
Thank You! ❤️
Further Reading
Articles
Hexagonal Architecture, there are always two sides to every story
Ready for changes with Hexagonal Architecture - Netflix Technology Blog
Misc
Highlighted Cat API Code
👆
Scan me to view slides!