name: apply-vertical-slice-architecture
description: Use when a backend's horizontal layers (controller → service → repository) force every small feature to touch many shared files, shared services grow into god classes, or abstractions exist only to satisfy the layering — and you want each request or use case implemented as a self-contained slice.
source: 'Bogard, "Vertical Slice Architecture", jimmybogard.com, 2018; Bogard, "Vertical Slice Architecture" talk, NDC Sydney 2018; Meyer, "Object-Oriented Software Construction" (Command-Query Separation), 1988; Young, "CQRS Documents", 2010'
tags: [project-structure, vertical-slices, cqrs, backend, feature-isolation, maintainability]
related: [apply-package-by-feature, design-cqrs-architecture, apply-hexagonal-architecture, apply-yagni-principle, design-backend-project-structure]
Apply Vertical Slice Architecture
Organize a backend around requests (commands and queries). Each slice holds everything one use case needs, from endpoint to data access, so you add features by adding slices, not by editing shared layers.
Why This Is Best Practice
Adopted by:
- Popularized by Jimmy Bogard (author of MediatR and AutoMapper) and widely used in the .NET ecosystem (MediatR, FastEndpoints, Wolverine).
- Similar ideas: Rails/Laravel "single-action controllers", the "use case per file" style in Node/Go, and CQRS handler-per-command designs.
Impact:
- Change locality: a new feature adds a new folder or file rather than modifying shared service and repository classes, which lowers merge conflicts and regression risk from edits to shared code.
- Each slice can choose the simplest data access that fits it (raw SQL for a report, ORM for a write), avoiding one-size-fits-all repository abstractions.
Why best:
- Layered architectures couple along the wrong axis. Features change independently, but layers force them to share files.
- Slices maximize cohesion within a feature and minimize coupling between features. Shared code is pulled out only when duplication is proven.
Sources: Bogard, J. (2018), "Vertical Slice Architecture" (jimmybogard.com). Meyer, B. (1988), Object-Oriented Software Construction. Young, G. (2010), "CQRS Documents".
Steps
List use cases as commands and queries. CreateOrder, CancelOrder, GetOrderById, ListOrdersForCustomer. Each one becomes a slice.
Give each slice its own file or folder, grouped by feature:
features/
orders/
create-order/
create-order.endpoint.ts
create-order.handler.ts
create-order.validator.ts
create-order.test.ts
get-order-by-id/
get-order-by-id.ts # small slices can be one file
customers/
...
domain/ # shared entities/value objects with real business rules
infrastructure/ # db context, messaging, auth (cross-cutting only)
Implement each slice end to end: request/response types, validation, handler logic, data access, and mapping. Choose the simplest approach per slice. A query may read straight from the database into a DTO.
Push real business rules into the domain model. When several slices enforce the same invariant ("an order can't be cancelled after shipping"), put it on the entity, not copied into each handler.
Handle cross-cutting concerns with a pipeline, not base classes: middleware or decorators for logging, validation, transactions, and authorization (MediatR behaviors, HTTP middleware, decorators).
Refactor only when duplication is proven. Leave small duplication between slices alone. Extract shared code after the third repetition (rule of three), and only if it represents the same concept.
Test each slice through its handler or endpoint with a real or containerized database. Slices are the natural unit for integration tests.
Rules
- One use case per slice. Slices don't call other slices' handlers directly. Share via the domain model or events.
- New features add code. They should rarely modify existing slices.
- No speculative shared abstractions (generic repositories, base services).
- Business invariants live in the domain, not duplicated across handlers.
Common Mistakes
- Transaction scripts everywhere. All logic lives in handlers, the domain stays anemic, and invariants are duplicated and drift apart.
- Slices calling slices.
CreateOrderHandler invokes ReserveStockHandler directly, recreating hidden coupling. Use a domain service or an event.
- Rebuilding layers inside slices. Every slice gets its own controller, service, and repository trio with nothing in it. Keep slices as thin as the use case allows.
- No conventions. Every slice is structured differently. Agree on a template (file names, validation, error handling).
Examples
Admin reporting: GetRevenueByMonth uses a hand-tuned SQL query returning a DTO, while UpdateProductPrice uses the ORM and domain entity. Neither forces the other into a shared repository.
.NET API with MediatR: each feature folder holds Command, Handler, Validator, and Endpoint. Validation and transactions run as pipeline behaviors.
When NOT to Use
- Rich, complex domains with heavily shared invariants. Combine slices with a strong domain model or hexagonal core (see
apply-hexagonal-architecture) rather than pure slices.
- Very small apps where a single file per resource is simpler.
- Teams that need strict layering for compliance or audit reasons. Document the trade-off before departing from it.
1---2name: apply-vertical-slice-architecture3description: Use when a backend's horizontal layers (controller → service → repository) force every small feature to touch many shared files, shared services grow into god c4---5---6name: apply-vertical-slice-architecture7description: Use when a backend's horizontal layers (controller → service → repository) force every small feature to touch many shared files, shared services grow into god classes, or abstractions exist only to satisfy the layering — and you want each request or use case implemented as a self-contained slice.8source: 'Bogard, "Vertical Slice Architecture", jimmybogard.com, 2018; Bogard, "Vertical Slice Architecture" talk, NDC Sydney 2018; Meyer, "Object-Oriented Software Construction" (Command-Query Separation), 1988; Young, "CQRS Documents", 2010'9tags: [project-structure, vertical-slices, cqrs, backend, feature-isolation, maintainability]10related: [apply-package-by-feature, design-cqrs-architecture, apply-hexagonal-architecture, apply-yagni-principle, design-backend-project-structure]11---1213# Apply Vertical Slice Architecture1415Organize a backend around requests (commands and queries). Each slice holds everything one use case needs, from endpoint to data access, so you add features by adding slices, not by editing shared layers.1617## Why This Is Best Practice1819**Adopted by:**20- Popularized by Jimmy Bogard (author of MediatR and AutoMapper) and widely used in the .NET ecosystem (MediatR, FastEndpoints, Wolverine).21- Similar ideas: Rails/Laravel "single-action controllers", the "use case per file" style in Node/Go, and CQRS handler-per-command designs.2223**Impact:**24- Change locality: a new feature adds a new folder or file rather than modifying shared service and repository classes, which lowers merge conflicts and regression risk from edits to shared code.25- Each slice can choose the simplest data access that fits it (raw SQL for a report, ORM for a write), avoiding one-size-fits-all repository abstractions.2627**Why best:**28- Layered architectures couple along the *wrong* axis. Features change independently, but layers force them to share files.29- Slices maximize cohesion within a feature and minimize coupling between features. Shared code is pulled out only when duplication is proven.3031Sources: Bogard, J. (2018), "Vertical Slice Architecture" (jimmybogard.com). Meyer, B. (1988), *Object-Oriented Software Construction*. Young, G. (2010), "CQRS Documents".3233## Steps34351. **List use cases as commands and queries.** `CreateOrder`, `CancelOrder`, `GetOrderById`, `ListOrdersForCustomer`. Each one becomes a slice.36372. **Give each slice its own file or folder, grouped by feature:**38 ```39 features/40 orders/41 create-order/42 create-order.endpoint.ts43 create-order.handler.ts44 create-order.validator.ts45 create-order.test.ts46 get-order-by-id/47 get-order-by-id.ts # small slices can be one file48 customers/49 ...50 domain/ # shared entities/value objects with real business rules51 infrastructure/ # db context, messaging, auth (cross-cutting only)52 ```53543. **Implement each slice end to end:** request/response types, validation, handler logic, data access, and mapping. Choose the simplest approach per slice. A query may read straight from the database into a DTO.55564. **Push real business rules into the domain model.** When several slices enforce the same invariant ("an order can't be cancelled after shipping"), put it on the entity, not copied into each handler.57585. **Handle cross-cutting concerns with a pipeline,** not base classes: middleware or decorators for logging, validation, transactions, and authorization (MediatR behaviors, HTTP middleware, decorators).59606. **Refactor only when duplication is proven.** Leave small duplication between slices alone. Extract shared code after the third repetition (rule of three), and only if it represents the same concept.61627. **Test each slice through its handler or endpoint** with a real or containerized database. Slices are the natural unit for integration tests.6364## Rules6566- One use case per slice. Slices don't call other slices' handlers directly. Share via the domain model or events.67- New features add code. They should rarely modify existing slices.68- No speculative shared abstractions (generic repositories, base services).69- Business invariants live in the domain, not duplicated across handlers.7071## Common Mistakes7273- **Transaction scripts everywhere.** All logic lives in handlers, the domain stays anemic, and invariants are duplicated and drift apart.74- **Slices calling slices.** `CreateOrderHandler` invokes `ReserveStockHandler` directly, recreating hidden coupling. Use a domain service or an event.75- **Rebuilding layers inside slices.** Every slice gets its own controller, service, and repository trio with nothing in it. Keep slices as thin as the use case allows.76- **No conventions.** Every slice is structured differently. Agree on a template (file names, validation, error handling).7778## Examples7980**Admin reporting:** `GetRevenueByMonth` uses a hand-tuned SQL query returning a DTO, while `UpdateProductPrice` uses the ORM and domain entity. Neither forces the other into a shared repository.8182**.NET API with MediatR:** each feature folder holds `Command`, `Handler`, `Validator`, and `Endpoint`. Validation and transactions run as pipeline behaviors.8384## When NOT to Use8586- **Rich, complex domains with heavily shared invariants.** Combine slices with a strong domain model or hexagonal core (see `apply-hexagonal-architecture`) rather than pure slices.87- **Very small apps** where a single file per resource is simpler.88- **Teams that need strict layering for compliance or audit reasons.** Document the trade-off before departing from it.