Hexagonal Architecture

👷 API Construction 🔨

Eddie the cat

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

Meme of Drake looking disapproving at refactoring and approving at copy-pasting

Introducing... 🥁

Hexagonal Architecture

Hero
					
						
					
				
					
						
					
				
					
						
					
				

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? 🤔

Cats API Hexagonal Architecture Diagram

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

QR code for the presentation
👆 Scan me to view slides!