โ Back to list

clean-architecture
by eco2-team
๐ฑ ์ด์ฝ์์ฝ(Ecoยฒ) BE
โญ 0๐ด 0๐
Jan 25, 2026
SKILL.md
name: clean-architecture description: Guide for implementing Clean Architecture in Python/FastAPI projects. Use when creating new modules, refactoring existing code to Clean Architecture, designing layer structures, or implementing Port/Adapter patterns. Triggers on "clean architecture", "hexagonal", "ports and adapters", "layer structure", "DIP", "dependency inversion".
Clean Architecture Implementation Guide
Quick Reference
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Dependency Rule โ
โ Dependencies ALWAYS point inward (to Domain) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ Presentation โโโถ Application โโโถ Domain โโโ Infrastructure โ
โ (Controllers) (Use Cases) (Entities) (Adapters) โ
โ โ
โ Infrastructure IMPLEMENTS Domain/Application Ports โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Layer Responsibilities
| Layer | Responsibility | Allowed Dependencies |
|---|---|---|
| Domain | Business rules, Entities, Value Objects | None (pure Python) |
| Application | Use Cases, Orchestration | Domain only |
| Infrastructure | DB, External APIs, Framework adapters | Domain, Application |
| Presentation | HTTP/CLI, Request/Response handling | Application |
Standard Directory Structure
app_name/
โโโ domain/
โ โโโ entities/ # Aggregate roots, Entities
โ โโโ value_objects/ # Immutable value types
โ โโโ services/ # Domain services (stateless)
โ โโโ ports/ # Domain-level abstractions
โ โโโ enums/ # Domain enumerations
โ โโโ exceptions/ # Domain exceptions
โ
โโโ application/
โ โโโ commands/ # Write Use Cases (Interactors)
โ โโโ queries/ # Read Use Cases (QueryServices)
โ โโโ ports/ # Application-level abstractions
โ โโโ dto/ # Data Transfer Objects
โ โโโ services/ # Application services
โ โโโ exceptions/ # Application exceptions
โ
โโโ infrastructure/
โ โโโ adapters/ # Port implementations
โ โโโ persistence/ # ORM, migrations
โ โโโ integrations/ # External API clients
โ โโโ exceptions/ # Infrastructure exceptions
โ
โโโ presentation/
โ โโโ http/
โ โโโ controllers/ # FastAPI routers
โ โโโ dependencies/ # FastAPI Depends
โ โโโ schemas/ # Pydantic request/response
โ
โโโ setup/
โโโ config.py # Settings
โโโ dependencies.py # DI wiring
Port & Adapter Pattern
Defining Ports (Interfaces)
# application/ports/user_repository.py
from typing import Protocol
class UserRepository(Protocol):
"""Port: Abstract interface for user persistence"""
async def get_by_id(self, user_id: UserId) -> User | None: ...
async def save(self, user: User) -> None: ...
async def exists_by_email(self, email: Email) -> bool: ...
Implementing Adapters
# infrastructure/adapters/postgres_user_repository.py
class PostgresUserRepository:
"""Adapter: Concrete implementation using PostgreSQL"""
def __init__(self, session: AsyncSession):
self._session = session
async def get_by_id(self, user_id: UserId) -> User | None:
stmt = select(UserModel).where(UserModel.id == user_id.value)
result = await self._session.execute(stmt)
row = result.scalar_one_or_none()
return self._to_entity(row) if row else None
CQRS Pattern
Command (Write Operation)
# application/commands/create_user.py
@dataclass(frozen=True)
class CreateUserCommand:
email: str
password: str
class CreateUserInteractor:
def __init__(
self,
user_repo: UserRepository,
hasher: PasswordHasher,
tx: TransactionManager,
):
self._repo = user_repo
self._hasher = hasher
self._tx = tx
async def execute(self, cmd: CreateUserCommand) -> UserId:
# Domain logic
user = User.create(
email=Email(cmd.email),
password_hash=await self._hasher.hash(cmd.password),
)
await self._repo.save(user)
await self._tx.commit()
return user.id
Query (Read Operation)
# application/queries/get_user.py
class GetUserQueryService:
def __init__(self, reader: UserQueryGateway):
self._reader = reader
async def execute(self, user_id: UUID) -> UserDTO | None:
return await self._reader.get_by_id(user_id)
Naming Conventions
| Type | Pattern | Example |
|---|---|---|
| Port (data access) | {Entity}Repository or {Entity}Gateway | UserRepository |
| Port (action) | {Action}er | PasswordHasher, TokenGenerator |
| Adapter | {Tech}{Port} | PostgresUserRepository, BcryptHasher |
| Command Use Case | {Verb}{Noun}Interactor | CreateUserInteractor |
| Query Use Case | {Verb}{Noun}QueryService | ListUsersQueryService |
| Value Object | {Noun} (noun form) | Email, UserId, Money |
Reference Files
- Layer details: See layer-structure.md
- Port/Adapter examples: See port-adapter.md
- CQRS patterns: See cqrs-patterns.md
- Evaluation checklist: See evaluation-checklist.md
- Anti-patterns to avoid: See anti-patterns.md
Ecoยฒ Project Conventions
This project follows conventions from docs/foundations/16-fastapi-clean-example-analysis.md:
- Use
Protocolover ABC for interfaces (structural typing) - Gateway naming for CQRS split (
CommandGateway,QueryGateway) - Separate
FlusherandTransactionManagerports - Value Objects with self-validation in
__post_init__
Score
Total Score
60/100
Based on repository quality metrics
โSKILL.md
SKILL.mdใใกใคใซใๅซใพใใฆใใ
+20
โLICENSE
ใฉใคใปใณในใ่จญๅฎใใใฆใใ
+10
โ่ชฌๆๆ
100ๆๅญไปฅไธใฎ่ชฌๆใใใ
0/10
โไบบๆฐ
GitHub Stars 100ไปฅไธ
0/15
โๆ่ฟใฎๆดปๅ
3ใถๆไปฅๅ ใซๆดๆฐใใใ
0/10
โใใฉใผใฏ
10ๅไปฅไธใใฉใผใฏใใใฆใใ
0/5
โIssue็ฎก็
ใชใผใใณIssueใ50ๆชๆบ
+5
โ่จ่ช
ใใญใฐใฉใใณใฐ่จ่ชใ่จญๅฎใใใฆใใ
+5
โใฟใฐ
1ใคไปฅไธใฎใฟใฐใ่จญๅฎใใใฆใใ
0/5
Reviews
๐ฌ
Reviews coming soon