Problem
Horizontal layering spreads one feature across many folders, so every change touches presentation, service, and data layers and merge conflicts abound. Shared abstractions fit some features badly.
Also known as: feature-slices, slice-based-architecture
Organize code by feature or request so each slice contains everything from interface to data access.
Horizontal layering spreads one feature across many folders, so every change touches presentation, service, and data layers and merge conflicts abound. Shared abstractions fit some features badly.
Vertical slice architecture, popularized by Jimmy Bogard, flips the usual layout. Instead of grouping by technical role, it groups by feature. To change how an order is placed, you open one folder.
Each request is handled by a handler within its slice, often via a mediator.
features/
place-order/ PlaceOrder.cs (request, validator, handler, SQL)
cancel-order/ CancelOrder.cs
get-invoice/ GetInvoice.cs
shared/ only proven reusable code
export async function placeOrder(cmd: PlaceOrder, db: Db) {
validate(cmd)
const stock = await db.query("select qty from stock where sku = $1", [cmd.sku])
if (stock < cmd.qty) throw new OutOfStock()
await db.exec("insert into orders ...")
}
A simple read slice can query the database directly and return a DTO. A complex command slice can load an aggregate and invoke domain methods. Rules shared by several slices live in the domain model, not in a generic service layer.
None — code organization
Low
Low to medium
Slices calling each other and recreating hidden layers
Copy-pasted business rules diverging between slices
Shared folder growing into a new monolithic layer