# Apply Vertical Slice Architecture

> Use when a backend's horizontal layers (controller → service → repository) force every small feature to touch many shared files, shared services grow into god c

- Skill: `jeffreytse/apply-vertical-slice-architecture` (Agent Skill)
- Install (CLI): `npx get-skill add jeffreytse/apply-vertical-slice-architecture`
- Raw SKILL.md: https://api.skillmd.com/api/skills/jeffreytse/apply-vertical-slice-architecture/raw
- Safety review: pending
- Works with: any agent that reads SKILL.md (Claude Code, Claude.ai, Cursor, Codex, Windsurf, 60+ more)
- Category: Coding & Dev Tools
- Author: jeffreytse (https://skillmd.com/u/jeffreytse)
- Updated: 2026-12-03
- Page: https://skillmd.com/skills/jeffreytse/apply-vertical-slice-architecture

---

---
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

1. **List use cases as commands and queries.** `CreateOrder`, `CancelOrder`, `GetOrderById`, `ListOrdersForCustomer`. Each one becomes a slice.

2. **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)
   ```

3. **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.

4. **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.

5. **Handle cross-cutting concerns with a pipeline,** not base classes: middleware or decorators for logging, validation, transactions, and authorization (MediatR behaviors, HTTP middleware, decorators).

6. **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.

7. **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.

