Clean Architecture
References: Examples
Dependency Rule
Dependencies point inward. Outer layers depend on inner, never reverse. Avoid circular dependencies.
| Layer | Contains | Must NOT |
|---|---|---|
| Usecase | Entities, business logic, interfaces, repository | Import transport or apps |
| Driving Adapters (transport) | HTTP handlers, consumers | Contain business logic |
| Driven Adapters (clients) | External API implementations | Import transport or usecase |
Interface Ownership
- •Interfaces defined where used, not where implemented
- •Each usecase defines only methods it needs (Interface Segregation)
- •Interfaces are unexported and private to package
- •No
ports/,interfaces/, orcontracts/packages
Folder Structure
| Location | Contains |
|---|---|
cmd/ | Lambda/CLI entrypoints |
config/appconfig/ | Environment configurations |
infra/ | CDK stack + constructs |
internal/<domain>/ | Self-contained bounded contexts (usecase/, repository/, model/) |
internal/clients/ | External API clients + models |
internal/transport/ | HTTP handlers, Kinesis consumers |
internal/apps/ | Dependency wiring |
Package Boundaries
- •
internal/- Private to service - •
pkg/- Shared utilities for other repos
Domain Structure
Each domain in internal/<domain>/ follows:
code
<domain>/
├── input.go # Input DTOs
├── output.go # Output DTOs
├── usecase/
│ ├── service.go # Business logic + interfaces
│ └── service_mock.go
├── repository/
│ └── repository.go
└── model/
└── entity.go # Domain entities