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

This is a kick-off project which will have all essential things integrated into it. When we have to start with a new project, then we should think of the business needs. When the business requirement is ready, then we have to start focusing the business and we should be ready with basic/essential libraries.
We will see some important libraries in order, which should be integrated to our project for kick-off.

Table of Contents

What is Hexagonal Architecture

It is an architecture pattern which was introduced by Alistair Cockburn in 2005, which will solve problems in maintaining applications in traditional architecture, which we used to implement by Database-centric 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 In ASP.NET Core

Benefits of Hexagonal Architecture

Disadvantages of Hexagonal Architecture
Domain layer will be heavy
Lots of logic will be implemented in Domain layer (sometimes called as Core layer)

Layers of the Hexagonal Architecture

In this approach, we can see that all the Layers are dependent only on the Core Layers.
Domain Api layer
Domain Api Layers (Core layer) is implemented in the center and never depends on any other layer. It is a contract for domain layer interaction (ports) so that primary and secondary adapters can implement the contract. This is also known and DIP or Dependency Inversion Principle Domain layer
Domain Layers (Business layer)
These layers have business logic and are kept clean with no other dependencies.
Rest Adapter layer
Rest Adapter also known as left port’s adapter and primary adapter where we implement restful service (i.e., GET, POST, PUT, DELETE, etc.)
Persistence Adapter layer
Rest Adapter, also known as right port's adapter and secondary adapter, is where we have implemented Entity framework core which already implements a repository design pattern. DbContext will be UOW (Unit of Work) and each DbSet is the repository. This interacts with our database using dataproviders
Bootstrap/Presentation Layer
This is the final build of project, where it all begins.
You will understand more when we start implementing the project below.

Getting Started with Hexagonal Architecture

Step 1
Download and install Visual Studio Extension from Project Template
Download and install Visual Studio extension from Microsoft marketplace.
Hexagonal Architecture In ASP.NET Core
Step 2 - Create Project
Select project type as WebAPI, and select Hexagonal Architecture.
Hexagonal Architecture In ASP.NET Core
Step 3 - Select Hexagonal Architecture Project Template
Select project type as Web API, and select Hexagonal Architecture.
Hexagonal Architecture In ASP.NET Core
Step 4
Project Is Ready
Hexagonal Architecture In ASP.NET Core
Step 5
Build and run application
Health check UI
Navigate to Health Checks UI https://localhost:44377/healthcheck-ui and make sure everything is green.
** Change port number according to your application
Hexagonal Architecture In ASP.NET Core
Swagger UI
Swagger UI https://localhost:44377/OpenAPI/index.html
** Change port number according to your application
Hexagonal Architecture In ASP.NET Core
Diving inside the code snippets
Will look inside all layers.
Domain Api layer code snippets
First we will see domain layer, here will create entities based on requirement. In this example, we have created Deal Model.
  1. public class BaseEntity
  2. {
  3. [Key]
  4. public int Id { get; set; }
  5. }
  6. public class Deal : BaseEntity
  7. {
  8. public string Name { get; set; }
  9. public string Description { get; set; }
  10. }
Domain layer code snippets
In this layer, we have created DealDomain which is derived from IRequestDeal interface.
  1. public class DealDomain : IRequestDeal where T : class
  2. {
  3. private readonly DbSet table;
  4. public DealDomain(ApplicationDbContext dbContext)
  5. {
  6. ApplicationDbContext _dbContext;
  7. _dbContext = dbContext;
  8. table = _dbContext.Set();
  9. }
  10. public T GetDeal(int id)
  11. {
  12. return table.Find(id);
  13. }
  14. public List GetDeals()
  15. {
  16. return table.ToList();
  17. }
  18. }
Persistence layer code snippets
In Persistence layer, we will implement EntityFrameworkCore by creating ApplicationDbContext.
  1. public class ApplicationDbContext : DbContext
  2. {
  3. public ApplicationDbContext()
  4. {
  5. }
  6. public ApplicationDbContext(DbContextOptions options) : base(options)
  7. {
  8. }
  9. public DbSet Deals { get; set; }
  10. public override Task SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
  11. {
  12. return base.SaveChangesAsync(cancellationToken);
  13. }
  14. }
Rest adapter code snippets
In Rest adapter, we implement Api by creating Controller by using DealController.
  1. [ApiController]
  2. [Route("api/v{version:apiVersion}/[controller]")]
  3. public class DealController : ControllerBase
  4. {
  5. private readonly IRequestDeal _requestDeal;
  6. public DealController(IRequestDeal requestDeal)
  7. {
  8. _requestDeal = requestDeal;
  9. }
  10. // GET: api/deal
  11. [HttpGet]
  12. public IActionResult Get()
  13. {
  14. var result = _requestDeal.GetDeals();
  15. return Ok(result);
  16. }
  17. // GET: api/deal/1
  18. [HttpGet]
  19. [Route("{id}", Name = "GetDeal")]
  20. public IActionResult Get(int id)
  21. {
  22. var result = _requestDeal.GetDeal(id);
  23. return Ok(result);
  24. }
  25. }
What problem does this solution solve?
An app solution inclues all essential libraries with best practices, which will helps quick start the project. Developer can concentrate on business requirements and build entities. This helps save lots of development time.
Below are some essentials libraries which are already included in a project with best practice built on Hexagonal architecture
How does this help someone else?
This solution helps developers who can save development time and concentrate on business modules. And if he/she has less experience then it helps to maintain best practices in the project (like clean code)
How does the code actually work?
This is a project template which is hosted in marketplace.visualstudio.com. Download this extension from marketplace and install in your visual studio. While creating your project select this template.

Conclusion

In this article, we have seen what Hexagonal Architecture is. Project template will help us to quick start application.