8. Hexagonal Architecture: how, why and when - 11/2019 8
Spaghetti architecture
✅ Fun to revisit after some years...
✅ Valid for small proof of concept scripts
with a 10 minutes life expectancy
...although that means you have it on production
15. Hexagonal Architecture: how, why and when - 11/2019 15
MVC architecture
❌ No isolation of business logic
✅ Valid for prototyping and purely CRUD applications
❌ Data centric
❌ Hard to test, infrastructure everywhere
18. Hexagonal Architecture: how, why and when - 11/2019 18
Layered architecture
HTTP & CLI
Same application
service
Diagram from book DDD in PHP
19. Hexagonal Architecture: how, why and when - 11/2019 19
Layered architecture (the wrong way)
Now need to read
products from this
random API
And we should
notify warehouse
guys
Database
language
Outside
world
Outside
world
Our thing
20. Hexagonal Architecture: how, why and when - 11/2019 20
Layered architecture (the wrong way)
Hey, plz generate a
RSS feed with
products we read
from this weird
machine
And did I
mentioned we
are migrating to
Mongo?
Combinatorial explosion
Infrastructure coupling
21. Hexagonal Architecture: how, why and when - 11/2019 21
Layered architecture (the wrong way)
Hey, plz generate a
RSS feed with
products we read
from this weird
machine
And did I
mentioned we
are migrating to
Mongo?
Combinatorial explosion
Infrastructure coupling
Aren’t tests
taking too much
time?
Testability
22. Hexagonal Architecture: how, why and when - 11/2019 22
Layered architecture
❌ Data persistence is in the core*
✅ Separation of concerns
✅ Application layer encapsulating use cases
❌ Potential infrastructure leaks in other layers*
✅ You can build good stuff
❌ Lasagna antipattern
* if you do it wrong
25. Hexagonal Architecture: how, why and when - 11/2019 25
Aka Ports and Adapters
Coined by Alistair Cockburn in 2005
Hexagonal architecture
Check out Alistair in the "Hexagone" in Youtube
26. Hexagonal Architecture: how, why and when - 11/2019 26
Allow an application to equally be driven by users,
programs, automated test or batch scripts, and to be
developed and tested in isolation from its eventual run-time
devices and databases.
Hexagonal architecture
27. Hexagonal Architecture: how, why and when - 11/2019 27
Outside the application
A boundary
Application
Hexagonal architecture
28. Hexagonal Architecture: how, why and when - 11/2019 28
Port:
● Outside element
communicating with the
application
● Defined by an interface*
Application
Adapter:
● Implementation of a port
interface into what
application understands
*Not a code interface, although
sometimes represented by one
Ports and adapters
29. Hexagonal Architecture: how, why and when - 11/2019 29
Primary ports
Drivers
Secondary ports
Repositories and
recipients
Ports and adapters
30. Hexagonal Architecture: how, why and when - 11/2019 30
Test driver
User interface
Router + HTTP Controller
Ports and adapters
CLI
Console component
REST API
External application
Other adapter
Other adapter
31. Hexagonal Architecture: how, why and when - 11/2019 31
Ports and adapters
REST API adapter with Symfony:
● Routing component
● HTTP Kernel and Controller
● HTTP component Request
32. Hexagonal Architecture: how, why and when - 11/2019 32
Ports and adapters
Persistence
Queue
Feed
Interface implementation
Interface implementation
Interfaces
defined by the
application
33. Hexagonal Architecture: how, why and when - 11/2019 33
Ports and adapters
Persistence adapter with Doctrine ORM
Application defines the contract in its
own language
We don’t care about using database
or library language in the adapter
34. Hexagonal Architecture: how, why and when - 11/2019 34
Ports and adapters
Persistence adapter with Doctrine ORM
No infrastructure leaks
Testability improved!
36. Hexagonal Architecture: how, why and when - 11/2019 36
Allow an application to equally be driven by users,
programs, automated test or batch scripts, and to be
developed and tested in isolation from its eventual run-time
devices and databases.
Hexagonal architecture
No mention to
layers...
37. Hexagonal Architecture: how, why and when - 11/2019 37
Hexagonal architecture
You can put whatever you want inside the hexagon
40. Hexagonal Architecture: how, why and when - 11/2019 40
User Interface and Infrastructure layer:
● UI for primary ports adapters
● Infrastructure for secondary ports implementations
Application layer:
● Defines the system use cases
● Orchestrates the domain logic
● Command + Command Handler pattern
Domain layer:
● Holds all the business logic and invariants
● Defines the contracts implemented in infrastructure
Hexagonal + Layered: a proposal
42. Hexagonal Architecture: how, why and when - 11/2019 42
Hexagonal + Layered: Domain layer
Domain
Application
UI
Infrastructure
Don’t leak
infrastructure in
your domain
43. Hexagonal Architecture: how, why and when - 11/2019 43
Hexagonal + Layered: Application layer
Domain
Application
UI
Infrastructure
More logic
than this is
suspicious
44. Hexagonal Architecture: how, why and when - 11/2019 44
Hexagonal + Layered: Driver port
Domain
Application
UI
Infrastructure
45. Hexagonal Architecture: how, why and when - 11/2019 45
Hexagonal + Layered: Repository port
Domain
Application
UI
Infrastructure
Dependency inversion
48. Hexagonal Architecture: how, why and when - 11/2019 48
When to apply hexagonal architecture?
Pros
● Testability
● Promotes separation of
concerns
● Pushes accidental complexity
out of the domain
● Open/Closed
● Handy base for DDD
● Handy base for CQRS and ES
Cons
● Indirection
● Amount of boilerplate code
49. Hexagonal Architecture: how, why and when - 11/2019 49
When to apply hexagonal architecture?
Makes no sense for
● Scripting
● Prototyping
● Tooling
● Frameworks
Makes sense for
● Applications relying on external technologies
● CRUD applications
● Anything more complex than that
50. Hexagonal Architecture: how, why and when - 11/2019 50
When to apply hexagonal architecture?
Take decisions based on the context and needs
Keep in mind the reasons and the consequences
ALWAYS
52. Hexagonal Architecture: how, why and when - 11/2019
● Slides of this presentation anytime soon in @xoubaman
● Article: Hexagonal Architecture by Alistair Cockburn
● Article: Hexagonal Architecture by Fideloper
● Article: DDD, Hexagonal, Onion, Clean, CQRS, … How I put it all together by Hebert Graca
● Video: Introducción Arquitectura Hexagonal (Spanish) by CodelyTV
● Video: Hexagonal Architecture - Message-Oriented Software Design by Matthias Noback
● Video: Alistair in the “Hexagone” by DDD FR
● Book: DDD in PHP by Carlos Buenosvinos, Christian Soronellas, and Keyvan Akbary
● Book: Implementing Domain Driven Design by Vaughn Vernon
● Code samples glittered with Carbon
52
Some resources to follow up