Vertical Slice
Vertical Slice
A vertical slice is one feature packaged with all the layers it needs: domain logic, application orchestration, infrastructure adapters, and presentation surface. One slice is the smallest unit of feature-level change.
What's inside one slice
order/
domain/
order.py # Aggregate root + entities + VOs
repository.py # Protocol
events.py # Domain events
exceptions.py
application/
use_cases/
place_order.py
cancel_order.py
commands.py # Input DTOs
infrastructure/
sql_repository.py # OrderRepository impl
publishers.py # Event publishers
presentation/
api/
router.py
schemas.py
dependencies.py
Four layers, all inside one folder. The dependency rule:
presentation -> application -> domain
infrastructure -> application -> domain
Domain knows about nothing outside itself. Outer layers know about inner.
Add a use case
Adding "cancel order" means:
- New event in
domain/events.py:OrderCancelled. - New method on
Order:cancel(). - New command in
application/commands.py:CancelOrderCommand. - New use case in
application/use_cases/cancel_order.py. - New endpoint in
presentation/api/router.py.
All in one slice folder. No other slice changes.
Add a new persistence backend
OrderRepository has a Protocol in domain/. To add MongoDB persistence:
- New file
infrastructure/mongo_repository.pyimplementing the Protocol. - Wire it in the composition root.
No domain, application, or presentation code changes.
What stays out of a slice
- True cross-cutting infrastructure: logging, HTTP framework setup, DB connection pool.
- Shared kernel: types used across many slices (e.g.,
Money,UserId). Keep this small and frozen.
Slice size
A slice corresponds to a business capability — usually a bounded context, sometimes a sub-context within one. Rough heuristic: if two features share a model with invariants between them, they're probably in the same slice.
Anti-patterns
- Layered slice. Slice folder with
entities/,services/,repositories/. That's just layered architecture nested one level deeper. The point is the vertical, not the folder name. - Leaking across slices. Slice A imports Slice B's domain types. Either move the type to shared kernel (deliberate), or talk via events / IDs (decoupled).
- God slice. One slice with 40 use cases. Probably 3 bounded contexts collapsed into one.
Why the structure matters
When a feature is one folder, deleting a feature is one rm -rf. When a feature is "everything called Order across 4 layer folders", deleting it is archaeology.