[0:05] thank you for being here so the title of [0:09] my talk today is clean architectures in [0:11] Python A Tale of durability utility and [0:15] beauty [0:16] and my goal today is so I'm here because [0:19] I want to share my views on system [0:22] design [0:24] and give you an example of what I [0:26] discovered and what I learned and maybe [0:28] meet people who are interested in system [0:31] design as well [0:33] so this is me well not a picture of me [0:38] but you know I'm a developer and a [0:41] blogger you are welcome to visit my blog [0:43] I usually write about python obviously [0:46] tdd object oriented cryptography and [0:50] infrastructure these are the main things [0:52] I like to write about [0:56] so today it's a talk about the clean [0:59] architecture and I want to start [1:02] with a question thinking a bit about [1:05] what architecture is [1:07] we mentioned architecture many times the [1:10] architecture of a system or what is it [1:13] what is it definition [1:15] so I found this in a book that has been [1:19] around for a while the architecture it [1:22] means about architecture [1:24] it's been written a couple of thousands [1:27] years ago [1:29] the two views who was a Roman architect [1:33] and engineer [1:35] says that architecture is about firmitas [1:39] utilitas and venustas which translated [1:42] in Modern English is durability [1:46] utility and beauty [1:48] so Vitruvius says that architecture is [1:52] about things that are durable something [1:56] that has to last [1:58] has to be useful has to be beautiful [2:02] and for me this was very interesting [2:04] because how many times [2:07] do you think about your code as [2:10] something that has to be useful [2:12] beautiful and durable [2:15] useful yes nobody wants to write [2:18] something that is useless [2:20] but what about durable [2:23] we change framework every two years now [2:26] right every six months I don't know so [2:28] having something that lasts [2:30] and beautiful that's the thing I'm [2:33] mostly concerned about [2:36] it's interesting let me mention this [2:38] that vituvius was an engineer and an [2:41] architect [2:43] and [2:44] these two professions don't go together [2:47] that much nowadays um it's interesting [2:50] to think that engineer the word comes [2:53] from engine [2:54] but engine comes we we when we discuss [2:58] about engines we think about something [3:00] mechanical [3:01] but engine actually comes from [3:04] um Ingenuity in Latin it's from [3:08] cleverness [3:09] so it's less about mechanical things is [3:12] more about solving a problem in a clever [3:15] way [3:16] anyway I went on and I checked also on [3:20] the dictionary Modern English dictionary [3:22] and I found these two interesting [3:24] definitions of architecture [3:27] one is the Art and Science of Designing [3:29] and making buildings [3:31] and the second one is more [3:33] concerned with computers and it says the [3:36] internal organization of computers [3:38] components with particularly the [3:40] reference to the way in which data is [3:42] transmitted I like these two definitions [3:44] I wanted to come up with something a bit [3:46] more compact [3:47] so I try to merge them into this which [3:51] is the Art and Science [3:53] in which the components of a computer [3:55] system are organized and integrated [4:00] I want to stress [4:01] Art and Science [4:04] how many times do you think about what [4:06] you do daily as Art and Science [4:10] as science yes data science computer [4:13] science [4:14] mechanical path right [4:16] what about art [4:19] when do you look at your code and think [4:21] this is art [4:22] why is it not data art it's not computer [4:25] art not as in you know painting [4:28] something with the computer but our code [4:31] is beautiful or it can be [4:33] and the other two interesting words here [4:36] are organized and integrated because a [4:39] system architecture when we when it [4:41] comes to computer science is all about [4:43] where components of the system are and [4:47] how data flows between them [4:51] so this is the integration part [4:54] cool now that I defined architecture [4:58] the following question is do we need it [5:02] so do we need things our code to be [5:06] useful durable and beautiful [5:10] and this is up to you I positive about [5:13] this otherwise it wouldn't be here [5:15] but I want to give you a couple of [5:17] examples and one comes from the [5:21] traditional architecture and it's the [5:24] page that is in front of this building [5:26] the Samoa backup page [5:29] it's interesting I was looking at it [5:31] these days and I thought [5:33] does this bridge have to be shaped like [5:36] an heart [5:39] if the requirement of the page is just [5:42] to take cars from one side to the other [5:45] it doesn't have you just need a plain [5:48] Bridge right something that I could [5:50] design because I don't know anything [5:51] about which design [5:54] but if the requirement is to make your [5:57] journey better [5:59] well being shaped like a hub in Dublin [6:03] is a nice thing to have [6:05] so this is Food For Thought maybe you [6:09] know like what is the requirement of [6:11] your code what what is it that you are [6:13] creating when you create a library do [6:15] you just create some Machinery or do you [6:16] create something to make the journey [6:18] better [6:19] the other example I have it's about [6:21] something lasting a long time [6:24] um [6:25] the Unix system it's an operating system [6:28] was designed in 71 so 50 years ago [6:31] and it was well thought [6:35] not everything in a unique system is [6:38] perfect okay sometimes it's far from it [6:41] but it was well thought [6:44] so well designed that I am running a [6:47] Linux machine and Linux is a clone of [6:49] Unix and many of you use Mac OS which is [6:53] a derivative of Unix 50 years [6:57] so again how many times do you look at [7:00] your code the code that you write and [7:02] you think [7:03] in 50 years people are still we will [7:05] still use this code or at least these [7:08] ideas [7:10] anyway I'm not the only one who thinks [7:13] that system design is an interesting [7:15] thing [7:16] um there are much smarter people than me [7:18] who wrote a lot of books there are vast [7:21] literature about this I selected five [7:24] books that I read which I believe are [7:27] interesting about the topic [7:30] some of these are door stoppers pretty [7:34] thick okay so if you are not up for the [7:36] challenge I recommend retrieving at [7:39] least the two in Orange so design [7:41] patterns and Enterprise Integration [7:43] patterns at least [7:46] read the introduction [7:49] and I'm not joking the introduction to [7:52] these two books the two introductions [7:54] are short [7:56] but they give you a narrative of the [7:59] challenges and some of the solutions [8:01] that you might have when you design a [8:04] system [8:05] and I was [8:07] um I was really [8:08] you know flabbergasted when I read the [8:12] introduction to design patterns because [8:14] it was like Hey I faced these issues [8:17] every day [8:20] I want to mention another thing about [8:21] Enterprise Integration patterns this is [8:24] a book about messaging message based [8:27] systems more about distributed systems [8:30] if you want [8:32] but it's interesting that message based [8:35] systems microservices for example [8:38] this software design and languages they [8:43] are all in the same league they share a [8:46] common trait [8:48] which is messages [8:52] object oriented programming and you can [8:54] quote me on this is supposed to be [8:57] about objects that exchange messages [9:02] so every time you call a method on an [9:04] object you are sending a message to [9:06] something [9:07] so it sees a distributed system [9:10] and if you if you think about that if [9:12] you when you code in Python if you think [9:14] about messages this might not change the [9:16] code itself because you are still [9:18] calling methods but it will definitely [9:21] change the way you think about your code [9:24] it's a bunch of objects it's a [9:27] distributed system and I'm exchanging [9:29] messages anyway this might be for [9:32] another talk another time now that I [9:34] defined architecture and I decided for [9:37] you that we need it [9:40] let me Define clean because this is [9:43] about a clean architecture [9:45] um I found it [9:47] easy to define the opposite of clean the [9:51] picture in the background if you look at [9:53] that system [9:54] you can definitely say this is not clean [9: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