TubeSum ← Transcribe a video

Clean Architectures in Python - presented by Leonardo Giordani

Transcribed Jun 19, 2026 Watch on YouTube ↗
Intermediate 14 min read For: Python developers with basic understanding of web frameworks and interest in software design patterns and architecture.
36.1K
Views
961
Likes
0
Comments
29
Dislikes
2.7%
📈 Moderate

AI Summary

Leonardo Giordani presents the clean architecture concept for Python systems, drawing parallels to Roman architect Vitruvius's principles of durability, utility, and beauty. He explains the layered architecture, dependency inversion, and the golden rule of communicating inward via simple structures and outward via interfaces, using a concrete example of a web application that retrieves a list of items.

[0:05]
Architecture Definition

Architecture is defined as the art and science of organizing and integrating components of a computer system, emphasizing the three qualities from Vitruvius: durability, utility, and beauty.

[2:51]
Why Architecture Matters

Draws analogies to the Sydney Harbour Bridge (beauty) and Unix system longevity (durability) to argue software should be useful, durable, and beautiful.

[5:09]
Core Concept: Clean Architecture Layers

The clean architecture (by Robert Martin) consists of four layers: entities, use cases, gateways, and external systems. The rule is that inner layers know about nothing outer; dependencies point inward.

[10:22]
Golden Rule of Communication

Talk inward with simple structures (e.g., Python dicts) and outward through interfaces (dependency injection). Use cases should not instantiate database objects directly.

[16:08]
Example: Use Case Implementation

A use case function receives a repository interface via dependency injection, calls repo.list() to get entities, and returns the result. The web framework handles HTTP ↔ use case translation.

[22:02]
Advantage 1: Testability

Clean architecture allows testing business logic in isolation by mocking the repository interface. Details (web framework, database) can be tested independently.

[25:01]
Advantage 2: Customization and Flexibility

Different data sources (SQL, MongoDB) can be used for different use cases without affecting business logic. The same use case can be served via web CLI or HTTP.

[28:01]
Comparison with Django Architecture

Django's ORM is tightly coupled with models and the database, making it harder to test business logic without a database. Django is more Playmobil (ready out of the box) while clean architecture is Lego (more flexible).

[33:35]
Migration Advice

