SlideShare ist ein Scribd-Unternehmen logo
1 von 31
Downloaden Sie, um offline zu lesen
Hexagonal Architecture
Ports & Adapters Pattern
1
Foreword
● I call myself Tung, & you can do that too!
● I identify as an Engineer
● Please feel free to interrupt me anytime for any question :)
2
Agenda
● A Long Introduction
○ What is an Architecture?
○ Backend Architectures
● Hexagonal Architecture
○ The Hexagon
○ The Actors
○ The Ports
○ The Adapters
● Summary, Pros, & Cons
3
What comes to your mind when you hear “architecture”?
● That depends!
● The off-the-top-of-one’s-head answer varies depending on:
○ Your background
○ Your most recent / pending task
○ Your pain point at the moment
○ …
4
The famous (& boring) 3-layer architecture
5
There are many variants of this 3-layer thingy
6
Let’s focus on the backend
● Monolith
● Micro services
→ Let’s jump into a service :)
7
8
MVC
9
There’s 3-layer thingy for the backend alone
10
It’s really popular!
11
There are too many!
● The point is that these architectures are not mutually exclusive
● The frontend - backend boundary could be very fluid
○ “Backend” can use template engine to render web pages
○ “Frontend” can run code on the server to render parts of the pages there
● How small a service has to be to be called “micro”?
○ Micro services architecture with only 1 big service
○ “Nano” services architecture with lazy services
■ & tons of mappers to convert all the DTOs to-and-fro
12
Nothing is perfect!
● A service, reasonably sized
● 3-layer Controller - Service - Persistence
● & still a big ball of mud in there
→ These architectures are just ways to see, to organize things
13
Even hexagonal architecture
“Ports & Adapters pattern says nothing about the structure of the inside of the
hexagon. You can have layers… you can have components by feature… you can
have spaghetti code… you can have a Big Ball of Mud… you can apply DDD
tactical patterns… you can have a single CRUD… it’s up to you.”
14
15
The hexagon
● The core, pure business logic
● Technology agnostic
→ Could not be 100% so, as it still has to be written using a programming
language and therefore, influenced by that language
16
The actors
1. Drivers / Primary Actors → trigger interaction
2. Driven / Secondary Actors → are triggered by the application
a. Repository: 2-way exchange of info
b. Recipient: 1-way from application
● For knowing which kind is the actor in an application-actor interaction, ask
yourself “who” triggers the conversation
○ If the answer is “the actor” then it is a driver
○ If the answer is “the application” then the actor is a driven actor
17
18
The ports
● Ports are the application boundary, in the picture a port is an edge of the
hexagon
● From the outside world, actors can only interact with the hexagon ports, they
shouldn’t be able to access the inside of the hexagon
● An important thing to remark is that ports belong to the application
● Driver ports → API, features that the application provides to outside world
● Driven ports → SPI needed, dependencies of that application
19
The adapters
● Given a port, there may be an adapter for each desired technology that we
want to use
● Adapters are outside the application
● A driver adapter uses a driver port interface, converting a specific
technology request into a technology agnostic request to a driver port
○ E.g. from a REST call to a java method call (Controller class calls Service class)
● A driven adapter implements a driven port interface, converting the
technology agnostic methods of the port into specific technology methods
○ E.g. from a java method call to a push to a message queue (Service class calls Queue Client
class)
○ or from a java method call to a SQL query (Service class calls Repository class)
20
21
22
Summary of hexagonal architecture
1. The Hexagon → the application
a. Driver Ports → API offered by the application
b. Driven Ports → SPI required by the application
2. Actors → environment devices that interact with the application
a. Drivers → application users (either humans or hardware/software devices)
b. Driven Actors → provide services required by the application
3. Adapters → adapt specific technology to the application
a. Driver Adapters → use the drivers ports
b. Driven Adapters → implement the driven ports
23
The Main Component
This component will run at startup and it builds the whole system doing the
following:
● It initializes and configures the environment (databases, servers, …)
● For each driven port:
○ It chooses a driven adapter implementing the port, and creates an instance of the adapter
○ It creates an instance of the application injecting the driven adapters instances into the
application constructor
● For each driver port:
○ It chooses a driver adapter that uses the port, and creates an instance of the adapter, injecting
the application instance into the adapter constructor
○ It runs the driver adapter instance
24
Pros
Flexibility & immunity to technology evolution
● Swapping between different technologies is easy
○ For a given port, you can have multiple adapters, each one using a specific technology
○ For choosing one of them, you just have to configure which adapter to use for that port
● The technology you want to upgrade is located at an adapter outside the
application
○ Technology evolves more frequently than business logic does
○ You just have to change the adapter
○ The application itself remains immutable because it doesn’t depend on adapters
25
Pros
Delay technological decisions
● Focus just on business logic
○ Start with the application itself (ports are part of the application)
○ Choose a technology later, & create an adapter for it
26
Cons
Complexity
● A lot of modules and explicit dependencies defined between them
○ Modules in the sense of Maven / Gradle modules / subprojects
○ Explicitly create the separation between different elements
■ E.g. you can’t “accidentally” importing an adapter class into the application
● At least, there will be one module for the hexagon, one module for each
adapter, and one module for starting up the whole project:
○ The hexagon depends on nothing
○ The adapters depend on the hexagon
○ The starting-up depends on all of them
27
Cons
Commitment
● The team needs good agreement & strong discipline to follow the pattern
○ It’s just too easy to use fancy framework utilities & dirty the core
○ For example, lots of things have to be wired up manually, while all it takes is a single Spring
Boot annotation to do it all
28
Parting words
● Actor → Adapter → (Port) Hexagon
● Clear dependency direction & pure logic business code
○ Testing is just a breeze
● And at what price?
○ Complexity:
■ Numerous modules
■ Need for internal mappers to map DTOs between components
○ Compromises:
■ Should we write those boilerplate getters & setters, or relax & use Lombok?
■ We want the fancy Spring Boot Configuration Properties but the Config classes are part
of the hexagon?
29
References
● https://jmgarridopaz.github.io/content/hexagonalarchitecture.html → an
excellent article on the pattern
● https://www.goodreads.com/book/show/34372564-microservice-patterns →
the book Microservice Patterns by Chris Richardson
30
Tung Vu Anh
Senior Software Engineer
https://www.linkedin.com/in/tung-vu-swe/
31

