Professional Documents
Culture Documents
Clean Architecture - 2020
Clean Architecture - 2020
Implementing in .NET
Ian Cooper
@ICooper
Who are you?
• Software Developer for more than 20 years
• Worked mainly for ISVs
• Reuters, SunGard, Misys, Huddle
• Worked for a couple of MIS departments
• DTI, Beazley
• Microsoft MVP for C#
• Interested in architecture and design
• Interested in Agile methodologies and practices
• No smart guys
• Just the guys in this room
2
3
Agenda
• On Modules
• On Layering
• On Domains
• On Clean Architecture
• On Implementing
4
Robert Martin
Ivar Jacobson
Alistair Cockburn Jeffrey Palermo
5
On Modules
5 6
Module
7
Modules
8
Cohesion and
Coupling
9
High and Low
Implementation Details
10
Interchangeability
If two modules present the same abstraction, in other words have the same contract with a client then we can swap one for another.
Because a module
We can isolate a Coarse Grained Facade implements an
module for testing by abstraction, we can
swapping its Implementation Details substitute it with a
dependencies for a dummy to defer a
stub design decision, whilst
working on other
modules
12
Modules in .NET
13
On Layering
5 14
1: If I change the façade of A,
the implementation details of C Cyclic
which depend on it need to
change. Dependency
I need to build A, then build C.
15
Layers
A D UI
Domain
Relaxed Model
Only
B
C Infrastructure
16
Dependency
Inversion A D
Implements
Interface
UI
Creates D
Passes to B
Domain
Exposes
B Interface D Requirements
as Interface
C Infrastructure
17
On Domains
20 18
We can distinguish two types of ‘business logic’
19
UI
A
B Domain
Service
Entity
D
Infrastructure
20
There are two basic implementation variations
21
In the domain facade approach a Service Layer is implemented as a
set of thin facades over a Domain Model, exhibiting the defining
characteristics of Service Layer.
22
In the operation script approach a Service Layer is implemented as a
set of thicker classes that directly implement application logic domain
object classes are mainly dumb objects holding state.
Each such class forms an application “service,” and it’s common for
service type names to end with “Service.”
23
Ports and Adapters
A ports & adapters architectural style (Hexagonal Architecture) is a variation of the
layered architectural style which makes clear the separation between the:
ports, which are ‘purposeful conversations’ between the actor, via the
adapter, and the domain model
24
Application
Adapter
Port
25
Infrastructure
E Adapter UI
A
B <<interface>> D
Port Domain
Dependency
Inversion Application
C
26
Ivar Jacobson
27
28
Boundary-Controller-Entity
29
Boundary
30
Entity
Entities are objects representing system data: Customer, Transaction, Cart, etc.
31
Controller (Interactor)
Controllers are objects that mediate between boundaries and entities. They
orchestrate the execution of commands coming from the boundary.
32
33
Infrastructure
E Adapter UI
A
B <<interface>> D
Port Domain
Application
C
34
Controllers (Interactors) are objects that implement use cases. There
is an interactor for every use case. Name them after the use case. It
executes the use case by talking to entities and boundaries. All the
interactors together contain your application specific business logic
or rules.
35
36
There is a correlation here between the use case boundary and the
test boundary - tests should focus on the behaviour expressed by a use
case, not on a unit of code.
37
38
39
40
On Clean Architecture
30 41
Depend
Inwards
Dependency
Inversion
Request Model
Entity
Interactor Entity
Delivery Entity
Boundary <<interface>> Entity
Mechanism
Use Cases
Response
Data Structure Model Entity Gateway
<<interface>>
Frameworks &
Drivers
Interface Adapters
44
Request Model
Entity
Interactor Entity
Entity
API Framework Web Controller <<interface>> Entity
Use Cases
Presenter Response
Response Entity Gateway
Data Structure <<interface>> Model
Model <<interface>>
Frameworks &
Drivers
45
Request Model
Entity
Interactor Entity
API Entity
Web Controller <<interface>> Entity
Framework
Use Cases
Response
Data Structure Model Entity Gateway
<<interface>>
Frameworks &
Drivers
Delivery
Interface Adapters
Mechanism
DB
46
On Implementing
Port, Boundary, Interactor, call it what you will but how do we do it?
40 47
Command
Design Pattern
48
Request Model Command
Entity
ICommand Entity
Delivery Entity
Boundary <<interface>> Entity
Mechanism
Use Cases
Response
Data Structure Model Entity Gateway
<<interface>>
Frameworks &
Drivers
Interface Adapters
49
Command Dispatcher
Invoker - has a lit of Commands that are to be
executed
Command - represents the request to be processed,
encapsulating the parameters to be passed to the
command-handler to perform the request
Command Handler - specifies the interface that any
command handler must implement
Concrete Command Handler – implements the
request
Command Dispatcher – Allows dynamic registration
of Command Handlers and looks up handlers
for commands, by matching command and handler
key.
Client – registers Commands with the Command
Dispatcher.
50
Request Model AsyncRequest
Handler
Entity
IHandleRequests Entity
XUnit Entity
Test <<interface>> Entity
Framework
Use Cases
Response
Data Structure Model Entity Gateway
<<interface>>
Frameworks &
Drivers
Delivery
Interface Adapters
Mechanism
In-Memory
DB
51
Demo
52
53