Architecture ‐ Vertical Slicing Architecture - FullstackCodingGuy/Developer-Fundamentals GitHub Wiki

Vertical Slicing Architecture

Vertical slice architecture is an approach for organising your code into features/vertical slices rather than organising by technical conerns (e.g. Controllers, Models, Services etc). Each slice will contain all the code which fullfills a feature or use case inside the application. One of the main benefits of VSA is the ability to structure each feature/slice independently, so each feature/slice can be as simple or complicated as it needs to be.

Example

image

Both Onion/Clean Architecture and Vertical Slicing Architecture aim to structure applications for better maintainability, scalability, and testability, but they take different approaches in organizing the codebase. Here's a breakdown of their differences:


🧅 Onion / Clean Architecture

Layered Architecture
Focuses on Separation of Concerns
Domain-Centric (Business Logic First)
Code is Organized by Layer (API, Application, Domain, Infrastructure)

📌 Structure of Onion/Clean Architecture

/TaskManager
│── /API                 --> Presentation Layer (Controllers, Endpoints)
│── /Application         --> Business Logic (Use Cases, DTOs, Services)
│── /Domain             --> Core Business Rules (Entities, Aggregates)
│── /Infrastructure     --> Persistence, External APIs, Logging

🔹 How It Works

  • The Domain is at the center (contains entities and business rules).
  • The Application Layer contains use cases, DTOs, and services.
  • The Infrastructure Layer includes database, logging, and external integrations.
  • The API Layer is just an interface for external consumers (Controllers/Minimal APIs).
  • Dependencies flow inwards (API → Application → Domain).

🟢 Pros

✔️ Strong separation of concerns and modular structure.
✔️ Testability (business logic is independent of frameworks).
✔️ Scalability (supports different UIs like Web, Mobile, or gRPC).

🔴 Cons

❌ Can be over-engineered for simple projects.
❌ Slower to develop due to multiple layers.


📌 Vertical Slicing Architecture

Feature-Based Organization
Each Feature is Independent
Reduces Unnecessary Abstraction
Follows CQRS and Mediator Patterns

📌 Structure of Vertical Slicing

/TaskManager
│── /Features
│    ├── /Tasks
│    │    ├── /Create
│    │    │    ├── CreateTaskHandler.cs
│    │    │    ├── CreateTaskRequest.cs
│    │    │    ├── CreateTaskResponse.cs
│    │    ├── /GetAll
│    │    │    ├── GetAllTasksHandler.cs
│    │    │    ├── GetAllTasksRequest.cs
│    │    │    ├── GetAllTasksResponse.cs
│── /Infrastructure      --> Shared Infrastructure (DB, Logging)
│── /API                 --> Entry Point (Minimal API, Controllers)

🔹 How It Works

  • Each feature (e.g., Tasks) is self-contained with its own:
    • Handler (Processing logic)
    • Request DTO
    • Response DTO
  • Uses CQRS (Command Query Responsibility Segregation) and MediatR for clean request handling.
  • No unnecessary abstraction (like repositories or service layers).

🟢 Pros

✔️ Faster Development (each feature is independent).
✔️ Avoids unneeded complexity (no forced service/repository layers).
✔️ Easier to scale individual features.
✔️ Better for Microservices (as features are isolated).

🔴 Cons

❌ Can lead to duplicate logic (each feature implements its own handling).
❌ Harder to maintain cross-cutting concerns (logging, caching, etc.).


📌 When to Use What?

Criteria Onion/Clean Architecture Vertical Slicing Architecture
Best for Large, Enterprise Applications Small to Medium Projects, Microservices
Focuses on Business Logic and Separation of Concerns Features and Quick Development
Complexity Higher (More Layers) Lower (Flat Structure)
Maintainability Easier for Long-Term Easier for Short-Term
Cross-Cutting Concerns Centralized Handled per Feature
Scaling Approach Modular and Layered Independent Features

🔥 Summary

Onion/Clean Architecture → Better for long-term projects, domain-driven design, and testability.
Vertical Slicing → Better for microservices, fast iteration, and reducing boilerplate.

⚠️ **GitHub.com Fallback** ⚠️