Weitere ähnliche Inhalte

Was ist angesagt?

Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean ArchitectureRoc Boronat
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven DesignRyan Riley
 
How to Implement Domain Driven Design in Real Life SDLC
How to Implement Domain Driven Design  in Real Life SDLCHow to Implement Domain Driven Design  in Real Life SDLC
How to Implement Domain Driven Design in Real Life SDLCAbdul Karim
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean ArchitectureMattia Battiston
 
SOLID Principles and The Clean Architecture
SOLID Principles and The Clean ArchitectureSOLID Principles and The Clean Architecture
SOLID Principles and The Clean ArchitectureMohamed Galal
 
Domain-Driven Design
Domain-Driven DesignDomain-Driven Design
Domain-Driven DesignAndriy Buday
 
Hexagonal Architecture
Hexagonal ArchitectureHexagonal Architecture
Hexagonal ArchitectureMarcelo Cure
 
Domain Driven Design (Ultra) Distilled
Domain Driven Design (Ultra) DistilledDomain Driven Design (Ultra) Distilled
Domain Driven Design (Ultra) DistilledNicola Costantino
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean ArchitectureBadoo
 
Microservices pattern language (microxchg microxchg2016)
Microservices pattern language (microxchg microxchg2016)Microservices pattern language (microxchg microxchg2016)
Microservices pattern language (microxchg microxchg2016)Chris Richardson
 
