Vertical Slice
Carpetas por feature
Escrito por Darjus Kazakevic · revisado el
Name the folders after features, so a change stays in one folder.
Layered, Onion and Clean all split code by technical job. A folder is “controllers” or “repositories”, and one feature is spread across all of them. Vertical Slice splits by feature instead. The route, the rule and the query for “place order” live in one folder.
The layers do not disappear. They live inside each slice, sized for that feature. The bet is that features change independently and technical jobs do not. So coupling inside a slice is fine, and coupling between slices is what you avoid. The cost is some duplicated code.
En esencia — Couple code tightly inside a feature and loosely between features, so each slice picks its own depth and only real domain rules are shared.
Cuándo recurrir a él
Recurre a él cuando
- Work arrives one feature at a time, as in most product teams
- A routine change already touches four or five folders
- Features differ in depth: some are CRUD, some have rich rules
Reconsidéralo cuando
- Rules cut across most features and nobody owns the shared kernel
- The app is small: three thin layers, and changes stay in one place
- The natural unit is technical, as in a library, a driver or a compiler
Anatomy of a slice
- The slice (a feature folder)
- One feature and everything it needs: the route, the rule, the query, and the request and response shapes. It is the unit you open, review and delete.
- The shared kernel (shared/)
- The few things that are true of the domain, not of one feature: what a valid order is, how money rounds, who may refund. Every slice uses them; nothing else is shared.
- Coupling, reconsidered (the bet)
- Low coupling matters between features. Inside one slice, tight coupling costs little, because everything that could break together already changes together.
- Slice depth (ceremony per feature)
- Each slice chooses how much structure it needs. Most are transaction scripts; the one with real rules grows a model and a repository. A layer cannot do this, because it applies to every feature.
Slicing well
- One feature, one folder
- Slice by behaviour, not by endpoint. “Place an order” is one slice even if it has two routes. One folder per HTTP verb spreads the feature across folders again.
- Duplicate first, extract on the third
- Two slices with similar code are not proof of a shared concept. Wait for a third copy, and until you can name the shared thing in domain terms.
- Share domain rules, copy the machinery
- Rules, money and identity go in the kernel. Handlers, DTOs and a feature's own queries stay in the slice, even when two look the same, because they will drift apart.
- Give cross-cutting concerns one home
- Auth, transactions, logging and validation no longer have a layer. Put them in a request pipeline (middleware) that every route passes through.
Errores habituales
- Reading “slices share nothing” as the rule. Bogard never said it, and a domain rule belongs in the kernel.
- Moving the first duplicate into shared/, until shared/ is a service layer and the slices are layers again.
- Slicing by endpoint instead of by behaviour. One feature is then spread across several folders again, one per route.
- Feature folders over unchanged layered code: the files move, the coupling stays.