Vertical Slice Architecture (also called Feature-First Structure) organizes code by features or use cases rather than technical layers.
Core Concept
Instead of organizing by technical concerns (Controllers, Services, Repositories), organize by features or slices that represent complete user workflows.
Traditional Layered vs. Vertical Slice
Traditional Layered
src/
├─ controllers/
│ ├─ UserController.cs
│ └─ OrderController.cs
├─ services/
│ ├─ UserService.cs
│ └─ OrderService.cs
└─ repositories/
├─ UserRepository.cs
└─ OrderRepository.cs
Vertical Slice
src/
├─ features/
│ ├─ users/
│ │ ├─ CreateUser/
│ │ │ ├─ CreateUserRequest.cs
│ │ │ ├─ CreateUserHandler.cs
│ │ │ └─ CreateUserValidator.cs
│ │ └─ GetUser/
│ │ ├─ GetUserRequest.cs
│ │ └─ GetUserHandler.cs
│ └─ orders/
│ └─ PlaceOrder/
│ ├─ PlaceOrderRequest.cs
│ └─ PlaceOrderHandler.cs
Benefits
Cohesion
- Related code lives together
- Easy to find all code for a feature
- Reduces cognitive load
Independent Evolution
- Features can evolve independently
- Different features can use different patterns
- No forced consistency across unrelated features
Team Scaling
- Teams can own entire features
- Reduces merge conflicts
- Clear boundaries for parallel work
Deletion
- Removing a feature means deleting a folder
- No scattered code across layers
Principles
CQRS-Friendly
- Commands and queries are separate slices
- Each slice optimized for its specific needs
- No forced abstraction for different operations
Minimal Coupling
- Slices are independent
- Shared code extracted only when truly reusable
- Duplication preferred over wrong abstraction
Feature-Driven
- Aligns with how users think about the application
- Maps to user stories and use cases
- Business-focused organization
Tradeoffs
Advantages:
- High cohesion within features
- Easy to understand and navigate
- Flexible - different slices can use different approaches
Disadvantages:
- May lead to some code duplication
- Shared infrastructure needs careful management
- Less enforcement of consistency
Resources
- Vertical Slice Architecture
- Vertical Slice Architecture - Jimmy Bogard (Video)
- Vertical Slice Architecture Project Setup From Scratch
Related Concepts
- Clean Architecture
- Domain Driven Design
- Feature Folders
- Screaming Architecture