Hexagonal architecture for java applications
-
Upload
fabricio-epaminondas -
Category
Software
-
view
1.589 -
download
121
Transcript of Hexagonal architecture for java applications
![Page 1: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/1.jpg)
Java Development
HEXAGONAL ARCHITECTURE for Java Applications
![Page 2: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/2.jpg)
01 Concepts
Software Architecture
![Page 3: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/3.jpg)
01 What drives the software architecture?
• High Maintainability • Low Technical Debt: The cost payed by bad decisions
• Lack of understanding • Lack of tests • Lack of documentation • Lack of collaboration • Lack of process
![Page 4: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/4.jpg)
01 Common multi-layer architecture
!
"
#
"
! UI Layer" Application Layer# Data" Crosscutting
Dependency
![Page 5: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/5.jpg)
01 Multi-tier architecture
Web Application Tier
! !
" ""
# # #
" ""
!
"
Client Tier
Browsers, Frontend App Mobile devices, PC$Q
%
!+
Data / Integration TierDatabase, File Storage, External Systems, Cloud
&
ERPCRMDOCs
Dependency
![Page 6: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/6.jpg)
01 Dependency inversion principle
Reference: http://blog.ploeh.dk/2013/12/03/layers-onions-ports-adapters-its-all-the-same
Dependency
DANGER - Possible business
logic leaks
![Page 7: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/7.jpg)
02 Hexagonal Architecture
The Pattern
![Page 8: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/8.jpg)
02 Intent
• Alternative names • Ports & Adapters • Onion Architecture / Onion Layers
“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.”
Alistair Cockburn
![Page 9: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/9.jpg)
02 Key Concepts
Ports are interfaces
Adapters are implementations
![Page 10: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/10.jpg)
The big picture: Ports & Adapters02
"Application
PortPo
rtPort
MockDB
DB
Integration
Test Agent
UI
Adapter
Adapter
Adapter
Adapter
Use Case Boundary
‘’driven’’ adapters ‘’driver’’ adapters
![Page 11: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/11.jpg)
Overview02
In-Memory
"Application
# Database( Web Services
)
) Enterprise Integration
*
SQL Server
REST Adapter
* Configuration
+
Test Agent
!HTML5 App
Environment Adapter
File Source Adapter
,
SQL Adapter
#
NoSQL Adapter
- MQ Service
MQ Adapter
.
. Messaging Queue
&ERPCRMDOCs
EI Adapter
(SOAP Adapter
TEST Adapter
![Page 12: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/12.jpg)
03 Application
Keeping it simple
![Page 13: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/13.jpg)
03
• Organizing Concerns and Code with Domains • Well defined boundaries between domains • Simple event-driven components • Life Preserver diagram
Life preserver approach by Russ Miles
![Page 14: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/14.jpg)
Life Preserver diagram03
Integration
Core
Domain Event
Service
Domain Boundary
![Page 15: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/15.jpg)
Notes03
• Core • Single purpose
• Focus on unit testing
• No side-effects
• Integration • Side effects
![Page 16: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/16.jpg)
04 Practice
Ticket System
![Page 17: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/17.jpg)
Kick-off04
github.com/fabricioepa/lab-
springboot-rest-sample
Lets start from the sample project and
then create the ports and adapters
![Page 18: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/18.jpg)
05 Core
Use Case as service
![Page 19: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/19.jpg)
Implementing the Use Case as Service05
• Domain model • Entities
• Repositories
• Well defined interface • Input/Output Events
• Event Handlers & Translators
• Service composition
• Unit Testing
![Page 20: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/20.jpg)
Life Preserver
Services Domain Event
Repository
Databas
eTest
UnitTest Framework
0405
![Page 21: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/21.jpg)
06 Controllers
![Page 22: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/22.jpg)
Implementing passive controllers• Dummy service invokers • Do not perform multiple service calls • It does not make any workflow decision • REST Domain translator
0305
• UC • Service
• Services
• Domain
• Controller
REST Domain
06
![Page 23: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/23.jpg)
Life Preserver
Services Domain Event
Repository
Databas
eTest
UnitTest Framework
Domain
ControllerREST
030506
![Page 24: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/24.jpg)
07 Configuration
![Page 25: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/25.jpg)
Externalized configuration
• External files • Environment variables • Configuration profiles
• Services
030507
![Page 26: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/26.jpg)
MySQL
Life Preserver
Services Domain Event
Repository
Databas
eTest
UnitTest Framework
Domain
ControllerREST
ConfigurationAppSettings
TestClient
H2in-memory
Environment
030507
![Page 27: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/27.jpg)
Comments
• How many ports? • The hexagon is a metaphor • Abstractions
• Use Case as port • Module as port: Notification, Administration,
Public API, Database
0303
• Services
![Page 28: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/28.jpg)
See more…
• RESTful API with Spring Boot
• Securing REST APIs
• References: [1] alistair.cockburn.us/Hexagonal+architecture
[2] www.infoq.com/news/2013/08/hexagonal-lifepreserver-spring
[3] github.com/russmiles/life-preserver-introductory-article-developer-magazine/
blob/master/README.md
[4] github.com/spring-guides/deprecate-tut-rest
[5] blog.ploeh.dk/2013/12/03/layers-onions-ports-adapters-its-all-the-same
/ fabricioepa.wordpress.com
![Page 29: Hexagonal architecture for java applications](https://reader034.fdocuments.us/reader034/viewer/2022042423/55a5c3631a28ab5e588b45aa/html5/thumbnails/29.jpg)
Contact
FABRICIO EPAMINONDAS 0 linkedin.com/in/fabricioepa
/ fabricioepa.wordpress.com
1 @fabricioepa