SlideShare a Scribd company logo
HEXAGONAL ARCHITECTURE
Message oriented software design
By Matthias Noback
ARCHITECTURE
What's the problem?
!
!
!
Nice app
!
!
!
Sad app
Your brain can't handle it
M V C ?
Coupling to frameworks
and libraries
!
!
!
How do you start a new
project?
Pick a framework

Install a skeleton project

Remove demo stuff

Auto-generate entities

Auto-generate CRUD controllers

Done
"It's a Symfony project!"
That's actually outside in
The boring stuff
The interesting stuff
Symfony
Doctrine
RabbitMQ
Redis
Angular
Slow tests
DB
Browser
Message
queue
Key-
value
Filesystem
Why do frameworks not solve this for us?
Because they can't ;)
Frameworks are about
encapsulation
Low-level API
$requestContent = file_get_contents('php://input');
$contentType = $_SERVER['CONTENT_TYPE'];
if ($contentType === 'application/json') {
$data = json_decode($requestContent, true);
} elseif ($contentType === 'application/xml') {
$xml = simplexml_load_string($requestContent);
...
}
Nicely hides the details
$data = $serializer->deserialize(
$request->getContent(),
$request->getContentType()
);
Low-level API
$stmt = $db->prepare(
'SELECT * FROM Patient p WHERE p.anonymous = ?'
);
$stmt->bindValue(1, true);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$patient = Patient::reconstituteFromArray($result);
Hides a lot of details
$patient = $repository->createQueryBuilder('p')
->where('p.anonymous = true')
->getQuery()
->getResult();
What about abstraction?
$patient = $repository->createQueryBuilder('p')
->where('p.anonymous = true')
->getQuery()
->getResult();
Concrete
Concrete
Concrete
$patients = $repository->anonymousPatients();
Abstract
Nice
DIY
Coupling to the delivery
mechanism
public function registerPatientAction(Request $request)
{
$patient = new Patient();
!
$form = $this->createForm(new RegisterPatientForm(), $patient);
!
$form->handleRequest($request);
!
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($patient);
$em->flush();
!
return $this->redirect($this->generateUrl('patient_list'));
}
!
return array(
'form' => $form->createView()
);
}
Request and Form are web-specific
EntityManager is ORM, i.e.
relational DB-specific
Reusability: impossible
Some
functionality
The web
The CLI
Some
functionalityRun
it
Lack of intention-revealing code
data
data
data
public function updateAction(Request $request)
{
$patient = new Patient();
!
$form = $this->createForm(new PatientType(), $patient);
!
$form->handleRequest($request);
!
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($patient);
$em->flush();
!
return $this->redirect($this->generateUrl('patient_list'));
}
!
return array(
'form' => $form->createView()
);
}
from the HTTP request
copied into an entity
then stored in the database
What exactly
changed?!
And... why?
R.A.D.
Rapid Application Development
B.A.D.
B.A.D. Application Development
In summary
Coupling to a framework

Coupling to a delivery mechanism (e.g. the web)

Slow tests

Lack of intention in the code
THE ESSENCE
of your application
The essence
Other things
The "heart"?
"The heart of software is its ability to solve
domain-related problems for its users.
–Eric Evans, Domain Driven Design
All other features, vital though they may be,
support this basic purpose."
What's essential?
Domain model
Interaction with it
Use cases
What's not essential?
“The database is an implementation detail”
–Cool software architect
The core doesn't need to
know about it
!
!
!
!
!
!
What about interaction?
!
The core doesn't need to
know about it
!
!
!
Infrastructure
The world outside
!
!
!
Web browser
Terminal
Database
Messaging
Filesystem
(E)mail
Mmm... layers Layers
allow you to
separate
Layers
allow you to
allocate
Layers
have
boundaries
Rules
for crossing
Rules about
communication
Actually: rules about dependencies
The dependency rule
–Robert Martin, Screaming Architecture
What crosses layer
boundaries?
Message
Messages
someFunctionCall(
$arguments,
$prepared,
$for,
$the,
$receiver
);
$message = new TypeOfMessage(
$some,
$relevant,
$arguments
);
handle($message);
What about the application
boundary?
The app
Message
The world outside
How does an app allow
incoming messages at all?
By exposing input ports
Routes
Console commands
A WSDL file for a SOAP API
Hexagonal architecture   message-oriented software design
Hexagonal architecture   message-oriented software design
Ports use protocols for
communication
Each port has a language of its own
Web (HTTP)
Messaging (AMQP)
HTTP
Request
Form
Request
Controller
Entity
Value object
W
eb
port
Translate
the
request
Repository
Adapters
The translators are called: adapters
"Ports and adapters"
Ports: allow for communication to happen

