SlideShare a Scribd company logo
ADVANCED
APPLICATION
ARCHITECTURE
I - Layers
Matthias Noback
@matthiasnoback
matthiasnoback/

layers-ports-and-adapters-
workshop
Clone from GitHub
docker-compose pull
bin/composer.sh install
docker-compose up -d
ARCHITECTURE
Decisions about coupling and cohesion
COUPLING
How are things linked to each other?
COHESION
Which things belong together?
LAYERED ARCHITECTURE
An answer to both questions
LAYERS
Help you protect what's in a deeper layer
LAYERS
Allow you to define dependency rules
LAYERS
Define the right place to put things
TRADITIONAL LAYERING
view
model
data
LAYERS
Horizontal division (as opposed to...)
USE CASES
Vertical division
LAYERS
Rendered as circles
CLEAN ARCHITECTURE
Or "union" architecture
COHESION
A basic set of layers
infrastructure
application
domain
COUPLING
The dependency rule
Layers should only depend on deeper layers
COUPLING
Dependency inversion principle
Classes should always depend on things that are more abstract
COUPLING
Dependency inversion principle
low-levelconcrete class specific
abstract interface generic high-level
COUPLING
Dependency inversion principle
use
COUPLING
Dependency inversion principle
use implement
COUPLING
Is about dependencies in code
Use Dependency Injection
LAYER CONVENTIONS
Domain layer
Domain model:
➤ Entities
➤ Value objects
➤ Domain services
➤ Factories
Business logic
Decisions
Data
State
Behavior
LAYER CONVENTIONS
Application layer
➤ Find an object
➤ Change something
➤ Notify something
➤ Get some information
Use cases
Orchestration
LAYER CONVENTIONS
Infrastructure layer
Communication with:
➤ HTTP client
➤ Database
➤ Filesystem
➤ Email server
➤ Message broker
Connecting the
application to
the world
outside
MATTHIASNOBACK/LAYERS-
PORTS-AND-ADAPTERS-
WORKSHOP
Assignments are on GitHub in assignments/
assignment/01.md
Layers
ADVANCED
APPLICATION
ARCHITECTURE
II - Ports & adapters
MESSAGING
... is the big idea
PORTS (COHESION)
Web, CLI, test client, messages, database queries
User interface
Persistence
PORTS... AND ADAPTERS
Translation
HTTP
SQL
HEXAGONAL ARCHITECTURE
Same thing!
User interface
Persistence
PORTS... AND ADAPTERS
(De)serialization, translation, structural validation
HTTP
SQL
assignment/02.md
Ports
PORT ADAPTERS
...in the Infrastructure layer
APPLICATION SERVICES
... in the Application layer
DOMAIN MODEL
in the Domain layer
APPLICATION SERVICES
Delivery-mechanism agnostic
assignment/03.md
Input adapters
VALIDATION
At different levels
Structural validation of messages
User-friendly input validation
Domain invariant protection
assignment/04.md
Input validation
APPLICATION SERVICES
Transactional consistency
assignment/05.md
Output adapter
assignment/06.md
A hidden dependency on the persistence mechanism
ADVANTAGES OF USING LAYERS, 

PORTS & ADAPTERS
Offers insight into the application
Isolates the low-level details
Allows for alternative implementations
Helps with testing
ADVANCED
APPLICATION
ARCHITECTURE
III - Testing
UNIT TESTS
Testing your units of code
One class at a time
No IO
No setup required
Mocking only dependencies "you own"
INTEGRATION TESTS
Testing your adapters
Maybe multiple classes
Including IO
Some setup required
Mocking no dependencies
ACCEPTANCE TESTS
Testing your application services
Multiple classes
Use cases
Infrastructure stand-ins
SYSTEM TESTS
Testing your application end-to-end
The real deal
TESTING PYRAMID
Make it well-balanced
Unit
Integration
System
Acceptance
assignment/07.md
Different types of tests
ACCEPTANCE TESTS
Use infrastructure stand-ins
Acceptance test
test the
application
layer
Persistence
FileBased InMemory
Port
Adapters
MeetupRepository
FileBasedMeetupRepository InMemoryMeetupRepository
Domain
Infrastructure
implements implements
assignment/08.md
A test double for the Persistence port
assignment/09.md
An acceptance test for the application layer
A WELL-BALANCED TEST SUITE
Adapters can be easily replaced
Test suite is fast
Feedback is quick
Small amount of fragile tests
Enables continuous delivery
assignment/10.md
Notifications
https://training.matthiasnoback.nl/
https://joind.in/talk/b1763
Feedback?

More Related Content

Advanced application architecture