hexagonal architecture for java applications

29
Java Development HEXAGONAL ARCHITECTURE for Java Applications

Upload: fabricio-epaminondas

Post on 15-Jul-2015

1.647 views

Category:

Software


121 download

TRANSCRIPT

Page 1: Hexagonal architecture for java applications

Java Development

HEXAGONAL ARCHITECTURE for Java Applications

Page 2: Hexagonal architecture for java applications

01 Concepts

Software Architecture

Page 3: Hexagonal architecture for java applications

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

01 Common multi-layer architecture

!

"

#

"

! UI Layer" Application Layer# Data" Crosscutting

Dependency

Page 5: Hexagonal architecture for java applications

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

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

02 Hexagonal Architecture

The Pattern

Page 8: Hexagonal architecture for java applications

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

02 Key Concepts

Ports are interfaces

Adapters are implementations

Page 10: Hexagonal architecture for java applications

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

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

03 Application

Keeping it simple

Page 13: Hexagonal architecture for java applications

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

Life Preserver diagram03

Integration

Core

Domain Event

Service

Domain Boundary

Page 15: Hexagonal architecture for java applications

Notes03

• Core • Single purpose

• Focus on unit testing

• No side-effects

• Integration • Side effects

Page 16: Hexagonal architecture for java applications

04 Practice

Ticket System

Page 17: Hexagonal architecture for java applications

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

05 Core

Use Case as service

Page 19: Hexagonal architecture for java applications

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

Life Preserver

Services Domain Event

Repository

Databas

eTest

UnitTest Framework

0405

Page 21: Hexagonal architecture for java applications

06 Controllers

Page 22: Hexagonal architecture for java applications

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

Life Preserver

Services Domain Event

Repository

Databas

eTest

UnitTest Framework

Domain

ControllerREST

030506

Page 24: Hexagonal architecture for java applications

07 Configuration

Page 25: Hexagonal architecture for java applications

Externalized configuration

• External files • Environment variables • Configuration profiles

• Services

030507

Page 26: Hexagonal architecture for java applications

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

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

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

Contact

FABRICIO EPAMINONDAS 0 linkedin.com/in/fabricioepa

/ fabricioepa.wordpress.com

1 @fabricioepa