Domain Driven Design - Strategic Patterns and Microservices
Domain Driven Design - Strategic Patterns and MicroservicesDomain Driven Design - Strategic Patterns and Microservices
Domain Driven Design - Strategic Patterns and MicroservicesRadosław Maziarka
 
Clean architecture
Clean architectureClean architecture
Clean architecture.NET Crowd
 
Domain Driven Design (DDD)
Domain Driven Design (DDD)Domain Driven Design (DDD)
Domain Driven Design (DDD)Tom Kocjan
 
Domain Driven Design Demonstrated
Domain Driven Design Demonstrated Domain Driven Design Demonstrated
Domain Driven Design Demonstrated Alan Christensen
 
Microservice Architecture Patterns, by Richard Langlois P. Eng.
Microservice Architecture Patterns, by Richard Langlois P. Eng.Microservice Architecture Patterns, by Richard Langlois P. Eng.
Microservice Architecture Patterns, by Richard Langlois P. Eng.Richard Langlois P. Eng.
 
A Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation SlidesA Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation Slidesthinkddd
 

Was ist angesagt? (20)

Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean Architecture
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
How to Implement Domain Driven Design in Real Life SDLC
How to Implement Domain Driven Design  in Real Life SDLCHow to Implement Domain Driven Design  in Real Life SDLC
How to Implement Domain Driven Design in Real Life SDLC
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
SOLID Principles and The Clean Architecture
SOLID Principles and The Clean ArchitectureSOLID Principles and The Clean Architecture
SOLID Principles and The Clean Architecture
 
Domain-Driven Design
Domain-Driven DesignDomain-Driven Design
Domain-Driven Design
 
Hexagonal Architecture
Hexagonal ArchitectureHexagonal Architecture
Hexagonal Architecture
 
Domain Driven Design (Ultra) Distilled
Domain Driven Design (Ultra) DistilledDomain Driven Design (Ultra) Distilled
Domain Driven Design (Ultra) Distilled
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
Microservices pattern language (microxchg microxchg2016)
Microservices pattern language (microxchg microxchg2016)Microservices pattern language (microxchg microxchg2016)
Microservices pattern language (microxchg microxchg2016)
 
Domain Driven Design - Strategic Patterns and Microservices
Domain Driven Design - Strategic Patterns and MicroservicesDomain Driven Design - Strategic Patterns and Microservices
Domain Driven Design - Strategic Patterns and Microservices
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Domain Driven Design (DDD)
Domain Driven Design (DDD)Domain Driven Design (DDD)
Domain Driven Design (DDD)
 
Domain driven design
Domain driven designDomain driven design
Domain driven design
 
Introduction to microservices
Introduction to microservicesIntroduction to microservices
Introduction to microservices
 
Event storming recipes
Event storming recipesEvent storming recipes
Event storming recipes
 
Domain Driven Design Demonstrated
Domain Driven Design Demonstrated Domain Driven Design Demonstrated
Domain Driven Design Demonstrated
 
Microservice Architecture Patterns, by Richard Langlois P. Eng.
Microservice Architecture Patterns, by Richard Langlois P. Eng.Microservice Architecture Patterns, by Richard Langlois P. Eng.
Microservice Architecture Patterns, by Richard Langlois P. Eng.
 
A Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation SlidesA Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation Slides
 

Ähnlich wie Hexagonal Architecture.pdf

Meetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCMeetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCDamienCarpy
 
On component interface
On component interfaceOn component interface
On component interfaceLaurence Chen
 
From class to architecture
From class to architectureFrom class to architecture
From class to architectureMarcin Hawraniak
 
Leveraging Open Standards to Build Highly Extensible Autonomous Systems
Leveraging Open Standards to Build Highly Extensible Autonomous SystemsLeveraging Open Standards to Build Highly Extensible Autonomous Systems
Leveraging Open Standards to Build Highly Extensible Autonomous SystemsICS
 
