At its core, engineering is the art of decomposing big problems into smaller ones so that are easier to be understood, communicated and tackled.
The traditional n-tier approach to building a feature is to break it down into several layers. By building it as such there is high risk of over-engineering each layer individually to ensure that the next dependable layer has everything it needs. This way, there is high potential for introducing non-necessary complexity.
By slicing through the layers, and building features in “vertical slices” the complexity gets as big as the individual feature needs it to be, isolated from other parts of the system.
In this session I’ll explain and demo how to simplify a horizontal-layered system towards vertical-sliced one, by employing principles and practices such as SRP and CQS.
3. User Intent - Reads and Writes
- WRITE is a request modeled as COMMAND triggered via HTTP POST
- READ is a request modeled as QUERY triggered via HTTP GET
- Commands modify data
- Queries retrieve data
3
4. User Intent - CQRS
CQRS is a simple pattern,
writing and reading data is placed
in 2 separate classes which have single responsibility.
4
6. What we are going to talk about
- Overview
- Horizontal vs. Vertical user stories
- Horizontal vs. Vertical slices
- Commands & Queries
- Feature folders
- DEMO code
- Summary
6
8. Horizontal vs. Vertical user stories
- Horizontal
- Story 1: Create the new DB Table for Users
- Story 2: Create the DAL to access the DB Users
- Story 3: Create the Users business layer code that references the DAL
- Story 4: Write the Users UI screen
- Vertical
- Story 1: User can register and provide username and password.
- Story 2: User can manage multiple addresses.
- Story 3: User can add additional contact fields (phone, email, etc).
- Story 4: User can subscribe to a subscription plan.
8
9. Horizontal vs. Vertical user stories
- Horizontal
- Implement the database layer for A, B and C
- Implement the business logic layer for A, B and C
- Implement the user interface for A, B and C
- Vertical
- Implement A from end to end
- Implement B from end to end
- Implement C from end to end 9
10. Horizontal slicing is not natural in agile
- Changes usually happen vertically
- You limit your application to the same horizontal
constraints for all features (regardless of the size and
complexity)
- ...and more
10
12. Vertical Slices over Horizontal Slices
- Horizontal slices is organization by technical layers.
- Vertical slices is organization by features (business requirements).
12
13. Vertical Slicing: The Stakeholder
Stakeholders talk and eat the cake in vertical slices.
They want one delicious piece at a time, not only the vanila part.
Why don't we structure our code vertically as well?
13
27. SUMMARY: Characteristics of good Vertical Slices
- Code is structured primarily by feature, secondary by layer
- Have single responsibility (business-oriented)
- Encapsulated and decoupled from other slices
- Complexity and defects are isolated in each slice, instead of
each layer
- Increases developer’s understanding of each feature as a
whole
- Enables incremental releases of features
- Enables faster feedback loop 27
28. SUMMARY: Trade-offs and Challenges
- Complete mindset shift
- Developers must be all-round-shaped (back, front, middle, db, all)
- Now you have twice as many classes as before
28