Giter Site home page Giter Site logo

python-clean-architecture-example's Introduction

Clean architecture example with Python

Resources

Description of the layers (from inside to outside)

The clean architecture (cleancoder.com)

Entities

Representation of the domain models

It contains classes, with methods that simplify the interaction with them.

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.

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.

Example :

  • /entities/room.py : class Room

Use cases

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.

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.

The use cases layer dictates the contract the upper layers must follow (since they can be used with dependency injection)

Example :

  • /use_cases/list_rooms.py : class ListRooms

Interfaces

Controllers, gateways, presenters, data providers, entrypoints

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.

Conversion of data format :

  • use cases and entities format => external systems format (ex : Database, API)
  • external systems format => use cases and entities format

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.

Example :

  • /interfaces/room_repository.py : class RoomRepository

External systems

Generally you don’t write much code in this layer other than glue code that communicates to the next circle inwards.

  • Configuration (database…)
  • APIs (used or exposed)
  • Web framework
  • Storage

Examples :

  • /data_providers/mem_room_repository.py : class MemRoomRepository
  • /data_providers/mongo_room_repository.py : class MongoRoomRepository
  • /data_providers/postgres_room_repository.py : class PostgresRoomRepository

python-clean-architecture-example's People

Contributors

tducret avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.