Vertical Slice Architecture Example in .NET 8
Introduction
The Vertical Slice Architecture project is a fascinating initiative that aims to create a more effective way of organizing API solutions using .NET 8. This approach centers around structuring code by features, rather than traditional technical layers, making it easier to manage and evolve applications as they grow more complex over time.
What is Vertical Slice Architecture?
Vertical Slice Architecture is a style of organizing code where each feature of the application stands as a separate "slice," containing all the necessary components such as business logic, data access, and user interface elements. This differs from the conventional method of dividing code into technical concerns like data access, business logic, and user interface, known as horizontal layering.
This architectural style encourages developers to focus on the functionality of individual features, allowing all related code to be housed together. This intuitive organization simplifies changes and enhancements to specific features, as everything needed is nearby.
Why Use Vertical Slice Architecture?
Applications typically start small and grow more complex as new features are added. With a vertical slice approach, developers can readily understand and update application features without the overhead of navigating multiple layers and files. This methodology reduces complexity by emphasizing feature-oriented structures over abstract layering, thus making development more straightforward and aligned with business objectives.
Project Structure
The solution is divided into two main projects:
API
The API project serves as the entry point for the application. Instead of housing controller logic directly, it delegates actions to the related feature files in the Application project.
Application
All application logic resides within this project. It includes everything from domain entities to shared infrastructural components. Each feature has its designated folder, consolidating all its elements into one place—commands, queries, validations, handlers, and models.
How to Get Started
To get started with the Vertical Slice Architecture project, follow these steps:
- Install the latest .NET 8 SDK.
- Navigate to the
src/Api
directory and executedotnet run
to start the ASP.NET Core Web API.
Building and Testing
To build, test, and publish the application, utilize the following CLI commands from the root directory:
- Build the application:
dotnet build
- Run the project:
dotnet run --project src/Api/Api.csproj
- Execute unit tests:
dotnet test tests/Application.UnitTests/Application.UnitTests.csproj
- Perform integration tests:
dotnet test tests/Application.IntegrationTests/Application.IntegrationTests.csproj
- Publish the application:
dotnet publish src/Api/Api.csproj --configuration Release
Running Locally
Run the API locally for debugging using an IDE like Visual Studio, Rider, or VS Code. The project uses an in-memory database by default, making setup easy. For SQL Server use, adjust configurations in Api/appsettings.json
.
Database Configuration
By default, this project uses an in-memory database. To switch to SQL Server, set the "UseInMemoryDatabase" to false in appsettings.json
and ensure the "DefaultConnection" string is valid.
Database Migrations
To manage database migrations, the dotnet-ef
tool is used with specific flags for project paths. These commands streamline adding new migrations and updating databases.
Code Analysis
Maintain code consistency by adhering to Microsoft's C# Coding Conventions, enforced through an .editorconfig file. Developers can format the code using the provided instructions within an IDE or via CLI commands such as dotnet format
.
Inspirations
This project takes inspiration from several sources, including Jason Taylor's Clean Architecture solution and Jimmy Bogard's work on vertical slice architecture.
License
This project is open-source and licensed under the MIT license.
The Vertical Slice Architecture project in .NET 8 embodies a structured yet flexible approach, revolutionizing how developers can build scalable, maintainable applications.