The Clean Architecture in Python. How to write testable and flexible code
(Hey! This article is almost 2 years old :) Look here for something about the Clean Archictecture in 2021) An ideal project? If someone asked about the features of an ideal project, responses would surely mention a few specific things. First of all, an ideal project would have a clean codebase that is simple to read. Secondly, there should be high test coverage to ensure that the project works as expected. What is more, one will instantly know if they broke something thanks to an extensive suite of automated tests. Last, but not least - technical debt should be kept at bay to not pose a threat of lowering a team's velocity. I bet you already see how enjoyable would be to work on such a project! Unfortunately, software like this is a real rarity. Time to change it. Building such a masterpiece sounds like a hell of a work. Luckily, countless software engineers have already responded to the challenge. This article describes one of the proposed designs - so-called The Clean Architecture authored by Robert C Martin in his 2012 blog post and a book that was published at the end of 2018. As you can see, the concept has not been conceived yesterday. The Clean Architecture based on even older concepts, such as Onion Architecture or Ports and Adapters (AKA Hexagonal Architecture). Motivation I wrote this blog post because I succeeded in applying the Clean Architecture in two Python projects - both of them reached production and are still being used and developed. This article contains python-specific techniques and tools helpful in embracing the Clean Architecture. The article is based on my talk that I gave 13 times so far (including PyCarribean, PyGotham or Python London Meetup). See recording here. The most interesting observation that leads to searching for better ways of writing software is the fact that all software is created to satisfy business needs. Since the requirements (and code that implements them) are crucial, we want to treat them as the first-class citizen. It means one should not scatter business logic around in random places. It deserves to have its own, designated area which is independent of tools we use. In short, we want our business logic to be: independent of framework easily testable in isolation without having to mock out half of the code independent from UI, API independent from databases independent from any third party services Such a level of decoupling is possible thanks to using abstraction layers and techniques like Dependency Injection. Example project - auctioning platform To illustrate the Clean Architecture, we will use an example project of an auctioning system. Among requirements there are these User Stories: As a bidder, I would like to place a bid to win an auction As a bidder, I would like to be informed by an e-mail when my bid is the winning one As an administrator, I would like to withdraw a selected bid from an auction Let's use Django + DRF Django is the most popular web framework in the Python world. It makes it possible to create web applications in no time (at least in the beginning) thanks to a plethora of goodies bundled with the framework. Django Rest Framework (DRF) brings to the party a bunch of classes helpful during creating a REST API. Among these goodies, we get certain building blocks which one can arrange in order to build the project. The building blocks are: The most important Django + DRF building blocks At first glance, one can see that there is one central building block, on which all others depend. It is Model - a class that will be mapped onto a table in a database. In other words, in order to do anything meaningful with Django one has to define their Models first. One way is to take a look at User Stories and look for nouns: As a bidder, I would like to place a bid to win an auction As a bidder, I would like to be informed by an e-mail when my bid is the winning one As an administrator, I would like to withdraw a selected bid from an auction Every word in bold from above list is a perfect candidate for a Model. Let's focus on the most important two: class Auction(models.Model): title = models.CharField(...) initial_price = models.DecimalField(...) current_price = models.DecimalField(...)
breadcrumbs collector.tech