Skip to content

maximemader/hexagonal-architecture-dotnet

Repository files navigation

🌦️ Hexagonal Architecture sample with .NET

This repository demonstrates three progressively more structured implementations of a weather API using .NET — from a basic monolith to a clean hexagonal architecture with multiple bounded contexts.

📁 Projects Overview

1. SampleWeatherApp

⚙️ Based on the official Microsoft weather API template (dotnet new webapi) with slight modifications.

  • Exposes a basic /weatherforecast endpoint
  • Uses random temperature generation
  • Lacks architectural layering or separation of concerns
  • Serves as a baseline for comparison

2. HexagonalWeatherApp

🛠 Implements hexagonal architecture (a.k.a. Ports & Adapters)

🔍 Core Features

  • Domain-focused WeatherService handles requests
  • Uses IWeatherProvider interface to decouple the weather data source
  • Domain entity: WeatherForecast (city, temperature)
  • Adapters:
    • RandomWeatherForecastAdapter: returns random temps
    • WeatherApiForecastAdapter: fetches real data (e.g. from WeatherApi)
  • Exposed via REST API (app.MapGet("/weatherforecast/{city}")

🔌 Interactions

  • API controller calls the domain service
  • Service calls injected ports:
    • Weather provider

✅ Benefits

  • Testable and modular
  • Clear separation of concerns
  • Swappable adapters without changing the domain

3. MultiHexagonalWeatherApp

🧩 Illustrates multi-hexagon communication using separate, independent modules with explicit interfaces in the Weather Module

📦 Modules (Hexagons)

  • Weather Module: same logic as HexagonalWeatherApp
    • Exposes IWeatherForecastQueryNotifier
    • Exposes IWeatherForecastQueryLogger
    • Adapters:
      • RandomWeatherForecastAdapter: returns random temps
      • WeatherApiForecastAdapter: fetches real data (e.g. from WeatherApi)
  • Notification Module:
    • Exposes INotificationEmailSender
    • Adapter: EmailWeatherNotifierAdapter using INotificationEmailSender (mocked with FakeEmailSender)
  • History Module:
    • Adapter: EfHistoryQueryLoggerAdapter stores past queries using Entity Framework

🧭 Responsibilities per hexagon

Hexagon Role
Weather Core logic & API
Notification Sends alerts (email, etc)
History Logs queries to database

🕸 Interaction

The WeatherService orchestrates both cross-hexagon calls via explicit interfaces injected through dependency injection.


4. MultiHexagonalWithUseCaseWeatherApp

🧩 Illustrates multi-hexagon communication using separate, independent modules without explicit interfaces in the Weather Module. The orchestration is handled by GetWeatherForecastUseCase in a dedicated Use Case module.

📦 Modules (Hexagons)

  • Weather Module: same logic as HexagonalWeatherApp
    • Adapters:
      • RandomWeatherForecastAdapter: returns random temps
      • WeatherApiForecastAdapter: fetches real data (e.g. from WeatherApi)
  • Notification Module:
    • Exposes INotificationEmailSender
    • Exposes IWeatherForecastQueryNotifier
    • Adapter: EmailWeatherNotifierAdapter using INotificationEmailSender (mocked with FakeEmailSender)
  • History Module:
    • Exposes IWeatherForecastQueryLogger
    • Adapter: EfHistoryQueryLoggerAdapter stores past queries using Entity Framework
  • UseCases Module:
    • Orchestrate the Weather, Notification and History modules.
    • Exposes GetWeatherForecastUseCase

🕸 Interaction

The GetWeatherForecastUseCase use case orchestrates both cross-hexagon calls via injected through dependency injection. No more side effects interfaces in the Weather Module at the cost of an extra-layer.


🛠 Setup Instructions

Before running the HexagonalWeatherApp, MultiHexagonalWeatherApp or MultiHexagonalWithUseCaseWeatherApp, make sure to configure your API key for the weather provider.

🌐 Configure Weather API Key

All three hexagonal applications use an external weather API (e.g. WeatherApi). To inject the API key securely:

cd HexagonalWeatherApp

dotnet user-secrets init
dotnet user-secrets set "WeatherApi:ApiKey" "YOUR_KEY_VALUE_HERE"

🧰 Tech Stack

  • .NET 9
  • ASP.NET Core
  • Entity Framework Core (InMemory)
  • Clean Hexagonal Architecture (Ports & Adapters)
  • WeatherApi

🐾 Special Thanks

Big thanks to my cats 🐱 who made sure I never wrote more than three lines of code without a tail on the keyboard, a paw on the screen, or a loud meow demanding attention.

This architecture may be hexagonal, but the inspiration was pure feline.

About

Hexagonal Architecture with .NET

Topics

Resources

Stars

Watchers

Forks

Languages