> For the complete documentation index, see [llms.txt](https://yoan-thirion.gitbook.io/knowledge-base/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yoan-thirion.gitbook.io/knowledge-base/software-craftsmanship/code-katas/clean-architecture.md).

# Clean Architecture

## Connection

* Split the group in 4 :
* Ask this question
  * What characteristics do these architectures have in common? - 5'
  * Craft as many post-its as they can
* Review what has been produced

#### Hexagonal architecture

![](/files/-MBmGI3OreGzXugbMmtM)

![Alistair Cockburn](/files/-MBmKntsiSt4Im6vvP2e)

#### Onion architecture

![](/files/-MBmLaZJ9xuAduCfeImo)

![Jeffrey Palermo](/files/-MBmLe2ZMeA1aXrmHfgW)

## Concepts

### Hexagonal Architecture

![](/files/-MBmMBWiBDooLipo0hns)

{% hint style="info" %}
*Allow an application to equally be driven by users, programs, automated test or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases.*
{% endhint %}

![](/files/-MBmMHM-PBPorzUve971)

Source : <https://blog.octo.com/architecture-hexagonale-trois-principes-et-un-exemple-dimplementation/>

### Onion Architecture

#### Layers

![](/files/-MBmMrvuxP8NznMVQgcQ)

* ***Domain Model layer***, where our entities and classes closely related to them e.g. value objects reside
* ***Domain Services layer***, where domain-defined processes reside
* ***Application Services layer***, where application-specific logic i.e. our use cases reside
* ***Outer layer***, which keeps peripheral concerns like UI, databases or tests

#### Dependency Inversion Principle (at Architecture Level)

![](/files/-MBmN_WMNqNN1LSiatby)

This means that when writing our high-level business logic, we don’t want to depend directly on low-level stuff like databases, file systems, network connections, provider contracts, and such.

![](/files/-MBmOBDmOG1XnOvjih9l)

{% hint style="success" %}
*Instead, we should expose an abstraction that represents a higher-level business need and implements it using these low-level mechanisms.*
{% endhint %}

### What characteristics do these architectures have in common?

Here is some answers to the connection question :

* Independent of frameworks
  * Does not depend on the existence of libraries
  * Allow us to use frameworks as tools (not a constraint)
* Independent of the front-end
  * Can easily change the UI (from web to console)
* Independent of the database
  * Business rules not bound to Database logic
* Independent of any external agency
  * Business rules don’t know anything about outside world

{% hint style="success" %}
***Business rules can be tested without external components***
{% endhint %}

### Use Case Driven Approach

![](/files/-MBmR-pxOQY_fYY8MMgR)

Ivar Jacobson in his book below explained we should map 1 Business Use case / 1 Application Use case. We should implement Application Use Case with the BCE "pattern" : Boundary Controller Entity :

![BCE](/files/-MBmRptAqqjnvRm2OqjM)

![](/files/-MBmR7zaLLaxnXQFd6Qe)

### Clean Architecture

![Uncle Bob's book](/files/-MBmXp8yL4EdnrGHuqUl)

The Clean Architecture as expressed by Robert C. Martin is a mix of the 3 architecture ideas explained before :

![](/files/-MBmS9tX9OP1kMC9I4z6)

#### Entities (Enterprise business rules)

* Encapsulate Enterprise wide business rules
  * Can be
    * Object with methods
    * Set of data structures and functions&#x20;
* Could be used by many different applications in the enterprise.

![](/files/-MBmSUTaYtkmeknpwB6O)

#### Use cases (Application business rules)

* Capture business rules&#x20;
* Structure should indicate what the application is, not how it does it&#x20;
* Application specific business rules

![](/files/-MBmSlD6KWJUvhsJ8Qn5)

#### Interface adapters

* Set of adapters
  * Convert data from the format most convenient for the use cases and entities
  * To the format most convenient for some external agency such as the Database or the Web&#x20;
* In a MVC architecture : Presenters, Views, and Controllers

![](/files/-MBmSymFA7SXW5X2vzL8)

#### Frameworks & drivers

* Frameworks and tools such as
  * Database
  * Web Framework

{% hint style="info" %}
*Glue code that communicates to the next circle inwards.*
{% endhint %}

* This layer is where all the details go
  * Keep these things on the outside where they can do little harm

#### The dependency rule

![](/files/-MBmTc95yWDUAdCVt6hL)

{% hint style="success" %}
Source code dependencies can only point inwards.
{% endhint %}

![](/files/-MBmThSYwcS6aIJOExjz)

## Concrete Practice

* Clone the repository at : <https://github.com/ivanpaulovich/clean-architecture-kata-dojo>
* Checkout the branch “kata-initial”

### Objective

Learn and practice the concepts behind Clean Architecture

### Part 1

Each participant :

* Position projects on the Clean Architecture Diagram
* Draw the sequence diagram of the Register Use Case
  * They can use :
    * [https://sequencediagram.org/      ](<https://sequencediagram.org/&#xD;&#xA;>)
    * <https://plantuml.com/>
    * <https://app.diagrams.net/>

![Empty Clean architecture Diagram](/files/-MBmUYIwDoSnZVsMIXwW)

#### Correction

![](/files/-MBmV-4aKfM5LP_oHgSq)

![Register User Case](/files/-MBmVqDnpmAe54_ZXA8q)

### Part 2

Cover the following use cases and requirements:

* The customer can register a new account.&#x20;
* Allow to get the customer details.&#x20;
* Allow to get the account details.&#x20;
* Allow to deposit into an existing account.&#x20;
* Allow to withdraw from an existing account.&#x20;
* Accounts can be closed only if they have zero balance.&#x20;
* Accounts does not allow to withdraw more than the current account balance.

![](/files/-MBmWMDzvbIEPg_IYu76)

You can check in the branch **final-solution** to see one possible implementation of those Use Cases

## Conclusion

* What benefits do you see?&#x20;
* Pros and cons ?

![](/files/-MBmX5VAZoSOusvvp33k)

### Pros & Cons

| Pros                                                                                                                                       | Cons                                                                                                                         |
| ------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------- |
| ***Plays well with DDD*** – architecture that builds everything on top of a domain model                                                   | ***Learning curve*** – People tend to mess up splitting responsibilities between layers, especially harming the domain model |
| ***Directed coupling*** – the most important code in our application depends on nothing, everything depends on it                          | ***Indirection*** – interfaces everywhere!                                                                                   |
| ***Flexibility*** – from an inner-layer perspective, you can swap out anything in any of the outer layers and things should work just fine | ***Potentially heavy***                                                                                                      |
| ***Testability*** – since your application core does not depend on anything else, it can be easily and quickly tested in isolation         |                                                                                                                              |

## Support

{% embed url="<https://speakerdeck.com/thirion/clean-architecture-hands-on?slide=1>" %}

## Resources

* Hexagonal Architecture : <https://blog.octo.com/architecture-hexagonale-trois-principes-et-un-exemple-dimplementation/>
* Onion Architecture : [https://bitbucket.org/jeffreypalermo/onion-architecture/src](<https://bitbucket.org/jeffreypalermo/onion-architecture/src&#xD;&#xA;>)
* Clean Architecture : <https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html>
* A simple template for Onion Architecture with .NET 5 : <https://pereiren.medium.com/a-simple-template-for-onion-architecture-with-net-5-6c0e2f3b29c8>

![](/files/-MToH9SZ3vfvnqCWEvV6)