Kubernetes at Reddit: An Origin Story - KubeCon NA 2018
Kubernetes at Reddit: An Origin Story - KubeCon NA 2018Kubernetes at Reddit: An Origin Story - KubeCon NA 2018
Kubernetes at Reddit: An Origin Story - KubeCon NA 2018Gregory Taylor
 
Secure Developer Access at Decisiv
Secure Developer Access at DecisivSecure Developer Access at Decisiv
Secure Developer Access at DecisivTeleport
 
Evolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand RaoEvolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand RaoVMware Tanzu
 
SFO15-100K1: Welcome Keynote: George Grey, Linaro CEO
SFO15-100K1: Welcome Keynote: George Grey, Linaro CEOSFO15-100K1: Welcome Keynote: George Grey, Linaro CEO
SFO15-100K1: Welcome Keynote: George Grey, Linaro CEOLinaro
 
The working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор ТурскийThe working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор ТурскийSigma Software
 
The working architecture of node js applications open tech week javascript ...
The working architecture of node js applications   open tech week javascript ...The working architecture of node js applications   open tech week javascript ...
The working architecture of node js applications open tech week javascript ...Viktor Turskyi
 
George Grey Welcome Keynote - BUD17-100K1
George Grey Welcome Keynote - BUD17-100K1George Grey Welcome Keynote - BUD17-100K1
George Grey Welcome Keynote - BUD17-100K1Linaro
 
Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...Pôle Systematic Paris-Region
 
JSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendJSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendVlad Fedosov
 
Software Architecture - All you need to know
Software Architecture - All you need to knowSoftware Architecture - All you need to know
Software Architecture - All you need to knowVincent Composieux
 
Shaping the Future of Automatic Programming
Shaping the Future of Automatic ProgrammingShaping the Future of Automatic Programming
Shaping the Future of Automatic ProgrammingChristos Tsakostas
 
Angular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraAngular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraLINAGORA
 
Rise of the machines: Continuous Delivery at SEEK - YOW! Night Summary Slides
Rise of the machines: Continuous Delivery at SEEK - YOW! Night Summary SlidesRise of the machines: Continuous Delivery at SEEK - YOW! Night Summary Slides
Rise of the machines: Continuous Delivery at SEEK - YOW! Night Summary SlidesDiUS
 
[scala.by] Launching new application fast
[scala.by] Launching new application fast[scala.by] Launching new application fast
[scala.by] Launching new application fastDenis Karpenko
 

Ähnlich wie Hexagonal Architecture.pdf (20)

Meetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCMeetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaC
 
On component interface
On component interfaceOn component interface
On component interface
 
From class to architecture
From class to architectureFrom class to architecture
From class to architecture
 
Leveraging Open Standards to Build Highly Extensible Autonomous Systems
Leveraging Open Standards to Build Highly Extensible Autonomous SystemsLeveraging Open Standards to Build Highly Extensible Autonomous Systems
Leveraging Open Standards to Build Highly Extensible Autonomous Systems
 
Kubernetes at Reddit: An Origin Story - KubeCon NA 2018
Kubernetes at Reddit: An Origin Story - KubeCon NA 2018Kubernetes at Reddit: An Origin Story - KubeCon NA 2018
Kubernetes at Reddit: An Origin Story - KubeCon NA 2018
 
Secure Developer Access at Decisiv
Secure Developer Access at DecisivSecure Developer Access at Decisiv
Secure Developer Access at Decisiv
 
Evolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand RaoEvolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand Rao
 
SFO15-100K1: Welcome Keynote: George Grey, Linaro CEO
SFO15-100K1: Welcome Keynote: George Grey, Linaro CEOSFO15-100K1: Welcome Keynote: George Grey, Linaro CEO
SFO15-100K1: Welcome Keynote: George Grey, Linaro CEO
 
The working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор ТурскийThe working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор Турский
 
The working architecture of node js applications open tech week javascript ...
The working architecture of node js applications   open tech week javascript ...The working architecture of node js applications   open tech week javascript ...
The working architecture of node js applications open tech week javascript ...
 
