python-fastapi-ddd-presentation-skill
2
总安装量
2
周安装量
#66925
全站排名
安装命令
npx skills add https://github.com/iktakahiro/python-fastapi-ddd-skill --skill python-fastapi-ddd-presentation-skill
Agent 安装分布
amp
2
claude-code
2
github-copilot
2
codex
2
kimi-cli
2
gemini-cli
2
Skill 文档
FastAPI Presentation Layer (DDD / Onion Architecture)
This skill focuses on the Presentation layer only: FastAPI routes/handlers, Pydantic schemas, and HTTP error mapping. It assumes you already have Domain + UseCase layers and repository wiring.
Non-negotiables (Onion rule)
- Presentation is the outermost layer: it can depend on UseCase and Domain types, but Domain must not depend on FastAPI/Pydantic.
- Keep business rules inside Domain/UseCase. Presentation does:
- request parsing/validation (shape, basic constraints)
- conversion primitives â Value Objects
- calling
usecase.execute(...) - mapping Domain exceptions â
HTTPException - conversion Entity â response schema
Recommended structure (per aggregate)
presentation/
api/
{aggregate}/
handlers/
schemas/
error_messages/
Implementation checklist (per endpoint)
- Depend on UseCase interface via
Depends(get_*_usecase). - Convert inputs (
UUID,str, etc.) into Domain Value Objects. - Handle ValueError (from Value Objects) as
400 Bad Request. - Execute the use case.
- Map Domain exceptions (e.g.,
NotFound, lifecycle errors) to404/400. - Return response model using
Schema.from_entity(entity)(or equivalent). - Document errors in OpenAPI using
responses={...: {'model': ...}}.
Route handler pattern (based on dddpy)
Prefer a small âroute registrarâ class per aggregate.
class TodoApiRouteHandler:
def register_routes(self, app: FastAPI):
@app.post("/todos", response_model=TodoSchema, status_code=201)
def create_todo(
data: TodoCreateSchema,
usecase: CreateTodoUseCase = Depends(get_create_todo_usecase),
):
try:
title = TodoTitle(data.title)
description = (
TodoDescription(data.description) if data.description else None
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e)) from e
todo = usecase.execute(title, description)
return TodoSchema.from_entity(todo)
Pydantic schemas
- Request schemas: validate shape + basic constraints (min/max length, optional fields).
- Response schemas: provide
from_entity()to convert Domain types (UUID/datetime) into JSON-friendly primitives (e.g., timestamps as milliseconds).
For detailed templates and a fuller walk-through, read references/PRESENTATION.md.