Clean Architecture PDF
Clean Architecture PDF
@MatthewRenze
#CodeMash
Overview
1. Clean Architecture
Overview
1. Clean Architecture
2. Domain-Centric Architecture
Overview
1. Clean Architecture
2. Domain-Centric Architecture
3. Application Layer
Overview
1. Clean Architecture
2. Domain-Centric Architecture
3. Application Layer
4. Commands and Queries
Overview
1. Clean Architecture
2. Domain-Centric Architecture
3. Application Layer
4. Commands and Queries
5. Functional Organization
Overview
1. Clean Architecture
2. Domain-Centric Architecture
3. Application Layer
4. Commands and Queries
5. Functional Organization
6. Microservices
Focus
Enterprise Architecture
Focus
Enterprise Architecture
Modern equivalent of 3-Layer
Focus
Enterprise Architecture
Modern equivalent of 3-Layer
Generally applicable
Focus
Enterprise Architecture
Modern equivalent of 3-Layer
Generally applicable
6 Key Points
Focus
Enterprise Architecture
Modern equivalent of 3-Layer
Generally applicable
6 Key Points
Q&A
What is Software Architecture?
Source: http://msdn.microsoft.com/
en-us/library/ff650706.aspx
What is Software Architecture?
High-level
Source: http://msdn.microsoft.com/
en-us/library/ff650706.aspx
What is Software Architecture?
High-level
Structure
Source: http://msdn.microsoft.com/
en-us/library/ff650706.aspx
What is Software Architecture?
High-level
Structure
Layers
Source: http://msdn.microsoft.com/
en-us/library/ff650706.aspx
What is Software Architecture?
High-level
Structure
Layers
Components
Source: http://msdn.microsoft.com/
en-us/library/ff650706.aspx
What is Software Architecture?
High-level
Structure
Layers
Components
Relationships
Source: http://msdn.microsoft.com/
en-us/library/ff650706.aspx
Levels of Architectural Abstraction
System
Sub-systems
Layers
Components
Classes
System
Sub-systems
Layers
Components
Classes
Complex
Inconsistent
Incoherent
Rigid
Brittle
Untestable
Unmaintainable
What Is Clean Architecture?
Simple
Understandable
Flexible
Emergent
Testable
Maintainable
What Is Clean Architecture?
Cost/benefit
Why Is Clean Architecture Important?
Cost/benefit
Minimize cost to maintain
Why Is Clean Architecture Important?
Cost/benefit
Minimize cost to maintain
Maximize business value
Why Is Clean Architecture Important?
Cost/benefit
Minimize cost to maintain
Maximize business value
Maximize total ROI
Decisions, Decisions, Decisions…
Context is king
Decisions, Decisions, Decisions…
Context is king
All decisions are a tradeoff
Decisions, Decisions, Decisions…
Context is king
All decisions are a tradeoff
Use your best judgement
Domain-Centric Architecture
Venus
Mercury
Earth
Mars
Sun
Moon
Saturn
Jupiter
Earth
Mercury
Mars
Venus Sun
Saturn
Jupiter
Venus Earth
Mercury
Earth Mercury
Mars Mars
Sun Venus Sun
Moon
Saturn Saturn
Jupiter Jupiter
UI
Business Logic
Data Access
Database
Database
Database- vs. Domain-centric Architecture
UI Presentation
Data Access
Database Domain
Database
Database
Database
“The first concern of the architect is
to make sure that the house is
usable, it is not to ensure that the
house is made of brick.”
– Uncle Bob
Essential vs. Detail
Essential vs. Detail
Space is essential
Essential vs. Detail
Space is essential
Usability is essential
Essential vs. Detail
Domain is essential
Essential vs. Detail
Domain is essential
Use cases are essential
Essential vs. Detail
Presentation is a detail
Essential vs. Detail
Presentation is a detail
Persistence is a detail
Database- vs. Domain-centric Architecture
UI Presentation
Data Access
Database Domain
Database
Database
Database
Database- vs. Domain-centric Architecture
UI
Business Logic
Data Access
Database
Database
Database- vs. Domain-centric Architecture
Presentation
Application
Domain
Database
Database
Hexagonal Architecture
answering
machine
adapter
email
adapter
http
feed
mock
telephone
Application
GUI
database
http adapter
adapter DB
app-to-app mock
adapter database
app
test
adapter
Application Services
Domain Services
Domain
Database
Model
Application Core
web
service
file
DB
Controllers
Use Cases
Database
Entities
I
Use case
Presenter
output port
Flow
of Use case
Control interactor
I
Use case
External Controller
input port
Interfaces
Original source: http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
It’s All the Same Thing
Levels of abstraction
Single-Responsibility Principle
Developer roles / skills
Multiple implementations
Varying rates of change
Classic 3-Layer Architecture
Users
UI
Business Logic
Data Access
Database
Dependency
Modern 4-Layer Architecture
Users
Presentation
Cross-Cutting Concerns
Application
Domain
Persistence Infrastructure
Database OS
Dependency
Application Layer
Users
Cross-Cutting Concerns
Application
Domain
Persistence Infrastructure
Database OS
Dependency
Application Layer
Users
Cross-Cutting Concerns
High-level application logic Application
Domain
Persistence Infrastructure
Database OS
Dependency
Application Layer
Users
Cross-Cutting Concerns
High-level application logic Application
Knows about domain
Domain
Persistence Infrastructure
Database OS
Dependency
Application Layer
Users
Cross-Cutting Concerns
High-level application logic Application
Knows about domain
Domain
No knowledge of other layers
Persistence Infrastructure
Database OS
Dependency
Application Layer
Users
Cross-Cutting Concerns
High-level application logic Application
Knows about domain
Domain
No knowledge of other layers
Contains interfaces for details Persistence Infrastructure
Database OS
Dependency
Layer Dependencies
Users
Presentation
Cross-Cutting Concerns
Application
Domain
Persistence Infrastructure
Database OS
Users
Presentation
Dependency inversion
Cross-Cutting Concerns
Application
Domain
Persistence Infrastructure
Database OS
Users
Presentation
Dependency inversion
Cross-Cutting Concerns
Inversion of control Application
Domain
Persistence Infrastructure
Database OS
Users
Presentation
Dependency inversion
Cross-Cutting Concerns
Inversion of control Application
Persistence Infrastructure
Database OS
Users
Presentation
Dependency inversion
Cross-Cutting Concerns
Inversion of control Application
Database OS
SalesController
Application
Cross-Cutting Concerns
ICreateSaleCommand
IDateService
DateService
CreateSaleCommand
IDatabaseContext IInventoryClient
Domain
Sale
Persistence Infrastructure
Database OS
Why Use an Application Layer?
Pros
Focus is on use cases
Why Use an Application Layer?
Pros
Focus is on use cases
Easy to understand
Why Use an Application Layer?
Pros
Focus is on use cases
Easy to understand
Follows DIP
Why Use an Application Layer?
Pros Cons
Focus is on use cases Additional cost
Easy to understand
Follows DIP
Why Use an Application Layer?
Pros Cons
Focus is on use cases Additional cost
Easy to understand Requires extra thought
Follows DIP
Why Use an Application Layer?
Pros Cons
Focus is on use cases Additional cost
Easy to understand Requires extra thought
Follows DIP IoC is counter-intuitive
Commands and Queries
Command-Query Separation
Command
Does something
Should modify state
Should not return a value
Command-Query Separation
Command Query
Does something Answers a question
Should modify state Should not modify state
Should not return a value Always returns a value
Command-Query Separation
Command Query
Does something Answers a question
Should modify state Should not modify state
Should not return a value Always returns a value
(ideally)
Users
Presentation
Queries Commands
Domain
Database
Data Flow
CQRS Architectures
Users
Presentation
Queries Commands
Domain
Database
Data Flow
CQRS Architectures
Users
Presentation
Queries Commands
Domain
Database
Data Flow
CQRS Architectures
Users
Presentation
Queries Commands
Domain
Database
Data Flow
CQRS Type 1 – Single Database
Users
Presentation
Queries Commands
Domain
Database
Data Flow
CQRS Type 1 – Single Database
Users
Presentation
Queries Commands
Domain
Database
Data Flow
CQRS Type 1 – Single Database
Users
Presentation
Queries Commands
Domain
Database
Data Flow
CQRS Type 2 – Read/Write Databases
Users
Presentation
Queries Commands
Domain
Read Write
Database Database
Data Flow
CQRS Type 2 – Read/Write Databases
Users
Presentation
Queries Commands
Domain
Read Write
Database Database
Data Flow
CQRS Type 2 – Read/Write Databases
Users
Presentation
Queries Commands
Domain
Read Write
Database Database
Data Flow
CQRS Type 2 – Read/Write Databases
Users
Presentation
Queries Commands
Domain
Read Write
Database Database
Data Flow
CQRS Type 2 – Read/Write Databases
Users
Presentation
Queries Commands
Domain
Read Write
Database Database
Data Flow
CQRS Type 3 – Event Sourcing
Users
Presentation
Queries Commands
Domain
Read Event
Database Store
Data Flow
CQRS Type 3 – Event Sourcing
Users Events
Item 2 Added
Domain
Payment Made
Data Access Persistence
Sale Completed
Read Event
Database Store
Data Flow
CQRS Type 3 – Event Sourcing
Users Events
Item 2 Added
Domain
Payment Made
Data Access Persistence
Sale Completed
Read Event
Database Store
Data Flow
CQRS Type 3 – Event Sourcing
Users
Presentation
Queries Commands
Domain
Read Event
Database Store
Data Flow
CQRS Type 3 – Event Sourcing
Users
Presentation
Queries Commands
Domain
Read Event
Database Store
CQRS Type 3 – Event Sourcing
Users
Presentation
Domain
Read Event
Database Store
CQRS Type 3 – Event Sourcing
Users
Presentation
Read Event
Database Store
CQRS Type 3 – Event Sourcing
Users
Presentation
Read Event
Database Store
CQRS Type 3 – Event Sourcing
Users
Presentation
Read Event
Database Store
Why Use CQRS?
Pros
More efficient design
Why Use CQRS?
Pros
More efficient design
Simpler within each stack
Why Use CQRS?
Pros
More efficient design
Simpler within each stack
Optimized performance
Why Use CQRS?
Pros Cons
More efficient design Inconsistent across stacks
Simpler within each stack
Optimized performance
Why Use CQRS?
Pros Cons
More efficient design Inconsistent across stacks
Simpler within each stack Type 2 is more complex
Optimized performance
Why Use CQRS?
Pros Cons
More efficient design Inconsistent across stacks
Simpler within each stack Type 2 is more complex
Optimized performance Type 3 might be overkill
Functional Organization
“The architecture should scream
the intent of the system!”
– Uncle Bob
Bedroom Bedroom
Utility
Bath
Dining Room
Kitchen
Living Room
Entry
Bedroom Bedroom
Utility
Bath
Dining Room
Kitchen
Living Room
Entry
Material Quantity Cost
Appliances 5 $5,000
Cabinets 10 $2,500
Doors 15 $750
Fixtures 12 $2,400
Floors 9 $4,000
Walls 20 $10,000
Windows 8 $2,500
Models
Views Controllers
Models Customers
vs
Views Controllers Products Vendors
Content
Controllers
Models
Scripts
Views
Content Customers
Controllers Employees
Models vs Products
Scripts Sales
Views Vendors
So what?
vs
Why Use Functional Organization
Pros
Spatial locality
Why Use Functional Organization
Pros
Spatial locality
Easy to navigate
Why Use Functional Organization
Pros
Spatial locality
Easy to navigate
Avoid vendor lock-in
Why Use Functional Organization
Pros Cons
Spatial locality Lose framework conventions
Easy to navigate
Avoid vendor lock-in
Why Use Functional Organization
Pros Cons
Spatial locality Lose framework conventions
Easy to navigate Lose automatic scaffolding
Avoid vendor lock-in
Why Use Functional Organization
Pros Cons
Spatial locality Lose framework conventions
Easy to navigate Lose automatic scaffolding
Avoid vendor lock-in Categorical is easier at first
Microservices
Components
Users
Database
Components
Users
Database
Problem Domain
Sales Support
Sales Opportunity Support Ticket
Contact Customer
Sales Person Support Person
Product Product
Sales Territory Resolution
Single Domain Model
Customer
Employee
Single Domain Model
Customer
Employee
Single Domain Model
Customer
Employee
Single Domain Model
Customer
Employee
Overlapping Contexts
Sales
Support
Customer
Employee
Bounded Contexts
Sales Support
Contact Customer
Support
Sales Person
Person
Microservice Architectures
Support
Sales Inventory
Marketing HR
Microservice Architectures
Support
Marketing HR
Microservice Architectures
Support
Marketing HR
Microservice Architectures
Support
Marketing HR
Microservice Architectures
Support
Marketing HR
Microservice Architectures
Support
Marketing HR
Microservice Architectures
Support
Marketing HR
Why Use Microservices?
Pros
Less cost for large domains
Why Use Microservices?
Pros
Less cost for large domains
Smaller teams
Why Use Microservices?
Pros
Less cost for large domains
Smaller teams
Independence
Why Use Microservices?
Pros Cons
Less cost for large domains Only for large domains
Smaller teams
Independence
Why Use Microservices?
Pros Cons
Less cost for large domains Only for large domains
Smaller teams Higher up-front cost
Independence
Why Use Microservices?
Pros Cons
Less cost for large domains Only for large domains
Smaller teams Higher up-front cost
Independence Distributed system costs
Code Demo
Where to Go Next?
Where to Go Next?
Martin Fowler
Where to Go Next?
Eric Evans
Where to Go Next?
Articles
Courses
Presentations
Source Code
Videos
www.matthewrenze.com
www.pluralsight.com/authors/matthew-renze
Conclusion
Summary
Presentation
Domain
Database
Summary
Users
Focus on the inhabitants
Presentation
Domain-centric Architecture
Cross-Cutting Concerns
Application Layer Application
Domain
Persistence Infrastructure
Database OS
Summary
Users
Focus on the inhabitants
Presentation
Domain-centric Architecture
Application Layer Queries Commands
Database
Summary
Customers
Focus on the inhabitants
Domain-centric Architecture
Application Layer
Commands and Queries Products Vendors
Functional Cohesion
Summary
Support
Focus on the inhabitants
Domain-centric Architecture
Sales
Application Layer Inventory
Twitter: @matthewrenze
Email: [email protected]
Website: www.matthewrenze.com
Thank You! : )