Java Development HEXAGONAL ARCHITECTURE for Java Applications
Java Development
HEXAGONAL ARCHITECTURE for Java Applications
01 Concepts
Software Architecture
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
01 Common multi-layer architecture
!
"
#
"
! UI Layer" Application Layer# Data" Crosscutting
Dependency
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
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
02 Hexagonal Architecture
The Pattern
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
02 Key Concepts
Ports are interfaces
Adapters are implementations
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
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
03 Application
Keeping it simple
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
Life Preserver diagram03
Integration
Core
Domain Event
Service
Domain Boundary
Notes03
• Core • Single purpose
• Focus on unit testing
• No side-effects
• Integration • Side effects
04 Practice
Ticket System
Kick-off04
github.com/fabricioepa/lab-
springboot-rest-sample
Lets start from the sample project and
then create the ports and adapters
05 Core
Use Case as service
Implementing the Use Case as Service05
• Domain model • Entities
• Repositories
• Well defined interface • Input/Output Events
• Event Handlers & Translators
• Service composition
• Unit Testing
Life Preserver
Services Domain Event
Repository
Databas
eTest
UnitTest Framework
0405
06 Controllers
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
Life Preserver
Services Domain Event
Repository
Databas
eTest
UnitTest Framework
Domain
ControllerREST
030506
07 Configuration
Externalized configuration
• External files • Environment variables • Configuration profiles
• Services
030507
MySQL
Life Preserver
Services Domain Event
Repository
Databas
eTest
UnitTest Framework
Domain
ControllerREST
ConfigurationAppSettings
TestClient
H2in-memory
Environment
030507
Comments
• How many ports? • The hexagon is a metaphor • Abstractions
• Use Case as port • Module as port: Notification, Administration,
Public API, Database
0303
• Services
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
Contact
FABRICIO EPAMINONDAS 0 linkedin.com/in/fabricioepa
/ fabricioepa.wordpress.com
1 @fabricioepa