Share
Embed
Transcript
47 slides
-
Clean Architecture Essentials Ivan Paulovich Stockholm Software Craftsmanship November 13,
2019 https://paulovich.net @ivanpaulovich -
Agenda • The “Software Architecture” • Introduction to Clean Architecture
• Plugin Argument • Use Cases Implementation Guided By Tests • Ports and Adapters Explained • The User Interface is a Detail • Frameworks are Details • Sample Application • Wrapping up • References -
Ivan Paulovich Agile Software Developer, Tech Lead, 20+ GitHub projects
about Clean Architecture, SOLID, DDD and TDD. Speaker/Streamer. @ivanpaulovich -
The “Software Architecture” Presentation Layer ASP.NET MVC and Autofac Business
Layer MediatR and AutoMapper Data Layer Entity Framework and Dapper Database and Messaging SQL Server and RabbitMQ IDE and Tools Visual Studio, Resharper and .NET Framework 4 @ivanpaulovich -
The “Software Architecture” Presentation Layer ASP.NET MVC and Autofac Business
Layer MediatR and AutoMapper Data Layer Entity Framework and Dapper Database and Messaging SQL Server and RabbitMQ IDE and Tools Visual Studio, Resharper and .NET Framework 5 @ivanpaulovich Not a Software Architecture! -
Clean Architecture • Use Cases as central organising structure. •
Follows the Ports and Adapters pattern (Hexagonal Architecture). • Implementation is guided by tests. • Decoupled from technology details. • Lots of Principles (SAP, SDP, SOLID..) • Pluggable User Interface @ivanpaulovich -
Ticket Terminal Ticket Terminal Display movies Request movie details Print
pre-ordered tickets Order tickets Customer Movie Catalog Order System Credit Processing Use Cases • Use Cases are delivery independent. • Show the intent of a system. • Use Cases are algorithms that interpret the input to generate the output data. • Primary and secondary actors. 8 @ivanpaulovich -
Business v Primary Actors @ivanpaulovich 9 Ports and Adapters Cloud
Messaging UI In Memory Persistence Tests In Memory Messaging SQL Secondary Actors -
@ivanpaulovich Abstract ⭐ ⭐ General ⭐ ⭐ Stable ⭐
⭐ Concrete ⭐ ⭐ Inconsistent ⭐ ⭐ Specific ⭐ ⭐ Abstractions and Stability Principles Concrete ⭐ ⭐ Consistent ⭐ Specific ⭐ -
Entities Entities Clean Architecture Entities Use Cases Controllers Gatew ays
Presenters D evices W eb UI DB External Interfaces @ivanpaulovich •Abstractness increases with stability. •Depend in the direction of stability. •Classes that change together are packaged together. -
Entities Use Cases Controllers Presenters Gateways Devices UI DB Web
External Interfaces Abstract, General, Stable, Consistent Concrete, Specific, Unstable, Inconsistent Level Clean Architecture @ivanpaulovich -
Plugin Architecture TicketController IOrderTicketUseCase OrderTicket ITicketRepository SQLTicket Storage Core Layer
Infrastructure Layer User Interface Layer Abstract, General, Stable Concrete, Specific, Unstable Concrete, Specific, Unstable Level Level 15 <<interface>> <<interface>> @ivanpaulovich -
Software Architecture “A good architecture allows major decisions to be
deferred. A good architect maximizes the number of decisions not made. Uncle Bob. 16 @ivanpaulovich -
Plugin Architecture OrderTicket ITicketRepository SQL Server / NoSQL <<interface>> Core
Layer Infrastructure Layer 17 @ivanpaulovich • The dependencies point in the direction of the abstract components. -
Core Layer Infrastructure Layer Plugin Architecture OrderTicket ITicketRepository SQL Server
/ NoSQL <<interface>> 2nd 18 @ivanpaulovich • The dependencies point in the direction of the abstract components. -
Core Layer Infrastructure Layer Plugin Architecture OrderTicket ITicketRepository SQL Server
/ NoSQL <<interface>> 2nd Fake Storage 1st Development Order 19 @ivanpaulovich • The dependencies point in the direction of the abstract components. • The simplest component version are implemented first. -
Core Layer Infrastructure Layer Plugin Architecture OrderTicket ITicketRepository SQL Server
/ NoSQL Fake Storage Cloud Storage Development Order • The dependencies point in the direction of the abstract components. • The simplest component version are implemented first. • Defer decisions. Keep the options open as long as possible! <<interface>> 1st 2nd 3rd 20 @ivanpaulovich -
Use Cases Implementation Guided by Tests • Test Cases are
the first consumers implemented. • Design the Use Cases with Single Responsibility Principle in mind. • Fake implementations are the first delivery mechanisms implemented. • Test Cases help you design fine grained Use Cases (Interface Segregation Principle). Test Cases Use Cases Fake Persistence Core Layer Infrastructure Layer Test Suite 21 @ivanpaulovich -
Use Cases Implementation Guided by Tests • Test Cases are
the first consumers implemented. • Design the Use Cases with Single Responsibility Principle in mind. • Fake implementations are the first delivery mechanisms implemented. • Test Cases help you design fine grained Use Cases (Interface Segregation Principle). 22 @ivanpaulovich -
Whats is a Fake (Test Double)? • Runs in memory
(No I/O). • Same capability as the real one. @ivanpaulovich -
Components Overview 1. Components are designed to fulfill the Core
needs and are implemented and tested in isolation. 2. Components could be replaced, upgraded or decommissioned with minimum Core business impact. 3. Good components are loosely coupled. Test Cases Infrastructure Layer Test Suite SQL Storage 24 @ivanpaulovich -
Run the host using Fakes Run the host
using the Real Implementations @ivanpaulovich ASPNETCORE_ENVIRONMENT=Development ASPNETCORE_ENVIRONMENT=Production -
Application Infrastructure Unit Test Web Database Domain Ports and Adapters
@ivanpaulovich 29 You can run it in Production if you want. Get feedback! -
Application Unit Test Web Database Domain Primary Actors Secondary Actors
@ivanpaulovich 33 Infrastructure Ports and Adapters -
Frameworks are Details • Application code called by frameworks should
be under control. • Hide 3rd party libraries, use them behind core interfaces. • The .NET Framework is a detail. • Keep outside the Business Layer: Reflection, Linq, Entity Framework, MVC, Data Annotations, WCF. 34 @ivanpaulovich -
The User Interface is a detail AccountsController IUseCase GetAccountDetails UseCase
Core Layer User Interface Layer <<interface>> GetAccountDetails Input GetAccountDetails Presenter GetAccountDetails Output IOutputPort Response <<interface>> @ivanpaulovich 39 -
Sample Application Ticket Terminal Todo List App Add Todo Item
List Todo Items Do Undo User Storage 40 @ivanpaulovich -
Sample Application Ticket Terminal Todo List App Add Todo Item
List Todo Items Do Undo User Storage 41 @ivanpaulovich -
42 ✅ Commad-Line Task management with storage on your GitHub
@ivanpaulovich $ dotnet tool install -g todo https://github.com/ivanpaulovich/todo -
Evolutionary Architecture In Action 43 Use Cases Todo List Do
Undo Remove Rename Unit Tests Web API Console App In Memory File System SQL Server GitHub Persistence User Interface @ivanpaulovich -
@ivanpaulovich Splitting packages on the software lifetime Domain Application Infrastructure
User Interface Tests Tests Core Tests Core Infrastructure 1 2 3 4 -
Wrapping up Clean Architecture • Clean Architecture is about usage
and the use cases are the central organising principle. • 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. 45 @ivanpaulovich -
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