Blank Slide
Jair Vercosa
Clean Architecture with Python
https://vercosa.life
https://medium/@jairvercosa
https://github.com/jairvercosa Made in Brazil
Carta helps private companies, public companies, and
investors manage their cap tables, valuations,
investments, and equity plans.
On the employee side, the platform provides ways to
participate on liquidity events and exercise their stock
options.
Private, Public, Investors, and beyond!
!2
1. Concepts
2. Rules
3. Layers
4. Code
5. The Startup Approach
Agenda
!3
Basic concepts
4
Clean Architecture
Universal rules of software architecture that when applied can dramatically improve developer
productivity throughout the life of any software system.
What is it?
!6
Rules
High-level components should avoid depending on low-level
components. The dependency is always inwards!
Dependency rule
Your system should scream your domain and not a
framework architecture.
Screaming architecture
Your system should scream your domain and not a
framework architecture.
Database, web, frameworks are all simple details
High cohesion and lower coupling in all levels (objects,
packages, systems)
Cohesion vs Coupling
Solid Principles
!7
!8
Solid Principles
!9
Solid Principles
!10
Solid Principles
!11
Solid Principles
!12
Solid Principles
Layers
13
Entities
Encapsulates the Enterprise wide business rules. They
are the least likely to change when something external
changes.
Definition
Entities
Architecture
Credentials
EncryptorPassword
Use Cases
Use Cases
It orchestrates the flow of data to-from entities. It has
the application business logic and manages the
different entities involved in some operation.
Definition
Entities
Architecture
Credentials
EncryptorPassword
CreateCredential <I>CredentialRepository
Interface Adapters
Responsible for translating the data from the database
or inputs into the format to be used by the domain.
Definition
Interfaces Adapters
Use Cases
Entities
Architecture
Credentials
EncryptorPassword
CreateCredential
AuthService DjangoCredentialRepository
<I>CredentialRepository
Infrastructure
Infrastructure
The edges of the application, usually where you collect
the inputs, deliver the outputs, persist data, and make
communication with external systems.
Definition
Interfaces Adapters
Use Cases
Entities
Architecture
Credentials
EncryptorPassword
CreateCredential
AuthService DjangoCredentialRepository
<I>CredentialRepository
RegisterView UserAccount
Architecture
Credentials
EncryptorPassword
CreateCredential
AuthService DjangoCredentialRepository
<I>CredentialRepository
RegisterView UserAccount
InfrastructureInterfaces
Adapters
UseCasesEntities
DB
UI
Benefits
23
Separation of concerns
!24
Well defined responsibilities for each layer
and clear boundaries.
Low coupling and high cohesion.
Better Testing
!25
The core of your business ends at the Use
Case level. Therefore you can test with simple
Python objects.
The dependency rule helps you to implement
interfaces for testing.
Flexibility
!26
The lower your layer, the easier becomes to
replace layers.
Depending on abstract/interfaces makes your
code way easier to work with.
The Startup Approach
27
Embrace the framework, but act as it wasn’t there
* Use interfaces/abstraction as much as you can
* Create an interface for queries, even if your ORM is nice. Specially if your ORM is nice!!
* Always have an Use Case layer for business logic
Build independent modules
* Your modules should care about only one thing. But it doesn’t mean only one model.
* Avoid foreign keys between modules
* Aways create a service layer for communication between modules
Delegate responsibilities! But don’t blindly trust it!
* Adapters! Adapters! Adapters!
Always revisit your code
Startup is about prototyping, testing, validating, failing, iterate! Don’t forget to iterate over your
code again!!!
32
One more thing…
33
Blank Slide
https://jobs.lever.co/carta/?department=Engineering
WE ARE HIRING!
Blank Slide
Jair Vercosa
Questions?
https://vercosa.life
https://medium/@jairvercosa
https://github.com/jairvercosa
https://github.com/jairvercosa/ca_auth

Clean architecture with Python