Table of Contents

Clean Vertical Slice Architecture

When executed effectively, Clean Architecture and Vertical Slice Architecture exhibit striking similarities. Debates often arise due to misunderstandings rather than inherent flaws in the concepts.

Critiques of Clean Architecture:

Are these criticisms truly of Clean Architecture or rather a consequence of poor package cohesion strategies?

Clean Architecture

Bad Clean Architecture

Many implementation and project templates tend to categorize classes by type, resembling the familiar structure of MVC project templates.

MySolution.sln
  πŸ“‚ MyCompany.MyProject.Api
    πŸ“‚ Controllers
      πŸ“„ AccountController.cs
      πŸ“„ OrderController.cs
  πŸ“‚ MyCompany.MyProject.Application
    πŸ“‚ Interfaces
      πŸ“„ IAccountRepository.cs
      πŸ“„ IAccountService.cs
      πŸ“„ IOrderRepository.cs
      πŸ“„ IOrderService.cs
    πŸ“‚ Services
      πŸ“„ AccountService.cs
      πŸ“„ OrderService.cs
    πŸ“‚ Validators
      πŸ“„ AccountValidator.cs
      πŸ“„ OrderValidator.cs
  πŸ“‚ MyCompany.MyProject.Domain
    πŸ“„ Account.cs
    πŸ“„ Order.cs
  πŸ“‚ MyCompany.MyProject.Infrastructure
    πŸ“„ AccountRepository.cs
    πŸ“„ OrderRepository.cs

Navigating through different folders to modify the Order feature is cumbersome. This simplistic organizational approach, while initially intuitive, fails to scale effectively. It’s akin to storing all socks in one drawerβ€”suitable for a single individual but impractical for a larger household or complex application.

I’ll bet you separate your socks from your partners socks, and each of your children has the socks stored in his or her own room.

Better clean architecture

MySolution.sln
  πŸ“‚ MyCompany.MyProject.Api
    πŸ“‚ Controllers
      πŸ“„ AccountController.cs
      πŸ“„ OrderController.cs
  πŸ“‚ MyCompany.MyProject.Application
    πŸ“‚ Accounts
      πŸ“„ Account.cs
      πŸ“„ AccountService.cs
      πŸ“„ AccountValidator.cs
      πŸ“„ IAccountRepository.cs  
      πŸ“„ IAccountService.cs
    πŸ“‚ Orders
      πŸ“„ IOrderRepository.cs
      πŸ“„ IOrderService.cs
      πŸ“„ Order.cs
      πŸ“„ OrderService.cs
      πŸ“„ OrderValidator.cs  
  πŸ“‚ MyCompany.MyProject.Infrastructure
    πŸ“„ AccountRepository.cs
    πŸ“„ OrderRepository.cs

Certain details warrant discussion and may vary based on your application’s specifics. For instance:

Vertical Slice Architecture naturally emerges from a well designed Clean Architecture.

Vertical Slice Architecture

What about integrating AccountRepository.cs into the Accounts folder? Certainly. How about the AccountController.cs? Without altering the architecture, everything can be grouped together.

Extreme Vertical Slice Architecture

MySolution.sln   
  πŸ“‚ MyCompany.MyProject.Api
    πŸ“‚ Accounts
      πŸ“„ Account.cs
      πŸ“„ AccountController.cs
      πŸ“„ AccountRepository.cs
      πŸ“„ AccountService.cs
      πŸ“„ AccountValidator.cs
      πŸ“„ IAccountRepository.cs  
      πŸ“„ IAccountService.cs
    πŸ“‚ Orders
      πŸ“„ IOrderRepository.cs
      πŸ“„ IOrderService.cs
      πŸ“„ Order.cs
      πŸ“„ OrderController.cs
      πŸ“„ OrderRepository.cs
      πŸ“„ OrderService.cs
      πŸ“„ OrderValidator.cs   

The most important thing is to push dependencies as far to the edge of your application as possible. Preventing web-related elements from seeping into services or database concerns from contaminating validators is paramount.

However, some Vertical Slice Architecture implementations falter when package cohesion strategies fail to discourage code misplacement. External package dependencies like ORMs and web frameworks may inadvertently infiltrate core application logic, leading to the coupling of business logic to implementation details.

Vertical Slice Clean Architecture

Employing a folder structure can help clarify these distinctions, gradually resembling Clean Architecture once again.

MySolution.sln   
  πŸ“‚ MyCompany.MyProject.Api
    πŸ“‚ Accounts
      πŸ“‚ Presentation
        πŸ“„ AccountController.cs
      πŸ“‚ Core 
        πŸ“„ Account.cs      
        πŸ“„ AccountService.cs
        πŸ“„ AccountValidator.cs
        πŸ“„ IAccountRepository.cs  
        πŸ“„ IAccountService.cs
      πŸ“‚ Infrastructure
        πŸ“„ AccountRepository.cs

Separate projects are preferable to prevent the use of external packages within the application’s core.

Clean vs Vertical Slice Architectures

When you take a closer look, Clean Architecture and Vertical Slice Architecture start to look like two peas in a pod. They both push for modular, flexible designs that are easy to maintain and scale. The small differences mostly come down to how they’re put into practice, rather than any big disagreements. Get them right, and you end up with software that’s easy to work with, adaptable, and reliable.

References

Β© 2024 Rob van der Velden. All rights reserved.