Skip to content

An example REST API for managing weather data using Minimal API and Vertical Slice Architecture, with various design patterns. Built with ASP.NET Core 9.0.

License

Notifications You must be signed in to change notification settings

Gramli/WeatherApi-VSA

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vertical Slice Architecture WeatherApi

.NET Build and Test Codacy Badge

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.

Menu

Prerequisites

  • .NET SDK 9.0.x

Installation

To install the project using Git Bash:

  1. Clone the repository:
    git clone https://github.com/Gramli/WeatherApi-VSA.git
  2. Navigate to the project directory:
    cd WeatherApi-VSA/src
  3. Install the backend dependencies:
    dotnet restore

Get Started

  1. Register on RapidAPI
  2. Subscribe Weatherbit (its for free) and go to Endpoints tab
  3. 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"
  }
  1. Run Weather.API

Try it using .http file (VS2022)

  • Go to Tests/Debug folder and open debug-tests.http file (in VS2022)
  • Send request

Motivation

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.

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.

Project Vertical Slice Architecture Diagram

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).

Comparsion to Clean Architecture

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

Technologies

About

An example REST API for managing weather data using Minimal API and Vertical Slice Architecture, with various design patterns. Built with ASP.NET Core 9.0.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published