

Discover more from codewithmukesh
Feature Folder in Vertical Slice Architecture
How do you organize/structure features in Vertical Slice Architecture?
How do you organize/structure features in Vertical Slice Architecture?
📌 Firstly, what's Vertical Slice Architecture?
This is a popular topic around the #software community lately. It dictates that you design your system to be feature specific. This means that each feature will be isolated from the others, and will have separate read-and-write models. This also infuses the essence of CQRS with the Mediator pattern.
VSA will help the developer organize code by grouping it on a feature basis. For example, if your Catalog Microservice has a Product Entity, you can group every feature related to it into a single folder. It also helps in easier navigation. Ideally, feature codes should be present in the Application Project.
How I organize my features on a high level. (just focusing on the feature use case).
📁 Domain
📁 Application
📁 Products
📄 AddProduct.cs
📄 DeleteProduct.cs
📄 GetProductDetails.cs
📄 UpdateProduct.cs
📁 Infrastructure
📁 API
So, I like to put all that's needed for a feature into a single file with a proper naming convention. In this case, AddProduct.cs.
👉 Here, I usually add the Command, a record sent via my API using the MediatR package.
👉 Next, If it's a Command (Create, Update, or any write operation), I also throw in the validation logic here. I use FluentValidation.
👉 Finally, the Handler of the command/query also lives here. This is almost like my service layer, where I decide to use the required concerns like database, caches, and write in other business logic.
This way everything is simple to access and work with. This also goes nicely with Domain Driven approach.
It also ensures that each of the features has its own Request model. Some may argue that it leads to code duplication on the basis of the request record. But it always helps to keep your incoming request isolated for each feature. That's the whole idea of CQRS, right? Also, you are free to have separate response models as per your business needs.
Attaching a rough diagram with the controller and feature code.
📌 For a more in-depth understanding, check out my implementations on dotnet-microservices boilerplate: https://github.com/fullstackhero/dotnet-microservices-boilerplate/blob/main/fluentpos/services/catalog/Application/Products/Features/AddProduct.cs
How do you organize your features?
Let me know in the comments section if you want me to write an article where we build an entire application using the Vertical Slice Architecture approach covering other aspects too.