Introduction to
Clean Architecture
Ivan Paulovich
Stockholm
Software Architecture Meetup
June 29th, 2020
https://paulovich.net
@ivanpaulovich
Slide 2
Slide 2 text
Paulovich.NET
@ivanpaulovich
Slide 3
Slide 3 text
What is the Clean Architecture
Style?
@ivanpaulovich
Slide 4
Slide 4 text
Each of these architectures produce systems that are:
• Independent of Frameworks.
• Testable.
• Independent of UI.
• Independent of Database.
• Independent of any external agency.
https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
“
@ivanpaulovich
Clean Architecture Style
Hexagonal Architecture Style
Ports and Adapters
Pattern
Dependency Inversion
Principle
Test-Driven Development
Clean Architecture Style
Object-Oriented Design
Principles
Use Cases as Central
Organizing Structure
Pluggable User Interface
@ivanpaulovich
Slide 10
Slide 10 text
Hexagonal Architecture Style
•Ports and Adapters pattern.
•The implementation is guided by tests.
•It is decoupled from technology details.
@ivanpaulovich
Slide 11
Slide 11 text
Business
@ivanpaulovich
11
Ports and Adapters
Cloud
Messaging
UI
In Memory
Persistence
Tests
In Memory
Messaging
SQL
Slide 12
Slide 12 text
Business
@ivanpaulovich
12
Ports and Adapters
Cloud
Messaging
UI
In Memory
Persistence
Tests
In Memory
Messaging
SQL
Ports
Slide 13
Slide 13 text
Ports
Slide 14
Slide 14 text
Business
v
@ivanpaulovich
14
Ports and Adapters
Cloud
Messaging
UI
In Memory
Persistence
Tests
In Memory
Messaging
SQL
Adapters
Slide 15
Slide 15 text
Adapters
Slide 16
Slide 16 text
Application
Unit Test
Web
Database
Domain
Primary
Actors
Secondary
Actors
@ivanpaulovich
16
Infrastructure
Ports and Adapters
Slide 17
Slide 17 text
Use Cases
• Use Cases show the intent of a
system.
• Use Cases are delivery independent.
• Use Cases are algorithms that
interpret the input to generate the
output data.
• Use Cases diagrams highlight the
Primary and secondary actors.
17 @ivanpaulovich
Slide 18
Slide 18 text
Deposit Use Case
@ivanpaulovich
Slide 19
Slide 19 text
Deposit Use Case Input and Output Messages
@ivanpaulovich
Slide 20
Slide 20 text
Deposit Use Case
Request/Response
@ivanpaulovich
Slide 21
Slide 21 text
Controller
Use Case
Core
User Interface
@ivanpaulovich
Request
Response
Input
Output
• Abstract
• General
• Stable
• Consistent
• Concrete
• Specific
• Unstable
• Inconsistent
Slide 22
Slide 22 text
Object-Oriented Design
Principles
Slide 23
Slide 23 text
Entities
Entities
Clean Architecture
Entities
Use Cases
Controllers
Gatew
ays
Presenters
D
evices W
eb
UI
DB
External Interfaces
@ivanpaulovich
•Abstractness increases with
stability.
•Modules depend in the
direction of stability.
•Classes that change together
are packaged together.
Slide 24
Slide 24 text
Entities
Use Cases
Controllers
Presenters
Gateways
Devices UI
DB Web
External Interfaces
Abstract,
General,
Stable,
Consistent
Concrete,
Specific,
Unstable,
Inconsistent
Level
Clean Architecture
@ivanpaulovich
Slide 25
Slide 25 text
The Stable Dependencies Principle¹
¹Clean Architecture, Robert C. Martin, 2017
Slide 26
Slide 26 text
The Stable Dependencies Principle
Infrastructure Web
Domain
Application
Slide 27
Slide 27 text
The Stable Dependencies Principle
Infrastructure Web
Domain
Application
Shinning Framework Shinning Library
Slide 28
Slide 28 text
The Stable Dependencies Principle
Infrastructure Web
Domain
Application
Shinning Framework Shinning Library
Slide 29
Slide 29 text
The Stable Dependencies Principle
Infrastructure Web
Domain
Application
Shinning Framework Shinning Library
Slide 30
Slide 30 text
Walkthrough an Use Case
Controller
@ivanpaulovich
Slide 31
Slide 31 text
Walkthrough an Use Case
Use Case
@ivanpaulovich
Slide 32
Slide 32 text
Walkthrough an Use Case
Repository
@ivanpaulovich
Slide 33
Slide 33 text
Walkthrough an Use Case
Use Case
@ivanpaulovich
Slide 34
Slide 34 text
Walkthrough an Use Case
Domain Service
@ivanpaulovich
Slide 35
Slide 35 text
Walkthrough an Use Case
Repository
@ivanpaulovich
Slide 36
Slide 36 text
Walkthrough an Use Case
Use Case
@ivanpaulovich
Slide 37
Slide 37 text
Walkthrough an Use Case
Unit of Work
@ivanpaulovich
Slide 38
Slide 38 text
Walkthrough an Use Case
Use Case
@ivanpaulovich
Slide 39
Slide 39 text
Walkthrough an Use Case
Use Case
@ivanpaulovich
Slide 40
Slide 40 text
Walkthrough an Use Case
@ivanpaulovich
Controller
Slide 41
Slide 41 text
Testing Strategy
Slide 42
Slide 42 text
A Web of Objects
@ivanpaulovich
Slide 43
Slide 43 text
Testing
?
?
?
Test
Expectations
@ivanpaulovich
Slide 44
Slide 44 text
The First Test
Deposit
Use Case
Customer
Repository
Account
Repository
Test
Expectations
@ivanpaulovich
Slide 45
Slide 45 text
The Walking Skeleton
Deposit
Use Case
Customer
Repository
Account
Repository
Fakes
User
Interface
@ivanpaulovich
Slide 46
Slide 46 text
Deposit
Use Case
Customer
Repository
Account
Repository
Fakes
User
Interface
Integration Tests
s
Account
Repository
Test
Customer
Repository
Slide 47
Slide 47 text
Tests Overview
In Process Out of Process
Memory Within the application memory Reads I/O. Secondary memory.
Speed Fast Slow
Setup Effort Easy to design a variety of cases Requires extensive setup
Samples Use Cases, Domain Repositories, Services
Slide 48
Slide 48 text
Software Skeleton
Gateways
Controllers
Use Cases
Domain
Web
Service
Repositories
Database
Slide 49
Slide 49 text
Unit Tests
Gateways
Controllers
Use Cases
Domain
Repositories
Unit Tests
Fake
Web Service
Fake
Repository
Slide 50
Slide 50 text
Integration Tests
Gateways
Controllers
Use Cases
Domain
Web
Service
Repositories
Database
Integration
Tests
Slide 51
Slide 51 text
Component Tests
Gateways
Controllers
Use Cases
Domain
Repositories
Component
Tests
Fake
Web Service
Fake
Repository
Slide 52
Slide 52 text
Test Pyramid
Unit Tests
Component
Tests
Integration
Tests
Slide 53
Slide 53 text
Wrapping up Clean Architecture
• Clean Architecture is about usage and the use cases are the central
organizing structure.
• Use cases implementation are guided by tests.
• The User Interface and Persistence are designed to fulfil the core
needs (not the opposite!).
• Defer decisions by implementing the simplest component first.
53
@ivanpaulovich
Slide 54
Slide 54 text
References
https://cleancoders.com
Clean Code: Component Design
Clean Code: SOLID Principles
Clean Code: Fundamentals
https://github.com/ivanpaulovich/clean-architecture-manga
Robert C Martin - Clean
Architecture and Design
@ivanpaulovich