Clean Architecture
Enforce Clean Architecture principles across all application code. The core rule: dependencies
point inward. Outer layers depend on inner layers, never the reverse.
Layer Model
┌──────────────────────────────────────┐
│ Frameworks & Drivers (outermost) │ Rails, React Native, PostgreSQL, Redis
├──────────────────────────────────────┤
│ Interface Adapters │ Controllers, Serializers, Presenters, Gateways
├──────────────────────────────────────┤
│ Use Cases (Application Logic) │ Service objects, Interactors, Commands
├──────────────────────────────────────┤
│ Entities (Domain Logic) (innermost) │ Models, Value Objects, Domain Rules
└──────────────────────────────────────┘
The Dependency Rule
- Entities know nothing about use cases, controllers, or frameworks.
- Use Cases know about entities but not about controllers, serializers, or databases.
- Interface Adapters translate between use cases and external concerns.
- Frameworks are implementation details — pluggable and replaceable.
Testing by Layer
- Entities: Unit tests, no mocks needed (pure domain logic).
- Use Cases: Unit tests with mocked repositories/gateways.
- Interface Adapters: Integration tests (controller specs, serializer specs).
- Frameworks: Minimal testing — trust the framework, test your configuration.
The Universal Violations
Every platform expresses the same four mistakes. The per-platform guides below name each one
concretely, with the file paths and code it appears in:
- An entity depends on an adapter or framework — a model that serializes itself, a domain
type that imports React.
- A use case knows about delivery — a service returning HTTP status codes, a server action
returning JSX, a hook importing UI components.
- An adapter carries business logic — a controller computing totals, a serializer querying
the database, a route handler applying discounts.
- A layer is skipped — a screen or page calling the API client directly instead of going
through a hook.
Deep guides (read on demand, do not preload)
Read only the guide for the platform you are working in. Each is self-contained: it restates
the dependency rule, gives the full layer→directory mapping, the rules per component, the boundary
violations to detect with bad/good code, and how to test each layer. A Rails task must not load
the frontend guides; a frontend task must not load the Rails guide.
| Working in… |
Read |
Rails backend — models, services, controllers, Panko serializers, Sidekiq jobs (app/**/*.rb, lib/**/*.rb) |
references/rails-mapping.md |
React Native mobile app — screens, hooks, domain types, Zustand stores (mobile/src/**) |
references/react-native-mapping.md |
ReactJS Vite SPA — pages, hooks, router config, Zustand stores (web/src/**) |
references/reactjs-vite-mapping.md |
Next.js App Router — Server Components, server actions, route handlers (next/app/**, next/src/**) |
references/nextjs-app-router-mapping.md |
If a change spans platforms (for example, a feature touching both Rails and Next.js), read the two
relevant guides and no others.
1---2name: std-clean-architecture3description: Clean Architecture conventions — layer separation, dependency direction, boundary violations across Rails and frontends. Use when structuring services or reviewing architecture.4paths:5 - "**/app/**/*.rb"6 - "**/lib/**/*.rb"7 - "**/src/**/*.ts"8 - "**/src/**/*.tsx"9 - "**/app/**/*.ts"10 - "**/app/**/*.tsx"11---1213# Clean Architecture1415Enforce Clean Architecture principles across all application code. The core rule: dependencies16point inward. Outer layers depend on inner layers, never the reverse.1718## Layer Model1920```21┌──────────────────────────────────────┐22│ Frameworks & Drivers (outermost) │ Rails, React Native, PostgreSQL, Redis23├──────────────────────────────────────┤24│ Interface Adapters │ Controllers, Serializers, Presenters, Gateways25├──────────────────────────────────────┤26│ Use Cases (Application Logic) │ Service objects, Interactors, Commands27├──────────────────────────────────────┤28│ Entities (Domain Logic) (innermost) │ Models, Value Objects, Domain Rules29└──────────────────────────────────────┘30```3132## The Dependency Rule3334- **Entities** know nothing about use cases, controllers, or frameworks.35- **Use Cases** know about entities but not about controllers, serializers, or databases.36- **Interface Adapters** translate between use cases and external concerns.37- **Frameworks** are implementation details — pluggable and replaceable.3839## Testing by Layer4041- **Entities**: Unit tests, no mocks needed (pure domain logic).42- **Use Cases**: Unit tests with mocked repositories/gateways.43- **Interface Adapters**: Integration tests (controller specs, serializer specs).44- **Frameworks**: Minimal testing — trust the framework, test your configuration.4546## The Universal Violations4748Every platform expresses the same four mistakes. The per-platform guides below name each one49concretely, with the file paths and code it appears in:50511. **An entity depends on an adapter or framework** — a model that serializes itself, a domain52 type that imports React.532. **A use case knows about delivery** — a service returning HTTP status codes, a server action54 returning JSX, a hook importing UI components.553. **An adapter carries business logic** — a controller computing totals, a serializer querying56 the database, a route handler applying discounts.574. **A layer is skipped** — a screen or page calling the API client directly instead of going58 through a hook.5960## Deep guides (read on demand, do not preload)6162Read **only the guide for the platform you are working in**. Each is self-contained: it restates63the dependency rule, gives the full layer→directory mapping, the rules per component, the boundary64violations to detect with bad/good code, and how to test each layer. A Rails task must not load65the frontend guides; a frontend task must not load the Rails guide.6667| Working in… | Read |68|-------------|------|69| Rails backend — models, services, controllers, Panko serializers, Sidekiq jobs (`app/**/*.rb`, `lib/**/*.rb`) | `references/rails-mapping.md` |70| React Native mobile app — screens, hooks, domain types, Zustand stores (`mobile/src/**`) | `references/react-native-mapping.md` |71| ReactJS Vite SPA — pages, hooks, router config, Zustand stores (`web/src/**`) | `references/reactjs-vite-mapping.md` |72| Next.js App Router — Server Components, server actions, route handlers (`next/app/**`, `next/src/**`) | `references/nextjs-app-router-mapping.md` |7374If a change spans platforms (for example, a feature touching both Rails and Next.js), read the two75relevant guides and no others.