├── CleanArchitecture.jpg ├── README.md └── rentomatic ├── data_providers ├── mem_room_repository.py ├── mongo_room_repository.py └── postgres_room_repository.py ├── entities └── room.py ├── interfaces └── room_repository.py └── use_cases └── list_rooms.py /CleanArchitecture.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tducret/python-clean-architecture-example/678a5f7bcbbcd7c1f5c16ccc409c096db733f657/CleanArchitecture.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Clean architecture example with Python 2 | 3 | ## Resources 4 | 5 | - [The clean architecture](http://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html) 6 | - [Clean architectures in Python](https://leanpub.com/clean-architectures-in-python) 7 | - [Clean architecture example](https://github.com/mattia-battiston/clean-architecture-example) 8 | - [Real life clean architecture](https://www.slideshare.net/mattiabattiston/real-life-clean-architecture-61242830) 9 | - [A Guided Tour inside a clean architecture code base](https://proandroiddev.com/a-guided-tour-inside-a-clean-architecture-code-base-48bb5cc9fc97?gi=eba71cf04e7) 10 | 11 | ## Description of the layers (from inside to outside) 12 | 13 | ![The clean architecture (cleancoder.com)](CleanArchitecture.jpg) 14 | 15 | ### Entities 16 | 17 | Representation of the domain models 18 | 19 | > It contains classes, with methods that simplify the interaction with them. 20 | 21 | > These models are not connected with a storage system, so they cannot be directly saved or queried using methods of their classes, they don’t contain methods to dump themselves to JSON strings, they are not connected with any presentation layer. They are so-called lightweight models. 22 | 23 | > Entities have a mutual knowledge since they live in the same layer, so the architecture allows them to interact directly. This means that one of your Python classes can use another one directly, instantiating it and calling its methods. Entities don’t know anything that lives in outer layers, however. For example, entities don’t know details about the external interfaces, and they only work with interfaces. 24 | 25 | Example : 26 | 27 | - **/entities/room.py** : `class Room` 28 | 29 | ### Use cases 30 | 31 | > Use cases are the processes that happen in your application, where you use you domain models to work on real data. Examples can be a user logging in, a search with specific filters being performed, or a bank transaction happening when the user wants to buy the content of the cart. 32 | 33 | > A use case should be as small a possible. It is very important to isolate small actions in use cases, as this makes the whole system easier to test, understand and maintain. Use cases know the entities, so they can instantiate them directly and use them. They can also call each other, and it is common to create complex use cases that put together other simpler ones. 34 | 35 | The use cases layer dictates the contract the upper layers must follow (since they can be used with dependency injection) 36 | 37 | Example : 38 | 39 | - **/use_cases/list_rooms.py** : `class ListRooms` 40 | 41 | ### Interfaces 42 | 43 | Controllers, gateways, presenters, data providers, entrypoints 44 | 45 | > A set of adapters that convert data from the format most convenient for the Use Cases and Entities to the format most convenient for an external agency such as a database. 46 | 47 | Conversion of data format : 48 | 49 | - use cases and entities format => external systems format (ex : Database, API) 50 | - external systems format => use cases and entities format 51 | 52 | > No code inward of this circle should know anything at all about the database. If the database is a SQL database, then all the SQL should be restricted to this layer, and in particular to the parts of this layer that have to do with the database. 53 | 54 | Example : 55 | 56 | - **/interfaces/room_repository.py** : `class RoomRepository` 57 | 58 | ### External systems 59 | 60 | > Generally you don’t write much code in this layer other than glue code that communicates to the next circle inwards. 61 | 62 | - Configuration (database…) 63 | - APIs (used or exposed) 64 | - Web framework 65 | - Storage 66 | 67 | Examples : 68 | 69 | - **/data_providers/mem_room_repository.py** : `class MemRoomRepository` 70 | - **/data_providers/mongo_room_repository.py** : `class MongoRoomRepository` 71 | - **/data_providers/postgres_room_repository.py** : `class PostgresRoomRepository` -------------------------------------------------------------------------------- /rentomatic/data_providers/mem_room_repository.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tducret/python-clean-architecture-example/678a5f7bcbbcd7c1f5c16ccc409c096db733f657/rentomatic/data_providers/mem_room_repository.py -------------------------------------------------------------------------------- /rentomatic/data_providers/mongo_room_repository.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tducret/python-clean-architecture-example/678a5f7bcbbcd7c1f5c16ccc409c096db733f657/rentomatic/data_providers/mongo_room_repository.py -------------------------------------------------------------------------------- /rentomatic/data_providers/postgres_room_repository.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tducret/python-clean-architecture-example/678a5f7bcbbcd7c1f5c16ccc409c096db733f657/rentomatic/data_providers/postgres_room_repository.py -------------------------------------------------------------------------------- /rentomatic/entities/room.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tducret/python-clean-architecture-example/678a5f7bcbbcd7c1f5c16ccc409c096db733f657/rentomatic/entities/room.py -------------------------------------------------------------------------------- /rentomatic/interfaces/room_repository.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tducret/python-clean-architecture-example/678a5f7bcbbcd7c1f5c16ccc409c096db733f657/rentomatic/interfaces/room_repository.py -------------------------------------------------------------------------------- /rentomatic/use_cases/list_rooms.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tducret/python-clean-architecture-example/678a5f7bcbbcd7c1f5c16ccc409c096db733f657/rentomatic/use_cases/list_rooms.py --------------------------------------------------------------------------------