REST API solution demonstrates how to create an API using Vertical Slice Architecture, minimal API and various design patterns.
The example API allows users to retrieve actual/forecast weather data by location from Weatherbit throught RapidAPI. Additionally, it allows users to add/delete favorite locations into an in memory database and then retrieve weather data by stored (favorite) locations.
- .NET SDK 9.0.x
To install the project using Git Bash:
- Clone the repository:
git clone https://github.com/Gramli/WeatherApi-VSA.git
- Navigate to the project directory:
cd WeatherApi-VSA/src
- Install the backend dependencies:
dotnet restore
- Register on RapidAPI
- Subscribe Weatherbit (its for free) and go to Endpoints tab
- In API documentation copy (from Code Snippet) X-RapidAPI-Key, X-RapidAPI-Host and put them to appsettings.json file in WeatherAPI project
"Weatherbit": {
"BaseUrl": "https://weatherbit-v1-mashape.p.rapidapi.com",
"XRapidAPIKey": "value from code snippet",
"XRapidAPIHost": "value from code snippet"
}
- Run Weather.API
- Go to Tests/Debug folder and open debug-tests.http file (in VS2022)
- Send request
The main motivation is to write a practical example of a simple API using Vertical Slice Architecture and then compare it to the same API designed by Clean Architecture.
Project folows Vertical Slice Architecture which instead of separation of technical concerns it encapsulating business logic of specific feature into vertical slice. So for example against Clean Architecture, one vertical slice is cross section of all layers like in picture below.
As this project involves a simple API, I decided to represent each vertical slice (or feature) as a CRUD operation with some additional business logic. Vertical slices shouldn't reference each other, so shared code is placed in the Domain folder. For example, the WeatherService, which acts as an adapter to the Weatherbit.Client project, or the DbContext, resides in the Domain.
Every feature (slice) folder contains a similar structure:
- request query or command - holds request parameters/data
- request handler - implementation of business logic
- specification holder - validation rules
- configuration - registering of classes into IoC container and minimal api endpoints.
Some slices also contains Dto objects, mapper profiles etc.., just all specific to the feature(slice).
I wrote a short blog post comparing Clean Architecture and Vertical Slice Architecture. The comparison is based on real pull requests, and the post also explores a hypothesis about a hybrid approach. Link to post