George Grey Welcome Keynote - BUD17-100K1
George Grey Welcome Keynote - BUD17-100K1George Grey Welcome Keynote - BUD17-100K1
George Grey Welcome Keynote - BUD17-100K1
 
A CQRS Journey
A CQRS JourneyA CQRS Journey
A CQRS Journey
 
Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...
 
Engineering Frontends
Engineering FrontendsEngineering Frontends
Engineering Frontends
 
JSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendJSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontend
 
Software Architecture - All you need to know
Software Architecture - All you need to knowSoftware Architecture - All you need to know
Software Architecture - All you need to know
 
Shaping the Future of Automatic Programming
Shaping the Future of Automatic ProgrammingShaping the Future of Automatic Programming
Shaping the Future of Automatic Programming
 
Angular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraAngular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - Linagora
 
Rise of the machines: Continuous Delivery at SEEK - YOW! Night Summary Slides
Rise of the machines: Continuous Delivery at SEEK - YOW! Night Summary SlidesRise of the machines: Continuous Delivery at SEEK - YOW! Night Summary Slides
Rise of the machines: Continuous Delivery at SEEK - YOW! Night Summary Slides
 
[scala.by] Launching new application fast
[scala.by] Launching new application fast[scala.by] Launching new application fast
[scala.by] Launching new application fast
 

