Clean Architectures in Python - Leonardo Giordani - PyLondinium19

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the last regular talk of the weekend in this room I can see there are a couple of people coming up so I'll talk first a little bit longer this is the last intro I'll do I promise that I'll be off the stage right we better get started then the last people are wandering in now Leonardo is going to talk to us about clean architecture in Python he's a software engineer a in the film industry which i think is really cool company called we got pop and author of a book of the same name now I'm told by many authors that it doesn't pay that great so if we can listen to the talk and appreciate the advice maybe we can go out and buy the book as well he didn't pay me to say that promise so with that over to Leonardo thank you very much thank you thank you I am the third Italian in a row in this room and I dare to say this is statistically significant right so about me I just got our amazing introduction I'm us of the developer and a blogger and I'm the author a book named clean architectures invite let me start with a question what is the definition of architecture and Vitruvius who was a Roman architect first century in his book the architecture speaks about fermitas Attila does venustas which in modern english is durability utility and beauty so he says that architecture deals with something that should last the test of time stand the test of time sorry be useful and be beautiful when it comes to software architecture so software development the dictionary gave me an interesting definition soft architecture is the art and science which the components of a computer system are organized and integrated so first of all art and science how many of you consider themselves artists many consider themselves engineers right but artists interesting the second thing is organization and integration so architecture software architecture has to deal with where components of your system are and how data flows between these components now that we know officially at least what architecture is the second question is do we need an architecture and a very short answer is yes I don't have that much time but the main point my point is if you don't decide anything about your architecture someone will decide for you typically your framework many of you if you create web of protecting applications or mobile application use frameworks right the framework enforces an app an architecture unfortunately the framework is just part of the system so know that no decision is already a decision the third question is what is the meaning of clean I guess it's very easy to understand what unclean is this I don't want to work in such an environment because I don't know which cable have to pull it's too risky while in a clean environment you know where things are you know why components are there and you know what something is okay what is the clean architecture this so the clean architecture is a layered architecture so it's a way to create systems based on layers here we have three well four layers let me spend a couple of minutes on this slide layered architecture means that when you create something in your system you are not free to put it where you want components are forced to live in a certain layer according to the in nature here we have entities use cases and external systems plus half layer which is called gateways the important thing in our layered architecture like these is that what have a leaves in an internal layer like for example entities doesn't know anything about what lives in outer layers I've come up with an example in a second but it's important to understand that if you create for example a Python class in the entities layer you are not allowed to name to call any class that has been created in an outer layer the golden rule of the green architecture is that external layers talk to internal layers through simple structures simple structures are for example structures defined by the language lists dictionaries all structures defined in an internal layer like entities and you talk outwards through interfaces why because you are not allowed as I said to call a specific class that has been defined outside you don't know about it so you have to go through an interface we don't have in Python specific construction it comes to construct for interfaces like for example in Java but we can still use interfaces so this is my example let's say we are dealing with a very simple model an object I am creating a clone of ebay for example ok in the entities layer there is a class object as you can see this is not a model like for example jungle models this is a very simple class it might be at that class might have methods but is definitely not connected with the database this is something new probably my use case is retrieve a list of objects ok so for example I have a cart I have a filtering someone is searching for an object so my use case lives in the use case layer use cases layer and the use case is a simple class I can instantiate it and execute it the only thing you can do with the use case is to execute it it's very very simple so I call execute in 2019 we are probably talking about a web application but it could be a mobile application it could be a common line interface I'm just giving you an example if this is a web application I can go off with flask for example and create an endpoint like this cool so the task of the web framework is that of receiving HTTP requests and translating these requests extracting Department parameters like the query string parameters for example and calling a use case so the HTTP request becomes a call with some parameters then we have a repository because we need to extract data from somewhere in this case for example a database again could be something different let's say a database the database as you see lives in the external system layer why because I don't want to be coupled with the database I just want to say there's a database somewhere something that can provide data but if I want to access the database I need an interface and the interface is specific for my business it's not an interface that map's the whole database to Python for example it's a specific interface the interface Maps what the database returns in terms of a specific language in two simple structures and entities again as you can see here there's an example a brief example of a interface towards postcards I expose one method which is list that except some filters I have a query that depends on these filters and I am returning returning some entities so the use case has to receive also the repository interface as you see I added the dictionary plus database interface because this is the pentacene version the use case cannot call the interface direct it cannot name it because the use case doesn't know the class is there so I have to pass the instance to the use case as you can see here I initialize the repository then I call the use case passing the repository instance and then I call the use case they execute execute method now the use case calls the repository interface so inside there it was the use case what happens is that the use case has to interact with the database somehow interacts with the interface extracts data and applies the business logic this is the core of your application because when you create an application the core of it is the business are the business rules you shouldn't be concerned with all the user clicks a button on the graphical user interface fine but what happens the user is deleting the message okay this is the business case this is the business use case so what happens in the use case is that we run the business logic and then we return a result what does the result go the result goes back to the external system that called the use case in this case the web framework so here you see I collect the result of execute in a result variable and at this point the web framework is in charge of converting this response which may be entities for example or again simple structures into an HTTP response so as you can see from this picture I am applying the separation of concerns okay so every actor in this system is dealing with a specific thing so the web framework is dealing with HTTP requests and responses the use case is dealing with some parameters and some data the database interface is dealing with the database advantages of this architecture are many there are many advantages two of them that I want to mention are testability and ease of change if you want easily changed so testability is this testing the components of this system is very easy because if I want to test the use case as you if you see if you look at this class testing it is very simple because I just need to pass a dictionary and a mock of the repository interface and I have to test that the use case gives me back the correct entities and an interface call it's a very simple test five lines testing the HTTP endpoint is very simple again I need an HTTP request some mocks for the use case for the repository and then I have to test the outputs my point is I don't need to instantiate the database for example if you are used to jungle just to name one framework very good framework by the way when you test your models you have to have a database running not here when do you need the database when you tested up on the repository interface obviously is much Kosinski here no he lives there you know he's dealing with database interfaces so in this case you need an integration test so here you need the real database running and here you need to for example run spin up docker images but these are slow tests one of the 30 now that they give you a very simple example I want to address a couple of other questions so is this the definitive architecture I don't think so so this is a very good architecture I really really suggest using it but this is not not the best architecture because I believe there is no one's not not a single solution to every problem so one of the problems of the clean architecture is that it's a layered architecture there are a lot of layers I showed you the minimum amount of layers for but you might have more layers means data transformation as you see you know something comes out of the database becomes a simple structure goes into the use case then becomes another structure then becomes a HTTP response bla bla bla so you have all these data transformation these a means time this means that you are affecting the performances of your system so not every system can be can be created with a clean architecture it's up to you to understand this it's not a panacea and the second question is is it possible to migrate an existing system towards a clean architecture and my advice here is to detach simple cases for example end point if you are dealing with a web application you might start with simple endpoints for example the login and point for example some data structures and implement them in a different system created from scratch with the clean architecture don't try to move your whole junk obligation towards the clean architecture in one go it doesn't work don't do it I have a lot of time ok fine so so there is one thing I want to say that is not in his slides but there are many books about domain domain driven design about clean architectures you might yes thank you you might argue these books come from the Java community ok many people writing books about these subjects come from the Java community we say many bad things about the Java community definitely the language is not the best one around probably please but the Java community has been addressing these issues for the last 30 years so we definitely have something to learn from those people and I definitely believe we should get in touch with them Harry Percival and Bob Gregory are about to write a book with orally called what they work entitles pythonic application architectural patterns the definite title will be different or really agreed to publish it with an open-source license so you can find it on github you can already read it you can you know send pull requests or something if you believe you can contribute to it and there is already a book about cleaning architectures in Python I published it last Christmas it's available for free so you are kindly invited to go and download it and read it I'm really really looking forward to discussing with people architectural problems because my point today here is to start again a discussion about architectures I believe we have incredible frameworks in the Python community one is jungle for example it's a very very really a good product but we are not addressing some of the problems like the architectural problems we are delegating the framework so we should start discussing again these things I am yes I mean it's early so maybe time for questions if you want [Applause] thanks you I love that architecture I used to work with that sort of architecture now I work with Django and makes me a bit sad inside you mentioned Django and you mentioned walk into a new way of doing things is there any advice that you can give me to to make Django to mold it into a more clean architecture okay yeah it's with jungle it's hard because Django well for start because it's a it's a very good framework but tries to do too much for me so for example jungle motors are tightly connected with the database so it's difficult to work with Django and motors that are not connect with the database my point is generally speaking you can always wrap things and trace a boundary so you might as I said one thing that you can say one thing you can do is to say okay I isolate part of my system some end points for example if you are working with junk it's a web application you know so I so linked in some part of it and say okay I move towards something different maybe with the same database you if you create a database interface that works with jungle with what jungle created you can deal with it but yes definitely is what I was saying before don't try to ban Django the whole structure that you created with jungle towards the cleaner etcher you should try to you know move jungle to it's part of the system so dealing with HTTP requests probably jungle it's really hard I chose flask in my example exactly because it's from the ground up it's simpler you know but yes I don't want I don't want these discussion in this talk to be like you know cleanup that you versus jungle the my my point is where I mentioned the web framework I mean a web framework so whatever Django can be there but definite jungle wants to deal with the database no not in this architecture I hope I answered I think thank you for your for your talk and I was wondering many mother systems are moving to for example microservice face architecture where there are several components and they machine each of the components having this general shape but can something like this be used to design the architecture or the larger scale or how different components going to each other or what kind of patterns do you ambition that are relevant at the scale oh that's interesting okay complex question okay short answer we might discuss it later short answer is I believe the power the the main point of the clean architecture is layering so separating concerns okay so this is the point of micro-services as well so when you create microservice it's a buzzword nowadays you know but why do we want micro services we were discussing with someone at lunch you know about monolithic architectures what's the definition of molarity a micro service is monolithic if you trace a boundary around the micro service it's a monolith so the point is this it depends on what you are doing so what is the definition of micro service what what parts of your system should you the couple from others not everything you know and you will have coupling so I know I'm not giving you a like a straight answer but my point is the clean architecture is a software architecture but generally speaking is about putting components in the right layer and enabling the communication between them and forcing some rules which is the Golden Rule like small and simple structures and interfaces but yeah we can discuss about it definitely thank you opening slide and wondering if perhaps the work of Christopher Alexander who inspired the designer pants people yes might be relevant here definitely I didn't mention any because I thought I didn't have time but what you are saying is that the design pattern which is a very important book in our in our job has been has been inspired by architectural concerns I didn't read the book the book Alexander wrote so I don't I cannot answer directly I believe that the main the main thing I learned from Alexander not having read the book is he observed something that happened in his job will happen in his community and it tried to formalize this so we shouldn't be scared of saying this is what happens in when we create web applications for example this is what happens when I did with databases this is what happens when I create big systems and trying to instead of avoiding things or masking you know problems addressing them this is what I can say but again it's a limited experience with that book Alexander's book thank you very much draw questions to a close thank you you
Info
Channel: PyLondinium
Views: 8,221
Rating: 4.9183674 out of 5
Keywords: PyLondinium, Conference, Python
Id: wtCQalq7L-E
Channel Id: undefined
Length: 23min 57sec (1437 seconds)
Published: Tue Jul 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.