Ports & Adapters Architecture - XP Days 2017

Preview:

Citation preview

Ports & Adapters Architecture

Inside & Outside Application Thinking

Nathan Johnstone - Technical Pathfinder @ Coolblue

Focus

A Bit About Me ...

A Bit About Me ...

Pigs != Pegs

The Starting Point

Joining a Team

Infrastructure Dependencies

Web APIMessage Service Scheduler

Application Assemblies

Web APIMessage Service Scheduler

Data Access Domain Common

Application Dependencies

Web APIMessage Service Scheduler

Data Access Domain Common

Class Library Dependencies

Web APIMessage Service Scheduler

Data Access Domain Common

N-Tier

Web API

Data Access

Domain

Isn't Something Missing ?

Pain

Leakage

DRY - Reuse Reuse Reuse !

Test Coverage

Unit Tests

Component Tests

Acceptance Tests

Integration Tests

Implementing Integration Tests

Interfaces Colocated With Implementations

Data Access Domain

PurchaseOrderSubmitServiceIPurchaseOrderRetriever

PurchaseOrderRetriever

?

Who Owns The Interface ?

Colocation Problems

Data Access Domain

PurchaseOrderSubmitServiceIPurchaseOrderRetriever

PurchaseOrderPurchaseOrderDocument

Isolation Violation

PurchaseOrderDocument

Domain

Reference Direction vs Control Direction

Presentation Layer

Data AccessLayer

Business LogicLayer

Reference DirectionControl Direction

Ports & Adapters Introduction

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.

Hexagonal Architecture

Inside Outside Architecture

First Port - Persistence

Web APIMessage Service Scheduler

Data Access Domain Common

PersistenceAdapter

Mikado Method

Second Port - Commands

Web APIMessage Service Scheduler

Data Access Domain

PersistenceAdapter

CommandAdapter

Upstream & Downstream Ports

Third Port - Notification

Web APIMessage Service Scheduler

Data Access Domain

PersistenceAdapter

CommandAdapter

NotificationAdapter

Clean Up

Web APIMessage Service Scheduler

Data Access Domain

PersistenceAdapter

CommandAdapter

NotificationAdapter

Hexagonal - Domain

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Hexagonal - Persistence Port

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Persistence

Port

Hexagonal - Command Port

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Persistence

Port

Com

man

d Po

rt

Hexagonal - Notification Port

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Persistence

Port

Com

man

d Po

rt

Notification Port

Hosts Are Types of Adapters

Web Service

Test Harness

Command Line

Daemon

Windows Service

Graphical UI

Hexagonal - Host Port

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Persistence

Port

Com

man

d Po

rt

Notification Port

Host Port

Advantages - Domain Testability

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Persistence

Port

Com

man

d Po

rt

Notification Port

Host Port

Advantages - Boundaries

Web APIMessage Service Scheduler

Domain

PersistenceAdapter

CommandAdapter

NotificationAdapter

Advantages - Alternative Adapters

Domain

Web APIMessage Service Scheduler

Persistence.Adapter Command.Adapter

NotificationAdapter

Persistence

Port

Com

man

d Po

rt

Notification Port

Host Port

Persistence.AdapterCsv

Command.AdapterFile

Notification via SMTP

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Persistence

Port

Com

man

d Po

rt

Notification Port

Host Port

Code Examples - Book Ordering Service

Book Ordering Service - Client 1

● REST endpoints for adding books,

approving and submitting

● Notification to supplier to be via SMTP

● Persistence to MySQL

Book Ordering Service - Client 2

● RabbitMq message for adding books

● REST endpoints for adding approving and

submitting

● Notification to supplier to be via

RabbitMq message

● Persistence to MySQL

Book Ordering Service - Architecture

Domain

Host

Web APIClient 1

Web API Client 2

Pers

isten

ce

MySQLNotification

SMTPRabbitMq

Comm

andRabbitMq

Warning Live Code Demo Ahead !

Takeaways● Ports are just a name given to a common group of abstractions in your

domain

● Adapters are a group of specific implementations of a given port

● Onion Architecture, Clean Architecture and Ports & Adapters are essentially the same thing

● Example repository on github for .NET

● Ports & Adapters is an architectural style that will produce applications that are independent of infrastructure details

Links● http://alistair.cockburn.us/Hexagonal+architecture

● http://blog.ploeh.dk/2013/12/03/layers-onions-ports-adapters-its-all-the-same/

● https://github.com/nrjohnstone/ports-adapters-examples

● https://www.linkedin.com/in/nrjohnstone

● https://nathanjohnstone.wordpress.com/

● n.johnstone@coolblue.nl

● locotewz@gmail.com

Recommended