Ports & Adapters Architecture - XP Days 2017

52
Ports & Adapters Architecture Inside & Outside Application Thinking Nathan Johnstone - Technical Pathfinder @ Coolblue

Transcript of Ports & Adapters Architecture - XP Days 2017

Page 1: Ports & Adapters Architecture - XP Days 2017

Ports & Adapters Architecture

Inside & Outside Application Thinking

Nathan Johnstone - Technical Pathfinder @ Coolblue

Page 2: Ports & Adapters Architecture - XP Days 2017

Focus

Page 3: Ports & Adapters Architecture - XP Days 2017
Page 4: Ports & Adapters Architecture - XP Days 2017
Page 5: Ports & Adapters Architecture - XP Days 2017

A Bit About Me ...

Page 6: Ports & Adapters Architecture - XP Days 2017

A Bit About Me ...

Page 7: Ports & Adapters Architecture - XP Days 2017
Page 8: Ports & Adapters Architecture - XP Days 2017

Pigs != Pegs

Page 9: Ports & Adapters Architecture - XP Days 2017

The Starting Point

Page 10: Ports & Adapters Architecture - XP Days 2017

Joining a Team

Page 11: Ports & Adapters Architecture - XP Days 2017

Infrastructure Dependencies

Web APIMessage Service Scheduler

Page 12: Ports & Adapters Architecture - XP Days 2017

Application Assemblies

Web APIMessage Service Scheduler

Data Access Domain Common

Page 13: Ports & Adapters Architecture - XP Days 2017

Application Dependencies

Web APIMessage Service Scheduler

Data Access Domain Common

Page 14: Ports & Adapters Architecture - XP Days 2017

Class Library Dependencies

Web APIMessage Service Scheduler

Data Access Domain Common

Page 15: Ports & Adapters Architecture - XP Days 2017

N-Tier

Web API

Data Access

Domain

Page 16: Ports & Adapters Architecture - XP Days 2017

Isn't Something Missing ?

Page 17: Ports & Adapters Architecture - XP Days 2017

Pain

Page 18: Ports & Adapters Architecture - XP Days 2017

Leakage

Page 19: Ports & Adapters Architecture - XP Days 2017

DRY - Reuse Reuse Reuse !

Page 20: Ports & Adapters Architecture - XP Days 2017

Test Coverage

Unit Tests

Component Tests

Acceptance Tests

Integration Tests

Page 21: Ports & Adapters Architecture - XP Days 2017

Implementing Integration Tests

Page 22: Ports & Adapters Architecture - XP Days 2017

Interfaces Colocated With Implementations

Data Access Domain

PurchaseOrderSubmitServiceIPurchaseOrderRetriever

PurchaseOrderRetriever

?

Page 23: Ports & Adapters Architecture - XP Days 2017

Who Owns The Interface ?

Page 24: Ports & Adapters Architecture - XP Days 2017

Colocation Problems

Data Access Domain

PurchaseOrderSubmitServiceIPurchaseOrderRetriever

PurchaseOrderPurchaseOrderDocument

Page 25: Ports & Adapters Architecture - XP Days 2017

Isolation Violation

PurchaseOrderDocument

Domain

Page 26: Ports & Adapters Architecture - XP Days 2017

Reference Direction vs Control Direction

Presentation Layer

Data AccessLayer

Business LogicLayer

Reference DirectionControl Direction

Page 27: Ports & Adapters Architecture - XP Days 2017

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.

Page 28: Ports & Adapters Architecture - XP Days 2017

Hexagonal Architecture

Page 29: Ports & Adapters Architecture - XP Days 2017

Inside Outside Architecture

Page 30: Ports & Adapters Architecture - XP Days 2017

First Port - Persistence

Web APIMessage Service Scheduler

Data Access Domain Common

PersistenceAdapter

Page 31: Ports & Adapters Architecture - XP Days 2017

Mikado Method

Page 32: Ports & Adapters Architecture - XP Days 2017

Second Port - Commands

Web APIMessage Service Scheduler

Data Access Domain

PersistenceAdapter

CommandAdapter

Page 33: Ports & Adapters Architecture - XP Days 2017

Upstream & Downstream Ports

Page 34: Ports & Adapters Architecture - XP Days 2017

Third Port - Notification

Web APIMessage Service Scheduler

Data Access Domain

PersistenceAdapter

CommandAdapter

NotificationAdapter

Page 35: Ports & Adapters Architecture - XP Days 2017

Clean Up

Web APIMessage Service Scheduler

Data Access Domain

PersistenceAdapter

CommandAdapter

NotificationAdapter

Page 36: Ports & Adapters Architecture - XP Days 2017

Hexagonal - Domain

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Page 37: Ports & Adapters Architecture - XP Days 2017

Hexagonal - Persistence Port

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Persistence

Port

Page 38: Ports & Adapters Architecture - XP Days 2017

Hexagonal - Command Port

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Persistence

Port

Com

man

d Po

rt

Page 39: Ports & Adapters Architecture - XP Days 2017

Hexagonal - Notification Port

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Persistence

Port

Com

man

d Po

rt

Notification Port

Page 40: Ports & Adapters Architecture - XP Days 2017

Hosts Are Types of Adapters

Web Service

Test Harness

Command Line

Daemon

Windows Service

Graphical UI

Page 41: Ports & Adapters Architecture - XP Days 2017

Hexagonal - Host Port

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Persistence

Port

Com

man

d Po

rt

Notification Port

Host Port

Page 42: Ports & Adapters Architecture - XP Days 2017

Advantages - Domain Testability

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Persistence

Port

Com

man

d Po

rt

Notification Port

Host Port

Page 43: Ports & Adapters Architecture - XP Days 2017

Advantages - Boundaries

Web APIMessage Service Scheduler

Domain

PersistenceAdapter

CommandAdapter

NotificationAdapter

Page 44: Ports & Adapters Architecture - XP Days 2017

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

Page 45: Ports & Adapters Architecture - XP Days 2017

Notification via SMTP

Domain

Web APIMessage Service Scheduler

PersistenceAdapter

CommandAdapter

NotificationAdapter

Persistence

Port

Com

man

d Po

rt

Notification Port

Host Port

Page 46: Ports & Adapters Architecture - XP Days 2017

Code Examples - Book Ordering Service

Page 47: Ports & Adapters Architecture - XP Days 2017

Book Ordering Service - Client 1

● REST endpoints for adding books,

approving and submitting

● Notification to supplier to be via SMTP

● Persistence to MySQL

Page 48: Ports & Adapters Architecture - XP Days 2017

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

Page 49: Ports & Adapters Architecture - XP Days 2017

Book Ordering Service - Architecture

Domain

Host

Web APIClient 1

Web API Client 2

Pers

isten

ce

MySQLNotification

SMTPRabbitMq

Comm

andRabbitMq

Page 50: Ports & Adapters Architecture - XP Days 2017

Warning Live Code Demo Ahead !

Page 51: Ports & Adapters Architecture - XP Days 2017

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

Page 52: Ports & Adapters Architecture - XP Days 2017

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/

[email protected]

[email protected]