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:
- Code dispersion throughout the codebase
- Tight coupling between features
- Unnecessary complexity
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:
- Domain objects can be grouped together in a folder or can be placed in the feature folder.
- The grouping of controllers and repositories can be tailored to match functional distinctions within the layer.
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
- Clean Architecture - Amazon
- Hexagonal Architecture - Wikipedia
- Package Principles - Wikipedia
- Vertical Slice - Wikipedia
- Clean Architecture is About Vertical Slices - YouTube