Do not rewrite from scratch (Netscape's mistake). Instead, isolate a small part of the system, re‑implement it with clean architecture, and route requests gradually using load balancers.

[34:33]
Final Verdict: 'It Depends'

The clean architecture is not the only correct answer. The choice depends on requirements: Lego (clean architecture) vs Playmobil (Django). Mixing both is possible.

[38:36]
Resource: Free Book and Bundle

Leonardo wrote a free book 'Clean Architectures in Python' which contains the example shown, implemented with TDD. During EuroPython 2022 the book and others are free for a limited time.

The clean architecture is a powerful pattern for building maintainable, testable Python applications, but it is not a one‑size‑fits‑all solution. Developers should evaluate their project's requirements before committing to any architectural style.

Clickbait Check

95% Legit

"The title accurately reflects the talk's content — it is a detailed exposition of clean architectures in Python, covering both theory and a concrete example."

Tutorial Checklist

1 10:22 Define layers: entities (models), use cases (business logic), gateways (interfaces), external systems (web framework, database).
2 12:55 Implement entities as simple Python classes that capture business data (e.g., Item with code and price).
3 14:55 Create the use case as a function in the use cases layer that does not import external frameworks. Accept a repository interface as a parameter.
4 17:20 Define a gateway/interface (e.g., an abstract base class or protocol) with methods like list(). Implement concrete repositories (PostgresRepo, MongoRepo) that implement that interface.
5 19:45 In the web framework (e.g., Flask route), instantiate the repository and inject it into the use case. Do not let the use case instantiate the repository directly.
6 22:02 Test the use case in isolation by passing a mock repository that returns fixed data. Test the web framework and repository integration separately.
7 28:01 To switch data sources, create a new repository implementation and inject it into the appropriate use case. The business logic remains unchanged.
8 33:35 When migrating an existing system, isolate a small component first, re‑implement it using clean architecture, and route a subset of requests to the new microservice using a load balancer.

Study Flashcards (10)

What are the three qualities of architecture according to Vitruvius?

easy Click to reveal answer

Durability, utility, and beauty (firmitas, utilitas, venustas).

01:29

What is the single rule governing layers in the clean architecture?

medium Click to reveal answer

Outer layers can depend on inner layers, but inner layers cannot depend on outer layers.

12:48

What is the golden rule for communication between layers?

hard Click to reveal answer

Talk inward with simple structures (simple data types) and outward through interfaces (dependency injection).

13:46

List the four traditional layers of the clean architecture from innermost to outermost.

medium Click to reveal answer

Entities, Use Cases, Gateways, External Systems.

10:19

How does the clean architecture improve testability?

medium Click to reveal answer

Business logic (use cases) can be tested in isolation by passing a mock repository interface, without needing a live database or web framework.

22:02

What is the main drawback of Django's architecture compared to the clean architecture?

medium Click to reveal answer

Django's ORM tightly couples models with the database, making it harder to test business logic without a database and harder to switch to non‑relational databases.

30:56

What is the recommended strategy for migrating an existing system to clean architecture?

hard Click to reveal answer

Isolate a small component, re‑implement it with clean architecture, and route requests gradually using a load balancer. Do not rewrite the entire system at once.

33:35

What is the role of the web framework in the clean architecture example?

medium Click to reveal answer

It translates HTTP requests into calls to use cases and translates the use case results (entities) back into HTTP responses (e.g., JSON).

14:55

What is dependency injection and how is it used in the clean architecture?

hard Click to reveal answer

Dependency injection is a technique where the outer layer (e.g., web framework) creates an instance of an interface (e.g., repository) and passes (injects) it into the use case, rather than the use case instantiating it directly.

19:45

Why is the database considered a 'detail' in system design?

medium Click to reveal answer

The database is a detail because it is not part of the core business logic — the user is concerned with the service provided, not the specific database technology used.

21:20

💡 Key Takeaways

💡

Vitruvius's Principles

Introduced a timeless framework for evaluating software quality beyond mere functionality.

01:29
🔧

Golden Rule of Communication

Central design principle that decouples layers and enforces dependency direction.

13:46
📊

Testability Advantage

Explained how isolating business logic enables pure unit testing without infrastructure dependencies.

22:02
💡

Comparison with Django

Provided a concrete, relatable contrast between two architectural styles, clarifying trade‑offs.

28:01
⚖️

Migration Caution (Netscape Story)

Strong warning against rewriting systems from scratch, grounded in a historical failure.

33:35
⚖️

'It Depends' Philosophy

Humble acknowledgment that no architecture is universally best; requirements drive the choice.

34:33

✂️ Creator Tools: Viral Hooks

AI-generated clip ideas for Shorts based on the transcript

Architecture = Durability, Utility, Beauty

60s

This philosophical take on software architecture using ancient Roman principles is surprising and thought-provoking, making viewers want to rethink their code.

▶ Play Clip

The Golden Rule of Clean Architecture

52s

The simple rule 'talk inward with simple structures, outward through interfaces' is a concise, powerful insight that developers love to share.

▶ Play Clip

Dependency Injection in 60 Seconds

60s

A clear, practical example of dependency injection using a real code snippet makes this abstract concept immediately understandable and applicable.

▶ Play Clip

Clean Architecture vs Django: The Showdown

60s

Comparing two popular architectures with a controversial 'unclean' label for Django sparks debate and draws in Django developers.

▶ Play Clip

Don't Rewrite Everything! Here's Why

44s

A cautionary tale using the Netscape disaster makes this advice memorable and highly shareable among developers considering major refactors.

▶ Play Clip

[00:05] thank you for being here so the title of

[00:09] my talk today is clean architectures in

[00:11] Python A Tale of durability utility and

[00:15] beauty

[00:16] and my goal today is so I'm here because

[00:19] I want to share my views on system

[00:22] design

[00:24] and give you an example of what I

[00:26] discovered and what I learned and maybe

[00:28] meet people who are interested in system

[00:31] design as well

[00:33] so this is me well not a picture of me

[00:38] but you know I'm a developer and a

[00:41] blogger you are welcome to visit my blog

[00:43] I usually write about python obviously

[00:46] tdd object oriented cryptography and

[00:50] infrastructure these are the main things

[00:52] I like to write about

[00:56] so today it's a talk about the clean

[00:59] architecture and I want to start

[01:02] with a question thinking a bit about

[01:05] what architecture is

[01:07] we mentioned architecture many times the

[01:10] architecture of a system or what is it

[01:13] what is it definition

[01:15] so I found this in a book that has been

[01:19] around for a while the architecture it

[01:22] means about architecture

[01:24] it's been written a couple of thousands

[01:27] years ago

[01:29] the two views who was a Roman architect

[01:33] and engineer

[01:35] says that architecture is about firmitas

[01:39] utilitas and venustas which translated

[01:42] in Modern English is durability

[01:46] utility and beauty

[01:48] so Vitruvius says that architecture is

[01:52] about things that are durable something

[01:56] that has to last

[01:58] has to be useful has to be beautiful

[02:02] and for me this was very interesting

[02:04] because how many times

[02:07] do you think about your code as

[02:10] something that has to be useful

[02:12] beautiful and durable

[02:15] useful yes nobody wants to write

[02:18] something that is useless

[02:20] but what about durable

[02:23] we change framework every two years now

[02:26] right every six months I don't know so

[02:28] having something that lasts

[02:30] and beautiful that's the thing I'm

[02:33] mostly concerned about

[02:36] it's interesting let me mention this

[02:38] that vituvius was an engineer and an

[02:41] architect

[02:43] and

[02:44] these two professions don't go together

[02:47] that much nowadays um it's interesting

[02:50] to think that engineer the word comes

[02:53] from engine

[02:54] but engine comes we we when we discuss

[02:58] about engines we think about something

[03:00] mechanical

[03:01] but engine actually comes from

[03:04] um Ingenuity in Latin it's from

[03:08] cleverness

[03:09] so it's less about mechanical things is

[03:12] more about solving a problem in a clever

[03:15] way

[03:16] anyway I went on and I checked also on

[03:20] the dictionary Modern English dictionary

[03:22] and I found these two interesting

[03:24] definitions of architecture

[03:27] one is the Art and Science of Designing

[03:29] and making buildings

[03:31] and the second one is more

[03:33] concerned with computers and it says the

[03:36] internal organization of computers

[03:38] components with particularly the

[03:40] reference to the way in which data is

[03:42] transmitted I like these two definitions

[03:44] I wanted to come up with something a bit

[03:46] more compact

[03:47] so I try to merge them into this which

[03:51] is the Art and Science

[03:53] in which the components of a computer

[03:55] system are organized and integrated

[04:00] I want to stress

[04:01] Art and Science

[04:04] how many times do you think about what

[04:06] you do daily as Art and Science

[04:10] as science yes data science computer

[04:13] science

[04:14] mechanical path right

[04:16] what about art

[04:19] when do you look at your code and think

[04:21] this is art

[04:22] why is it not data art it's not computer

[04:25] art not as in you know painting

[04:28] something with the computer but our code

[04:31] is beautiful or it can be

[04:33] and the other two interesting words here

[04:36] are organized and integrated because a

[04:39] system architecture when we when it

[04:41] comes to computer science is all about

[04:43] where components of the system are and

[04:47] how data flows between them

[04:51] so this is the integration part

[04:54] cool now that I defined architecture

[04:58] the following question is do we need it

[05:02] so do we need things our code to be

[05:06] useful durable and beautiful

[05:10] and this is up to you I positive about

[05:13] this otherwise it wouldn't be here

[05:15] but I want to give you a couple of

[05:17] examples and one comes from the

[05:21] traditional architecture and it's the

[05:24] page that is in front of this building

[05:26] the Samoa backup page

[05:29] it's interesting I was looking at it

[05:31] these days and I thought

[05:33] does this bridge have to be shaped like

[05:36] an heart

[05:39] if the requirement of the page is just

[05:42] to take cars from one side to the other

[05:45] it doesn't have you just need a plain

[05:48] Bridge right something that I could

[05:50] design because I don't know anything

[05:51] about which design

[05:54] but if the requirement is to make your

[05:57] journey better

[05:59] well being shaped like a hub in Dublin

[06:03] is a nice thing to have

[06:05] so this is Food For Thought maybe you

[06:09] know like what is the requirement of

[06:11] your code what what is it that you are

[06:13] creating when you create a library do

[06:15] you just create some Machinery or do you

[06:16] create something to make the journey

[06:18] better

[06:19] the other example I have it's about

[06:21] something lasting a long time

[06:24] um

[06:25] the Unix system it's an operating system

[06:28] was designed in 71 so 50 years ago

[06:31] and it was well thought

[06:35] not everything in a unique system is

[06:38] perfect okay sometimes it's far from it

[06:41] but it was well thought

[06:44] so well designed that I am running a

[06:47] Linux machine and Linux is a clone of

[06:49] Unix and many of you use Mac OS which is

[06:53] a derivative of Unix 50 years

[06:57] so again how many times do you look at

[07:00] your code the code that you write and

[07:02] you think

[07:03] in 50 years people are still we will

[07:05] still use this code or at least these

[07:08] ideas

[07:10] anyway I'm not the only one who thinks

[07:13] that system design is an interesting

[07:15] thing

[07:16] um there are much smarter people than me

[07:18] who wrote a lot of books there are vast

[07:21] literature about this I selected five

[07:24] books that I read which I believe are

[07:27] interesting about the topic

[07:30] some of these are door stoppers pretty

[07:34] thick okay so if you are not up for the

[07:36] challenge I recommend retrieving at

[07:39] least the two in Orange so design

[07:41] patterns and Enterprise Integration

[07:43] patterns at least

[07:46] read the introduction

[07:49] and I'm not joking the introduction to

[07:52] these two books the two introductions

[07:54] are short

[07:56] but they give you a narrative of the

[07:59] challenges and some of the solutions

[08:01] that you might have when you design a

[08:04] system

[08:05] and I was

[08:07] um I was really

[08:08] you know flabbergasted when I read the

[08:12] introduction to design patterns because

[08:14] it was like Hey I faced these issues

[08:17] every day

[08:20] I want to mention another thing about

[08:21] Enterprise Integration patterns this is

[08:24] a book about messaging message based

[08:27] systems more about distributed systems

[08:30] if you want

[08:32] but it's interesting that message based

[08:35] systems microservices for example

[08:38] this software design and languages they

[08:43] are all in the same league they share a

[08:46] common trait

[08:48] which is messages

[08:52] object oriented programming and you can

[08:54] quote me on this is supposed to be

[08:57] about objects that exchange messages

[09:02] so every time you call a method on an

[09:04] object you are sending a message to

[09:06] something

[09:07] so it sees a distributed system

[09:10] and if you if you think about that if

[09:12] you when you code in Python if you think

[09:14] about messages this might not change the

[09:16] code itself because you are still

[09:18] calling methods but it will definitely

[09:21] change the way you think about your code

[09:24] it's a bunch of objects it's a

[09:27] distributed system and I'm exchanging

[09:29] messages anyway this might be for

[09:32] another talk another time now that I

[09:34] defined architecture and I decided for

[09:37] you that we need it

[09:40] let me Define clean because this is

[09:43] about a clean architecture

[09:45] um I found it

[09:47] easy to define the opposite of clean the

[09:51] picture in the background if you look at

[09:53] that system

[09:54] you can definitely say this is not clean

[09:57] it's not tidy good luck maintaining

[10:00] something like that okay they said pull

[10:03] the green cable yeah good luck okay this

[10:07] is an extreme example probably but

[10:11] sometimes our code looks like that right

[10:14] you change something and suddenly

[10:16] everything crashes nothing works anymore

[10:19] instead in a clean system with a tidy

[10:21] system if you want you have these

[10:26] characteristics for for each component

[10:29] you know the three W's right you know

[10:33] where it is it's easy to find the

[10:35] component in the system it's isolated

[10:38] you know what it is from the name for

[10:41] example and you know why it is in the

[10:43] system you can say why it's been

[10:46] included in the pictures in the

[10:48] background there are two just you know

[10:49] hardware systems um and they are tidy

[10:52] it's easy to trace where our cable goes

[10:55] some are color coded so it's easy to

[10:59] understand you know why they are there

[11:00] what they are what they are doing

[11:05] okay now that I Define the claim

[11:07] architecture in terms of the words let's

[11:10] go for the concept for an example

[11:14] so what is the clean architecture

[11:18] it is a concept that was introduced by

[11:20] Robert Martin uh some years ago Herbert

[11:22] Martin is a system engineer system and

[11:25] designer you know a developer and I'm

[11:27] not here to advertise Robert Martin's

[11:30] work mostly because Robert Martin is

[11:33] very good at advertising himself you

[11:36] know so

[11:38] um but I'm gonna use the same name uh

[11:41] it's it's important for me to stress

[11:43] that the concepts that Robert Martin

[11:46] dubbed the clean architecture uh predate

[11:50] his work so they have been around for a

[11:53] long while

[11:55] so let's call it the clean architecture

[11:57] but it's a set of Concepts such and that

[12:00] predates what Robert Martin did what is

[12:03] it it's a layered approach so it's a way

[12:07] to structure your software

[12:10] um project right you your code it's

[12:13] layered and it's circular

[12:16] so in the traditional

[12:18] [Music]

[12:19] say definition we have four layers you

[12:23] can have more of them but this these are

[12:25] the traditional ones entities use cases

[12:28] gateways and external systems

[12:31] what happens is that when you create

[12:33] something in a clean architecture this

[12:36] something this component will belong to

[12:39] one of these layers and there are rules

[12:42] there is actually one simple rule one

[12:46] rule at least

[12:48] which is that your component can see

[12:54] I'm going to Define what C is can see

[12:57] only what has been defined in an inner

[12:59] layer

[13:00] so if you create something in the use

[13:02] cases layer or ring

[13:06] you can see everything that has been

[13:08] defined in the same layer use cases and

[13:10] everything that has been defined in

[13:12] entities you are not allowed to use to

[13:15] access anything that has been defined

[13:17] outside

[13:19] and that that has to do with

[13:21] dependencies

[13:23] the problem of unclean systems remember

[13:26] the cables before is dependencies

[13:30] between

[13:31] components when you have a component

[13:33] that depends on other components and

[13:36] these other components depends on other

[13:37] components and you can't trace these

[13:40] dependencies and sometimes they are

[13:41] circular dependencies

[13:43] in a clean architecture there are no

[13:45] circular dependencies

[13:46] the Golden Rule I'm going to introduce

[13:49] it now and then show you an example that

[13:51] clarifies it is that you talk inward

[13:54] with simple structures outwards through

[13:56] interfaces what does it mean simple

[13:59] structures

[14:01] are data types that have been defined

[14:06] in inside so for example again something

[14:09] in use cases can use data types and I

[14:12] mean data types in Python for example

[14:15] you can instantiate them if they have

[14:17] been defined in use cases and if they

[14:20] have been defined in entities

[14:22] if something has been defined in

[14:24] external systems you don't see it you

[14:27] can't instantiate it

[14:30] interfaces interfaces have to do

[14:34] um so they they are related to

[14:36] dependency injection which is something

[14:38] I will introduce later

[14:40] what is an interface

[14:43] um going back to what I said about

[14:44] objects and sending messages when you

[14:47] send a message to something you expect

[14:49] it to be able to receive that message

[14:51] when you call a method on an object you

[14:54] expect that object to well have that

[14:57] method right otherwise you'll get an

[14:59] exception in Python we don't have an

[15:02] explicit way to State infrastructure in

[15:06] interfaces okay to create them even

[15:09] though we have now protocols we have

[15:12] abstract based classes there are many

[15:14] ways to work with interfaces and we can

[15:17] discuss about this another time because

[15:18] there is no enough time today anyway I'm

[15:21] coming back to this slide later when

[15:24] after the example

[15:27] the example today is simple

[15:30] the code I will show is python it's

[15:33] valid python but I stripped all the you

[15:36] know error checking a lot of things that

[15:38] are not useful for now obviously they're

[15:40] real the real code is a bit more

[15:43] complicated

[15:44] my use case is to retrieve a list of

[15:48] items is very simple

[15:50] when do we want to achieve a list of

[15:52] items I don't know you have a social

[15:54] network and you want to retrieve a list

[15:56] of posts right or you are Amazon you

[15:59] want to send Using to show a list of

[16:01] proper items that you are selling

[16:05] in this example my use case is just a

[16:08] simple function it exists in the use

[16:10] cases layer

[16:12] and for the time being it doesn't do

[16:15] anything

[16:17] then I Define some entities entities are

[16:20] models okay they represent real items

[16:25] that are in my business logic

[16:27] so in this case for example something

[16:29] with a code and a price okay just a

[16:31] simple class that captures data

[16:34] encapsulates data

[16:37] the entities leave in the entities layer

[16:40] and they are known to all other

[16:42] components

[16:45] it's 2022 so we probably want to build a

[16:49] web application but this is not required

[16:52] okay it's just an example

[16:54] the web application

[16:56] requires a web framework because I don't

[16:59] want to implement you know the logic to

[17:01] deal with HTTP requests and all these

[17:04] things there are smarter people who did

[17:06] for me

[17:07] in this case I'm using flask

[17:09] but I can use any other web framework

[17:11] the web framework exists in the external

[17:14] systems and I want to say a couple of

[17:17] things about this

[17:18] um

[17:20] I mentioned business logic before the

[17:23] business logic is what you Market

[17:26] is the core of your application

[17:29] the web framework is not generally

[17:32] speaking part of your business logic you

[17:35] are not marketing Django you're not

[17:38] marketing flask you are marketing a

[17:41] social network your marketing items

[17:43] delivery whatever right

[17:45] so it's reasonable for the web framework

[17:49] to be in a very external layer where we

[17:52] use it but we don't manipulate it

[17:56] the core

[17:58] um the most important thing in a clean

[18:00] architecture I would say in any

[18:02] architecture is the business logic this

[18:04] is what 99 of your time should go

[18:08] anytime you every time you spend you

[18:11] know configuring external systems it's

[18:14] not wasted but it's not given to the

[18:16] core of your business

[18:18] anyway this is the web application the

[18:22] web framework

[18:25] now what is the task of the web

[18:27] framework

[18:29] the web framework is there because it

[18:31] wants it has to translate HTTP requests

[18:36] into course

[18:38] this is all a web framework has to do

[18:41] granted it's not an easy task okay there

[18:43] are many things involved but this is

[18:46] what the web framework should do get an

[18:49] HTTP request and transform it into a

[18:52] call for example for a python function

[18:54] in this case this is exactly what I'm

[18:56] doing I'm calling the use case which is

[19:00] a function

[19:02] here you see that the web framework

[19:05] communicates with the use case with

[19:08] simple structures what does it mean in

[19:11] this case I'm using request args for the

[19:13] sake of you know Simplicity it's just a

[19:16] dictionary

[19:17] and it's a simple structure because it

[19:19] has been defined in the in the language

[19:21] right so it it might be an entity or it

[19:25] might be something that the language

[19:26] defines all these things exist in Python

[19:30] so whatever is defined in python as a

[19:32] core language is available

[19:34] so I'm sending to um the use case as

[19:40] simple structure something that the use

[19:42] case can understand

[19:44] for example I shouldn't send anything

[19:46] that is defined in the web framework

[19:48] some structure that has been defined

[19:51] there a type that flask uses to manage

[19:54] an HTTP request because the use case

[19:56] doesn't do anything doesn't know

[19:58] anything about HTTP request and it

[20:01] shouldn't know anything

[20:05] okay then we have to retrieve items and

[20:09] data data is stored usually in a

[20:13] repository and we are used to think

[20:15] about the repository as a database which

[20:17] is what I have in the example here

[20:19] but I want to stress that a repository

[20:22] is much more than a database for

[20:24] starters it doesn't have to be a

[20:26] relational database it might be no SQL

[20:30] mongodb for example or it might be

[20:33] something different for example a web

[20:36] API

[20:37] it's a source of data right you code the

[20:40] API you get data it sucks exactly what

[20:42] you do with the database

[20:44] it might be a bunch of text files which

[20:46] is a rudimentary database it might be a

[20:50] hardware sensor that is a source of data

[20:53] it's a repository okay so let's think

[20:56] about it as a database I'm going to say

[20:58] database probably a lot of times but the

[21:01] repository is the right word here

[21:06] and the database the repository exists

[21:10] in the in the external systems layer

[21:13] because again it's something that is not

[21:16] part of my core business it's not my

[21:18] business logic

[21:20] in system design we usually call the web

[21:23] framework the database everything that

[21:25] is outside as a detail we call it a

[21:28] detail and many many times it surprises

[21:31] people because they are like what

[21:33] details I mean configuring postcodes you

[21:35] know and all these things is complicated

[21:37] it's not it's not secondary it's not

[21:39] just so simple a detail means that it's

[21:44] not part of the business logic this is

[21:47] not what I'm marketing if my product

[21:49] works with postgres or works with

[21:52] mongodb

[21:53] you are not concerned you as a client

[21:55] are not concerned

[21:57] your problem is to receive a service

[21:59] so this is why it is a detail while the

[22:04] specific algorithm I use for my

[22:06] recommendation system for example is the

[22:08] core business is what you like of my

[22:11] product

[22:12] okay so this is the difference between a

[22:15] Core Business logic and a detail so the

[22:18] database is a detail even though it

[22:20] might be complicated

[22:22] however itex is in the external systems

[22:24] so as I said before the use case is not

[22:28] allowed to communicate directly with the

[22:31] database in Python terms

[22:35] um the use case is not allowed to

[22:37] instantiate anything that is

[22:40] tightly coupled with the database that

[22:43] is connected with the database directly

[22:45] because if I had code in my use case

[22:48] something which is I don't know postgres

[22:51] a library for to interact with postgres

[22:53] or to interact with mongodb I am

[22:56] coupling my use case with the

[22:58] implementation of the database

[23:00] and this is not good because at that

[23:03] point I have my core business my core

[23:05] business logic

[23:07] coupled with a detail which might change

[23:10] my mind might not be the same thing in

[23:13] time

[23:14] so I create an interface this is um in

[23:18] this case we are talking about python

[23:20] it's an object that provides a facade

[23:23] okay so a set of methods that are common

[23:26] to databases

[23:29] so the web framework instantiates the

[23:34] um

[23:36] database interface or the repository

[23:38] interface the web framework can do it

[23:40] because it's in an outer layer so the

[23:42] web frame sees can see what's in the

[23:46] gateways

[23:47] and passes the so it sends the in

[23:52] instance of the possible in this case to

[23:55] the use case this is called dependency

[23:58] injection if you are not familiar with

[24:00] the concept I have a slide for that

[24:02] later but look at it

[24:05] the use case the code of my use case

[24:08] doesn't have postgres or apple

[24:10] hard-coded in it I'm receiving an object

[24:13] that provides methods that might be an

[24:16] object of postgres repo mongodb repo

[24:19] type whatever the important thing is the

[24:22] interface

[24:25] or the set of methods that are that

[24:27] thing provides

[24:30] cool now internally I'm in the use case

[24:33] now and I have finally my business logic

[24:36] okay this is where my brain comes into

[24:40] play right I have to write something

[24:42] that implements the recommendation

[24:44] system the whatever filtering you want

[24:48] eventually sooner or later I have to use

[24:50] the database interface to retrieve the

[24:53] data so in this case I'm calling repo

[24:55] list okay passing the parameters that I

[24:58] received from the HTTP request

[25:00] or from outside I should say right where

[25:04] they come from is not important

[25:06] the business logic might be for example

[25:09] to prepare the parameters to add some

[25:12] filtering you know or to do something

[25:13] else

[25:16] anyway back to the database interface

[25:19] I'm calling repo list okay here right

[25:22] police so I'm in the database interface

[25:24] now and the database interface is

[25:27] tightly coupled with the database that

[25:30] is postgres repo so it's it has been

[25:33] designed to work with postgres okay it's

[25:37] in the name

[25:38] so the two

[25:40] um communicate with a specific language

[25:42] in this case I'm using SQL Alchemy it's

[25:46] an object or additional mapper but

[25:48] eventually I'm in um in a mindset of

[25:53] querying a relational database this is

[25:56] what I'm doing here okay I'm I'm

[25:58] committed to relational databases at

[26:02] this point and this is what I'm doing so

[26:04] the two things are tightly coupled

[26:07] or the database interface does is to as

[26:10] I said the query the database and then

[26:12] it transforms the output of the database

[26:15] the database just sends me values right

[26:17] it's it's SQL so just standard types

[26:22] known to SQL databases

[26:25] the database interface has the task to

[26:28] convert those values into entities

[26:31] because at this point everybody can see

[26:34] entities as a layer it's a very inner

[26:37] layer so the database interface can say

[26:40] okay these SQL values that I get become

[26:44] items

[26:46] as in items that I defined

[26:49] and these items are sent back to the use

[26:52] case as a result of the repo list at

[26:56] this point I can add more business logic

[26:58] okay just to say that obviously if you

[27:00] want to if you have to call your

[27:04] repository you can do it at any time and

[27:06] your business logic is around that code

[27:08] is where you augment these results okay

[27:12] with your algorithms with your

[27:14] cleverness

[27:16] your product

[27:18] at this point the use case has the

[27:21] results and it can send it back to the

[27:26] web framework or to whatever called it

[27:30] and the web framework so this is again

[27:32] sorry entities the web framework can

[27:35] knows about entities so this can still

[27:39] be

[27:40] um

[27:41] an entity a model that I created

[27:45] the web framework again has one task

[27:49] that of converting the entities which

[27:52] are specific models of my business into

[27:56] something that is understandable outside

[27:58] for example Json

[28:02] okay this is the task of the web

[28:05] framework my use case doesn't know

[28:07] anything about Json it doesn't care

[28:09] because the use case is okay with

[28:11] entities it's part of my business

[28:15] this is the Journey of the data in a

[28:18] clean system at least in this example

[28:21] in a clean architecture

[28:23] I want to go back quickly to

[28:27] um

[28:27] the initial slide about the Golden Rule

[28:31] right talk inwards through simple

[28:34] structures and outwards through

[28:36] interfaces have a look at this code

[28:38] these are two different possible

[28:40] implementations of a use case the second

[28:43] one is the one I used the first one is

[28:45] the incorrect one if you want

[28:48] why is it incorrect it works okay first

[28:51] of all so it's not incorrect from that

[28:54] point of view

[28:55] but these Calpers my code the code of my

[28:59] use case

[29:00] with the postgres rifle it means that I

[29:04] can't use anything else or if I want to

[29:07] use something else I have to touch the

[29:10] use case but the use case is your

[29:12] business logic and it shouldn't be

[29:14] touched because you change something

[29:15] which is a detail where you store data

[29:19] okay the second one instead and this is

[29:22] a good example I I hope of dependency

[29:24] injection

[29:25] is when you create something outside

[29:29] and then you pass an instance of it the

[29:32] instance has been instantiated outside

[29:35] so the part of the code that is coupled

[29:39] with a type the postcode so I put it

[29:42] outside in this case is the web

[29:43] framework

[29:45] your use case just receives an instance

[29:49] something that can accept a certain set

[29:52] of messages does it make sense I hope so

[29:55] this is what happens in a clean

[29:58] architecture simple structures inside

[30:00] interfaces outside

[30:03] cool

[30:05] um I want to tell you about the

[30:08] advantages of the clean architecture why

[30:10] should I go through all this pain

[30:14] and there are two specific things I want

[30:16] to mention

[30:17] um the first one probably the most

[30:19] important one for me is testability

[30:23] a clean architecture a software designed

[30:25] with a clean architecture can be tested

[30:28] very well

[30:30] what do I mean by that

[30:32] um look at the use case

[30:34] I can easily isolate the use case from

[30:38] the web framework and from the database

[30:40] interface it's just an object that

[30:43] receives a repo

[30:45] and some parameters and returns some

[30:48] results

[30:49] so what I can do is to pass a dictionary

[30:51] you know of parameters

[30:53] a mock database interface so something

[30:55] that pretends to be the database

[30:57] interface but is not connected with any

[30:59] database it's just you know amok it

[31:01] returns a fixed set of data

[31:04] and check that my business logic Works

[31:07] given that input gives some output

[31:11] this allows me to test my business logic

[31:14] in isolation I don't need the database

[31:18] to test my business logic I don't need

[31:20] the web framework because these are

[31:22] details my business logic is not about

[31:24] details

[31:26] at the same time I can test the details

[31:29] because yeah details but they are part

[31:32] of the implementation so I have to test

[31:34] that my web Frameworks

[31:37] and the web framework can be detached

[31:39] from the use case

[31:41] because the web framework the only task

[31:43] is not a simple task again but the only

[31:45] task of the web framework is to accept

[31:47] HTTP requests convert them into calls

[31:52] get the result of this code and convert

[31:55] it back to an HTTP response as I said

[31:58] it's not simple I have to test that this

[32:00] works and I can do it in isolation

[32:04] and last I have to test I can test my

[32:07] repository interface this requires

[32:11] the database because this is an

[32:13] integration test okay I'm testing that

[32:15] my the facade of the database works so I

[32:19] need a database running this might be a

[32:22] slow test you know that you might run

[32:24] just sometimes again I want to stress

[32:28] this I see it too many times when we

[32:31] when it comes to testing in particular

[32:32] with web Frameworks

[32:35] we end up testing the database

[32:38] so we store a model then we retrieve the

[32:41] model and we say hey it works thank you

[32:43] very much this means that Django or

[32:46] whatever framework you're using works

[32:49] that postgres works but this is not what

[32:51] you are supposed to test

[32:53] these are you know provided by third

[32:56] party is not part of the local business

[33:00] the second Advantage

[33:02] is about

[33:03] [Music]

[33:04] um

[33:04] customization I would say

[33:07] look at this in this case I have two

[33:10] different use cases one is to list items

[33:14] and one is two list users a phone sub

[33:16] for some reason from for example for

[33:18] performances reasons I store the users

[33:21] in a mongodb which is not relational

[33:24] okay so I can't use it with a SQL

[33:26] Alchemy for example because it's not SQL

[33:29] but this is not a problem in a clean

[33:31] architecture because my use case is

[33:34] customized it just has to receive a

[33:37] different object okay that is

[33:40] instantiated by the web framework

[33:44] um pay attention that this might happen

[33:45] inside the same use case so those use

[33:48] cases those two use cases might be the

[33:51] same and you might have some logic

[33:52] inside might be part of your business

[33:54] logic that says well in this case I go

[33:57] and fetch things from repo a in this

[34:00] case I go and fetch things from repo B

[34:04] performances for example okay but these

[34:06] repos are something that you get from

[34:08] outside as you see here I'm instantiated

[34:10] them

[34:11] in the web framework not in the use case

[34:16] and the other side of this is that the

[34:20] web framework is just one of the

[34:22] possible front ends and with front end

[34:24] now I don't mean reactors similar things

[34:26] I mean the way you present your results

[34:30] to the client it might be a common line

[34:33] interface it might be a web protocol or

[34:36] something else

[34:37] because I just need to code the use case

[34:40] and translate the output of the use case

[34:42] into something that is Meaningful for my

[34:44] front end okay HTTP request for a web

[34:47] framework something else for example

[34:49] text for a command line interface

[34:55] okay at this point

[34:57] um I want to draw a comparison with an

[35:00] architecture that we or at least many of

[35:02] you probably know which is the jungle

[35:04] architecture Django is a amazing web

[35:07] framework

[35:08] well known in the python Community it

[35:11] has a different architecture it's not

[35:14] the clean architecture this doesn't mean

[35:16] it's unclean maybe

[35:19] um well yes it's unclean from giving

[35:22] given the definition it's unclean

[35:24] of what I mean is

[35:26] I don't want to say it's bad okay I'm

[35:29] I'm coming back two days later I just

[35:30] want to draw the comparison for now

[35:33] um well Django has models these are

[35:36] similar to entities superficially

[35:38] speaking they just represent part of my

[35:40] business right I have items I have I

[35:42] don't know books you know films

[35:44] something that I'm marketing

[35:48] I have business logic obviously

[35:50] otherwise why should you use Django you

[35:53] have something to market right something

[35:54] to sell

[35:57] business logic in Django is usually

[35:59] implemented in views but it can be

[36:01] implemented in functions that are called

[36:03] in views okay so this is similar if you

[36:06] want to what I did before with the clean

[36:08] architecture

[36:11] okay for the first big difference Django

[36:14] as an uh om an object or additional

[36:17] mapper which is if you want a Gateway

[36:20] it's an interface

[36:22] because if you use

[36:25] um

[36:25] MySQL if you use postgres your views

[36:29] don't change right so you are using an

[36:31] interface you are using something that

[36:33] masks the details of the underlying

[36:36] database

[36:38] however the object relational mapper the

[36:41] name says it all it is an interface to

[36:44] relational databases and it's not easy

[36:47] to use Django with no relational

[36:50] database always something else okay a

[36:53] web API

[36:55] because of the OM is customized for

[36:58] relational databases and this is

[36:59] different in a clean architecture

[37:01] because the gateways is a more generic

[37:04] definition of interface

[37:08] last two components the database

[37:11] um which is an external system

[37:13] which is also tightly connected with

[37:16] models because Motors in Jungle can be

[37:19] saved everything from the database

[37:21] natively so the models are connected

[37:23] with the database they are aware of the

[37:25] database

[37:27] what is the drawback of these that when

[37:30] you test your Django application you

[37:33] need the database

[37:35] it is possible to test it without the

[37:37] database okay but you are sort of

[37:40] fighting against the framework you're

[37:42] doing something that the framework

[37:43] doesn't want you to do so again it might

[37:46] not be bad but it's different okay so

[37:49] there is a big connection between two

[37:51] layers one is the inner layer one is

[37:54] outside

[37:55] and the same happens for the web

[37:56] framework itself as in the part of the

[37:59] framework that deals with HTTP HTTP

[38:03] request responses

[38:04] because that is connected with the

[38:06] business logic as I said before you

[38:08] usually Implement your business logic in

[38:10] views and Views are specific things

[38:13] provided by the web framework they are

[38:15] connected with URLs right

[38:19] cool so so far this is the jungle

[38:21] architecture just to show you that there

[38:24] are different approaches to the things

[38:25] and as I said this might be a good

[38:27] approach it's not bad it's just

[38:29] different

[38:33] okay let's assume I convinced you okay

[38:36] in 40 minutes you are like yes the clean

[38:39] architecture is the way to go so I want

[38:41] to go back home and convert everything

[38:42] you know to the clean architecture well

[38:45] um don't do it

[38:48] so oh if you want to do it to do it the

[38:50] right way so I always recommend to um

[38:53] you know remember what happens to what

[38:56] happened to Netscape when they decided

[38:58] to rewrite the whole thing from scratch

[39:01] they lost everything have you heard of

[39:04] Netscape lately no you know well it

[39:06] resurrected at a certain point but you

[39:09] know uh it's a sad destiny that of

[39:12] Netscape and it's for a bad choice so

[39:16] don't do the same Choice migrations

[39:17] happen one step at a time

[39:20] so my recommendation if you want to try

[39:22] these Concepts is to isolate part of

[39:25] your system some something you know tiny

[39:27] in your system and re-implement it maybe

[39:30] with a clean architecture something that

[39:33] doesn't affect the rest of the of the

[39:36] architecture

[39:37] remember that when it comes to web

[39:39] applications you have load balancers

[39:41] they are your best friends you can

[39:44] always root a requests to another system

[39:48] okay and you can go back quickly if it

[39:50] doesn't work

[39:54] final slides is this the definitive

[39:57] architecture so

[39:58] done okay it's the perfect architecture

[40:01] we don't have to do anything else go and

[40:04] Implement everything with the clean

[40:06] architecture thank you very much

[40:09] so

[40:10] the answer to this question is in my

[40:13] opinion the answer to any computer

[40:16] science question ever and it is

[40:21] it depends okay it depends on many

[40:25] things it depends on your requirements

[40:28] for example when it comes to the clean

[40:30] architecture versus something else for

[40:32] example the jungle architecture I tend

[40:34] to show this slide you are the

[40:37] crossroads between these two options

[40:40] Lego versus Playmobil on the right you

[40:44] have something that works out of the box

[40:47] and it's it's very nice you you can play

[40:50] with it it's amazing you know I don't

[40:52] play with it nowadays but I remember I

[40:54] have fun memories

[40:56] you want a farm you get a farm it's

[40:59] customizable up to a certain point you

[41:02] can move things around okay

[41:04] on the other side on the left you can

[41:07] build whatever you want

[41:09] but you are on your own

[41:12] so it depends what are you constrained

[41:16] what do you want to achieve can you mix

[41:19] and match the two yes you can okay so

[41:22] this is my recommendation always

[41:23] whenever you design a system always stop

[41:26] and think look at the requirements don't

[41:29] go for a solution out of the box it

[41:32] might be the right solution but you have

[41:34] to be you know clear that why it is the

[41:37] right solution

[41:39] uh reading last two slides I wrote a

[41:43] book about this uh Concepts clean

[41:46] architectures in Python it's a free book

[41:48] it's available there

[41:50] the example I showed you today comes

[41:52] from the book there I implemented it

[41:55] properly with tdd all the way

[41:59] a lot of error checking so the whole

[42:02] book is about that example okay just

[42:04] write even a list of objects I

[42:05] implemented it with postgres and mongodb

[42:08] just to show that it's possible to use

[42:11] different databases

[42:12] for the Euro python so for this week and

[42:15] the next week I teamed up with some

[42:19] friends and I'm offering the book and

[42:22] other books for free it's a bundle worth

[42:24] of 60 dollars so you are free to follow

[42:29] us on Twitter and you can check the URL

[42:32] on the in Pub to get the bundle for free

[42:36] with that I'm done I hope it was useful

[42:40] thank you

[42:50] yeah thank you very much Leonardo for

[42:52] that very inspiring talk we have a few

[42:54] minutes that are available for questions

[42:57] so if anybody has a question we have a

[43:00] microphone here or just quickly go to

[43:03] that microphone and ask a question I'll

[43:06] be around for the next two hours so feel

[43:09] free to get in touch okay yes uh hello

[43:12] thank you for the really nice

[43:13] presentation and why I try to follow the

[43:17] layered architecture the one thing that

[43:20] I always struggled is with

[43:22] so if you want to really follow this

[43:25] then it seems like you have a lot of

[43:26] duplication so you have to have the

[43:28] models in the core of the business that

[43:30] your business or your cases are using

[43:32] and then you want to persist also those

[43:34] models into the database or somewhere

[43:36] else they need to have some kind of

[43:38] definition of how this happens with some

[43:40] bookkeeping data and potentially the

[43:43] duplicate the definition of the original

[43:45] data so since there's a application so

[43:48] do you have a suggestion how to avoid

[43:50] that or maybe make it less painful to

[43:53] implement the clean architecture

[43:57] I don't think there will be duplication

[43:59] we might we might talk about this later

[44:00] there is definitely a lot of message

[44:03] passing so if that is what you mean by

[44:06] duplication yes the same data is going

[44:08] around a lot which is part of the

[44:11] drawbacks of the clean architecture

[44:13] there are a lot of layers that is my

[44:15] impact performances like you don't have

[44:18] a direct access to the database

[44:20] but in terms of duplication I I'm not

[44:23] really sure what you mean so if you want

[44:24] to expand on that for example when in

[44:27] the example at the show for the jungle

[44:29] that is you define the model once and

[44:31] then this that's also the definition of

[44:33] how the table would look like and

[44:35] majority of RMS work the same way so you

[44:38] have single source of proof that defines

[44:41] basically the layout of the data in the

[44:43] database as well as the layout of the

[44:46] model that you're working with and for

[44:48] the data heavy application that's really

[44:50] convenient and very simple to like work

[44:52] with and Define your system so if you

[44:55] don't want to use that then you need to

[44:57] have I definitely see what you mean

[44:59] thank you

[45:00] um now I understand yes it's true that

[45:03] for example the model I use in entities

[45:06] correct me if I understood the wrong way

[45:08] is duplicated in the database interface

[45:12] because this is what I'm storing exactly

[45:14] it's true there is no way to avoid it

[45:17] because this is a feature

[45:20] as in I am splitting the model The

[45:25] Entity if you want as in a business

[45:26] entity and the entity has something that

[45:29] is stored in the database

[45:31] I agree this is more work

[45:35] and there is no way to avoid it because

[45:37] it's considered a good thing

[45:40] again it's part of the it depends the

[45:43] requirements you might think that in

[45:45] your business logic this separation is

[45:47] not needed or it's Overkill and you're

[45:50] free to go with a different architecture

[45:52] but the idea if we want the extreme idea

[45:57] is to split these two things because

[45:58] they might exist in the same space but

[46:01] they are not the same thing one thing is

[46:03] the business logic one thing is how I

[46:05] did the business entity if you want and

[46:07] one thing is how I store it

[46:10] okay thank you thank you very quick

[46:13] final question

[46:16] hi hi Leonardo thank you you're welcome

[46:19] I really appreciate your emphasis on

[46:21] beauty and art in terms of software

[46:25] um with clean architecture I can see the

[46:27] benefits of say durability uh utility

[46:30] how it by making a architecture clean do

[46:34] I get Beauty out of the box or is there

[46:36] something more elusive uh making

[46:39] something beautiful it might be a bit

[46:40] more elusive

[46:42] um you know beautiful beauty is to be

[46:45] defined

[46:46] to be honest when I was working on the

[46:49] book and I came up with the example and

[46:52] you know I was looking at the projects

[46:54] where components were how they were

[46:57] interacting between them a certain point

[46:59] my feeling and this is pretty personal

[47:01] it was like this is this is really

[47:03] beautiful it clicks it works and this is

[47:07] what I meant as in Beauty for me it

[47:11] might be subjective okay definitely it

[47:14] doesn't shine doesn't come in us you

[47:16] know but the beauty of that system for

[47:20] me is that it's again it's tidy I know

[47:23] where things are I'm confident that when

[47:26] I'm touching something and touching just

[47:28] the little Universe around that

[47:31] component and not other things

[47:33] this works for me and this is the beauty

[47:36] in that architecture for me thank you

[47:39] very much for the questions and let's

[47:41] have another round of applause for

[47:43] Leonardo thank you

⚡ Saved you time reading this? Transcribe any YouTube video for free — no signup needed.