Kürzlich hochgeladen

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Kürzlich hochgeladen (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Hexagonal Architecture.pdf

  • 1. Hexagonal Architecture Ports & Adapters Pattern 1
  • 2. Foreword ● I call myself Tung, & you can do that too! ● I identify as an Engineer ● Please feel free to interrupt me anytime for any question :) 2
  • 3. Agenda ● A Long Introduction ○ What is an Architecture? ○ Backend Architectures ● Hexagonal Architecture ○ The Hexagon ○ The Actors ○ The Ports ○ The Adapters ● Summary, Pros, & Cons 3
  • 4. What comes to your mind when you hear “architecture”? ● That depends! ● The off-the-top-of-one’s-head answer varies depending on: ○ Your background ○ Your most recent / pending task ○ Your pain point at the moment ○ … 4
  • 5. The famous (& boring) 3-layer architecture 5
  • 6. There are many variants of this 3-layer thingy 6
  • 7. Let’s focus on the backend ● Monolith ● Micro services → Let’s jump into a service :) 7
  • 8. 8
  • 10. There’s 3-layer thingy for the backend alone 10
  • 12. There are too many! ● The point is that these architectures are not mutually exclusive ● The frontend - backend boundary could be very fluid ○ “Backend” can use template engine to render web pages ○ “Frontend” can run code on the server to render parts of the pages there ● How small a service has to be to be called “micro”? ○ Micro services architecture with only 1 big service ○ “Nano” services architecture with lazy services ■ & tons of mappers to convert all the DTOs to-and-fro 12
  • 13. Nothing is perfect! ● A service, reasonably sized ● 3-layer Controller - Service - Persistence ● & still a big ball of mud in there → These architectures are just ways to see, to organize things 13
  • 14. Even hexagonal architecture “Ports & Adapters pattern says nothing about the structure of the inside of the hexagon. You can have layers… you can have components by feature… you can have spaghetti code… you can have a Big Ball of Mud… you can apply DDD tactical patterns… you can have a single CRUD… it’s up to you.” 14
  • 15. 15
  • 16. The hexagon ● The core, pure business logic ● Technology agnostic → Could not be 100% so, as it still has to be written using a programming language and therefore, influenced by that language 16
  • 17. The actors 1. Drivers / Primary Actors → trigger interaction 2. Driven / Secondary Actors → are triggered by the application a. Repository: 2-way exchange of info b. Recipient: 1-way from application ● For knowing which kind is the actor in an application-actor interaction, ask yourself “who” triggers the conversation ○ If the answer is “the actor” then it is a driver ○ If the answer is “the application” then the actor is a driven actor 17
  • 18. 18
  • 19. The ports ● Ports are the application boundary, in the picture a port is an edge of the hexagon ● From the outside world, actors can only interact with the hexagon ports, they shouldn’t be able to access the inside of the hexagon ● An important thing to remark is that ports belong to the application ● Driver ports → API, features that the application provides to outside world ● Driven ports → SPI needed, dependencies of that application 19
  • 20. The adapters ● Given a port, there may be an adapter for each desired technology that we want to use ● Adapters are outside the application ● A driver adapter uses a driver port interface, converting a specific technology request into a technology agnostic request to a driver port ○ E.g. from a REST call to a java method call (Controller class calls Service class) ● A driven adapter implements a driven port interface, converting the technology agnostic methods of the port into specific technology methods ○ E.g. from a java method call to a push to a message queue (Service class calls Queue Client class) ○ or from a java method call to a SQL query (Service class calls Repository class) 20
  • 21. 21
  • 22. 22
  • 23. Summary of hexagonal architecture 1. The Hexagon → the application a. Driver Ports → API offered by the application b. Driven Ports → SPI required by the application 2. Actors → environment devices that interact with the application a. Drivers → application users (either humans or hardware/software devices) b. Driven Actors → provide services required by the application 3. Adapters → adapt specific technology to the application a. Driver Adapters → use the drivers ports b. Driven Adapters → implement the driven ports 23
  • 24. The Main Component This component will run at startup and it builds the whole system doing the following: ● It initializes and configures the environment (databases, servers, …) ● For each driven port: ○ It chooses a driven adapter implementing the port, and creates an instance of the adapter ○ It creates an instance of the application injecting the driven adapters instances into the application constructor ● For each driver port: ○ It chooses a driver adapter that uses the port, and creates an instance of the adapter, injecting the application instance into the adapter constructor ○ It runs the driver adapter instance 24
  • 25. Pros Flexibility & immunity to technology evolution ● Swapping between different technologies is easy ○ For a given port, you can have multiple adapters, each one using a specific technology ○ For choosing one of them, you just have to configure which adapter to use for that port ● The technology you want to upgrade is located at an adapter outside the application ○ Technology evolves more frequently than business logic does ○ You just have to change the adapter ○ The application itself remains immutable because it doesn’t depend on adapters 25
  • 26. Pros Delay technological decisions ● Focus just on business logic ○ Start with the application itself (ports are part of the application) ○ Choose a technology later, & create an adapter for it 26
  • 27. Cons Complexity ● A lot of modules and explicit dependencies defined between them ○ Modules in the sense of Maven / Gradle modules / subprojects ○ Explicitly create the separation between different elements ■ E.g. you can’t “accidentally” importing an adapter class into the application ● At least, there will be one module for the hexagon, one module for each adapter, and one module for starting up the whole project: ○ The hexagon depends on nothing ○ The adapters depend on the hexagon ○ The starting-up depends on all of them 27
  • 28. Cons Commitment ● The team needs good agreement & strong discipline to follow the pattern ○ It’s just too easy to use fancy framework utilities & dirty the core ○ For example, lots of things have to be wired up manually, while all it takes is a single Spring Boot annotation to do it all 28
  • 29. Parting words ● Actor → Adapter → (Port) Hexagon ● Clear dependency direction & pure logic business code ○ Testing is just a breeze ● And at what price? ○ Complexity: ■ Numerous modules ■ Need for internal mappers to map DTOs between components ○ Compromises: ■ Should we write those boilerplate getters & setters, or relax & use Lombok? ■ We want the fancy Spring Boot Configuration Properties but the Config classes are part of the hexagon? 29
  • 30. References ● https://jmgarridopaz.github.io/content/hexagonalarchitecture.html → an excellent article on the pattern ● https://www.goodreads.com/book/show/34372564-microservice-patterns → the book Microservice Patterns by Chris Richardson 30
  • 31. Tung Vu Anh Senior Software Engineer https://www.linkedin.com/in/tung-vu-swe/ 31