Hexagonal Architecture ASP.NET Core

In this article, you will see a Web API solution template which is built on Hexagonal Architecture with all essential features using .NET Core.

Introduction

We will see some important libraries order wise, which should be integrated to our project for kick-off

Essentials Libraries/ Steps to be Ready for ASP.NET Core

  • Web API
  • Entityframework Core
  • Expection handling
  • Unit testing via NUnit
  • Versioning
  • Swagger UI
  • Loggings — seriLog
  • Health checks UI
  • JWT authentication

What is Hexagonal Architecture

Hexagonal Architecture, or to call it properly, “Ports and Adapters pattern”, is driven by the idea that the application is central to your system. All inputs and outputs reach or leave the core of the application through a port that isolates the application from external technologies, tools and delivery mechanics

Hexagonal Architecture is based on the inversion of control principle. It’s composed of domain concentric architecture where layers interface with each other towards the Domain (Entities/Classes).

Main benefit of Hexagonal architecture is plug & play, flexibility and de-coupling. In this approach, we can see that all the Layers are dependent only on the Domain layer.

Benefits of Hexagonal Architecture

  • Testability: As it decoupled all layers, so it is easy to write test case for each components
  • Adaptability/Enhance: Adding new way to interact with application is very easy
  • Sustainability: We can keep all third party libraries in Infrastructure layer and hence maintenance will be easy
  • Database Independent: Since database is separated from data access, it is quite easy switch database providers
  • Clean code: As business logic is away from presentation layer, it is easy to implement UI (like React, Angular or Blazor)
  • Well organised: Project is well organised for better understanding and for onboarding for new joinee to project

Disadvantages of Hexagonal Architecture

  • Bunch of layers: For implementing small application like CRUD operation, then too bunch of layers should be implemented

Layers of the Hexagonal Architecture

Domain Api layer

Domain layer

Rest Adapter layer

Persistence Adapter layer

Bootstrap/Presentation Layer

Getting Started with Hexagonal Architecture

Step 1: Download and Install Visual Studio Extension from Project Template

Step 2: Create Project

Step 3: Select Hexagonal Architecture project template

Step 4: Project is ready

Step 5: Build and run application

Health check UI

** Change port number according to your application

Swagger UI

** Change port number according to your application

What problem does this solution solve

Below are some essentials libraries which is already included in project with best practice built on Hexagonal architecture

  1. Entity Framework Core
  2. Dependency injection
  3. Logging-Serilog
  4. Authentication
  5. Swagger/ OpenAPI
  6. Error handling
  7. Unit testing via NUnit
  8. Versioning

How does this help someone else

How does the code actually work

Conclusion

Acknowledgements

Technical lead with over 10+ years of work experience in Analysis, Design and Development of Software Applications Architecture https://github.com/Amitpnk