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:

  1. New event in domain/events.py: OrderCancelled.
  2. New method on Order: cancel().
  3. New command in application/commands.py: CancelOrderCommand.
  4. New use case in application/use_cases/cancel_order.py.
  5. 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:

  1. New file infrastructure/mongo_repository.py implementing the Protocol.
  2. Wire it in the composition root.

No domain, application, or presentation code changes.

What stays out of a slice

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

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.