Architecture Intermediate
Clean Architecture — Layers and Dependencies¶
Clean ArchitectureSOLIDUncle Bob 3 min read
Robert C. Martin’s Clean Architecture. The dependency rule, entities, use cases, and implementation.
Four Layers¶
- Entities — enterprise business rules
- Use Cases — application business rules
- Interface Adapters — controllers, gateways
- Frameworks — DB, UI, web framework
Dependencies point only inward. Inner layers know nothing about outer ones.
Implementation¶
# entities/user.py — no external imports!
class User:
def __init__(self, name: str, email: str):
if '@' not in email: raise ValueError("Invalid email")
self.name = name
self.email = email
# use_cases/create_user.py
class CreateUserUseCase:
def __init__(self, user_repo: UserRepository):
self.user_repo = user_repo
def execute(self, name, email) -> User:
user = User(name, email)
self.user_repo.save(user)
return user
Dependency Inversion¶
The use case defines the interface, the outer layer implements it. This is the key to independence.
Summary¶
Clean Architecture ensures business logic independence from frameworks. Clean, Hexagonal, and Onion share the same principle — domain isolation.
Need Help with Implementation?¶
Our team has experience designing and implementing modern architectures. We’re happy to help.