Adapters: translate messages from the world outside
== Hexagonal architecture
Alistair Cockburn
An example
Plain HTTP
message
$_POST,
$_GET,
$_SERVER,
Request
POST /patients/ HTTP/1.1
Host: hospital.com
!
name=Matthias&email=matthiasn
oback@gmail.com
Command
$command = new RegisterPatient(
$request->get('name'),
$request->get('email')
);
Command
$command = new RegisterPatient(
$request->get('name'),
$request->get('email')
);
Expresses
intention
Implies
change
Independent
of delivery
mechanism
Only the
message
class RegisterPatientHandler
{
public function handle(RegisterPatient $command)
{
$patient = Patient::register(
$command->name(),
$command->email()
);
$this->patientRepository->add($patient);
}
}
Command
Command
handler
Command
Command
handler A
Command
bus
Command
handler B
Command
handler C
HTTP
Request
Form
Request
Controller
Patient
(entity)
W
eb
port
PatientRepository
RegisterPatient-
Handler
RegisterPatient
(command)
Infrastructure
Application
Dom
ain
Change
New entity
(Patient)
Entity-
Manager
UnitOf-
Work
$patient = Patient::register(
$command->name(),
$command->email()
);
$this->patientRepository
->add($patient);
Insert
query (SQL)
INSERT INTO patients SET
name='Matthias',
email='matthiasnoback@gmai
l.com';
SQL query
EntityManager
UnitOfWork
QueryBuilder
Persistence
port
Prepare
for
persistence
PatientRepository
C
ore
Infrastructure
Hexagonal architecture   message-oriented software design
Messaging (AMQP)
Persistence
(MySQL)
What often goes wrong:
we violate boundary rules...
EntityManager
UnitOfWork
QueryBuilder
PatientRepository
C
ore
Infrastructure
RegisterPatient-
Handler
Domain
Infrastructure
Application
Domain
PatientRepository
(uses MySQL)
EntityManager
UnitOfWork
QueryBuilder
PatientRepository
(uses MySQL)
Domain
Infrastructure
Application
Domain
RegisterPatient-
Handler
Domain
PatientRepository
(interface)
Dependency
inversion
PatientRepository
(uses MySQL)
RegisterPatient-
Handler
Domain
InMemory-
PatientRepository
Speedy
alternative
RegisterPatient-
Handler
PatientRepository
(uses MySQL)
PatientRepository
(interface)
"A good software architecture allows decisions [...]
to be deferred and delayed."
–Robert Martin, Screaming Architecture
IN CONCLUSION
what did we get from all of this?
Separation of concerns
Core
Infrastructure
Command
Command
Command
handler
Command
handler
Stand-alone use cases
Command
Command
handler
Intention-
revealing
Reusable
Infrastructure stand-ins
Regular
implementation
Interface
Stand-in, fast
implementation
This is all very much
supportive of...
See also: Modelling by Example
DDD
TDD
BDD
CQRS
QUESTIONS?
joind.in/14978
FEEDBACK?

More Related Content

PDF
Javascript
PDF
NoSQL
PPT
Php Presentation
PDF
Oracle Drivers configuration for High Availability, is it a developer's job?
PPTX
A Brief Intro to Scala
KEY
Web API Basics
PDF
Spring Boot
PPTX
Modelos NoSQL e a Persistência Poliglota
Javascript
NoSQL
Php Presentation
Oracle Drivers configuration for High Availability, is it a developer's job?
A Brief Intro to Scala
Web API Basics
Spring Boot
Modelos NoSQL e a Persistência Poliglota

What's hot (20)

PDF
Spring Web Services: SOAP vs. REST
PDF
Hexagonal architecture - message-oriented software design (PHP Barcelona 2015)
PPTX
Oracle application express ppt
PDF
Maximizing performance via tuning and optimization
PDF
Architecture at Scale
PDF
REST API Best (Recommended) Practices
PDF
Railway Oriented Programming
PDF
Introduction to ASP.NET Core
PDF
Rest in flask
PPTX
Introduction to Apache Camel
PDF
SpringBoot 3 Observability
PDF
Laravel - The PHP Framework for Web Artisans
PDF
Hexagonal architecture - message-oriented software design
PDF
Nodejs - A performance que eu sempre quis ter
PPTX
PPTX
Introduction to MERN Stack
PPTX
Spring Boot and REST API
PDF
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
PDF
Oracle APEX for Beginners
Spring Web Services: SOAP vs. REST
Hexagonal architecture - message-oriented software design (PHP Barcelona 2015)
Oracle application express ppt
Maximizing performance via tuning and optimization
Architecture at Scale
REST API Best (Recommended) Practices
Railway Oriented Programming
Introduction to ASP.NET Core
Rest in flask
Introduction to Apache Camel
SpringBoot 3 Observability
Laravel - The PHP Framework for Web Artisans
Hexagonal architecture - message-oriented software design
Nodejs - A performance que eu sempre quis ter
Introduction to MERN Stack
Spring Boot and REST API
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Oracle APEX for Beginners
Ad

Viewers also liked (20)

PDF
Hexagonal architecture for java applications
PDF
Arquitectura hexagonal
PDF
Symfony: Your Next Microframework (SymfonyCon 2015)
PDF
Driving Design through Examples
PDF
Composer in monolithic repositories
PDF
Command Bus To Awesome Town
PDF
Guard Authentication: Powerful, Beautiful Security
PDF
Tactical DDD (just better OOP?) - PHPBenelux 2017
PPTX
Zephir - A Wind of Change for writing PHP extensions
PDF
Amongst models
PPTX
Confoo - Javascript Server Side : How to start
PDF
Diving deep into twig
PDF
Get Soaked - An In Depth Look At PHP Streams
ODP
Elastic Searching With PHP
PPTX
Electrify your code with PHP Generators
PDF
Techniques d'accélération des pages web
ODP
PHP5.5 is Here
PDF
Automation using-phing
PDF
The quest for global design principles (SymfonyLive Berlin 2015)
PDF
Mocking Demystified
Hexagonal architecture for java applications
Arquitectura hexagonal
Symfony: Your Next Microframework (SymfonyCon 2015)
Driving Design through Examples
Composer in monolithic repositories
Command Bus To Awesome Town
Guard Authentication: Powerful, Beautiful Security
Tactical DDD (just better OOP?) - PHPBenelux 2017
Zephir - A Wind of Change for writing PHP extensions
Amongst models
Confoo - Javascript Server Side : How to start
Diving deep into twig
Get Soaked - An In Depth Look At PHP Streams
Elastic Searching With PHP
Electrify your code with PHP Generators
Techniques d'accélération des pages web
PHP5.5 is Here
Automation using-phing
The quest for global design principles (SymfonyLive Berlin 2015)
Mocking Demystified
Ad

Similar to Hexagonal architecture message-oriented software design (20)

PDF
Hexagonal architecture - message-oriented software design (Symfony Live Berli...
PDF
Hexagonal architecture - message-oriented software design (PHP Benelux 2016)
PDF
Hexagonal Symfony - SymfonyCon Amsterdam 2019
PDF
Hexagonal architecture for the web
PDF
Explicit architecture
PDF
Introduction to hexagonal architecture
PDF
Hexagonal architecture: how, why and when
PDF
Application architecture
PDF
Advanced web application architecture - PHP Barcelona
PDF
Advanced web application architecture - Talk
PDF
A very simple hexagonal architecture.pdf
PDF
Hexagonal Architecture.pdf
PDF
Hexagonal architecture in PHP
PDF
Hexagonal architecture
PDF
Hexagonal Architecture using Grails
PDF
Beyond MVC: from Model to Domain
PDF
From silex to symfony and viceversa
PPTX
Framework Independent Architectures
PDF
Advanced web application architecture Way2Web
PDF
Layers, ports and adapters
Hexagonal architecture - message-oriented software design (Symfony Live Berli...
Hexagonal architecture - message-oriented software design (PHP Benelux 2016)
Hexagonal Symfony - SymfonyCon Amsterdam 2019
Hexagonal architecture for the web
Explicit architecture
Introduction to hexagonal architecture
Hexagonal architecture: how, why and when
Application architecture
Advanced web application architecture - PHP Barcelona
Advanced web application architecture - Talk
A very simple hexagonal architecture.pdf
Hexagonal Architecture.pdf
Hexagonal architecture in PHP
Hexagonal architecture
Hexagonal Architecture using Grails
Beyond MVC: from Model to Domain
From silex to symfony and viceversa
Framework Independent Architectures
Advanced web application architecture Way2Web
Layers, ports and adapters

More from Matthias Noback (20)

PDF
Rector fireside chat - PHPMiNDS meetup
PDF
Service abstractions - Part 1: Queries
PDF
A testing strategy for hexagonal applications
PDF
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
PDF
Beyond design principles and patterns (muCon 2019 edition)
PDF
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
PDF
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
PDF
Beyond Design Principles and Patterns
PDF
Building Autonomous Services
PDF
Advanced Application Architecture Symfony Live Berlin 2018
PDF
Designing for Autonomy
PDF
Docker workshop
PDF
Docker swarm workshop
PDF
Docker compose workshop
PDF
Building autonomous services
PDF
Designing for autonomy
PDF
Advanced application architecture
PDF
Continously delivering containerized microservices
PDF
Apprendre le français
PDF
Living Documentation (presentation)
Rector fireside chat - PHPMiNDS meetup
Service abstractions - Part 1: Queries
A testing strategy for hexagonal applications
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
Beyond design principles and patterns (muCon 2019 edition)
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Beyond Design Principles and Patterns
Building Autonomous Services
Advanced Application Architecture Symfony Live Berlin 2018
Designing for Autonomy
Docker workshop
Docker swarm workshop
Docker compose workshop
Building autonomous services
Designing for autonomy
Advanced application architecture
Continously delivering containerized microservices
Apprendre le français
Living Documentation (presentation)

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Encapsulation theory and applications.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Approach and Philosophy of On baking technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Network Security Unit 5.pdf for BCA BBA.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Review of recent advances in non-invasive hemoglobin estimation
Digital-Transformation-Roadmap-for-Companies.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
20250228 LYD VKU AI Blended-Learning.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Encapsulation theory and applications.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Approach and Philosophy of On baking technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Per capita expenditure prediction using model stacking based on satellite ima...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx
Machine learning based COVID-19 study performance prediction
Agricultural_Statistics_at_a_Glance_2022_0.pdf

Hexagonal architecture message-oriented software design