Building Clean Architecture Application using ASP.NET Core Web API and Angular 11 — Backend
Tags: angular, dotnet, cleanarchitecture
Published: 2020-11-23 06:40:37

In this article, we are going to learn building a Clean Architecture application using ASP.NET Core Web API and Angular 11 Front End.

**Overview**
Building an ASP.NET Core WebAPI using Clean Architecture
**Features:**
- ASP.NET Core 3.1 Web API.
- API Versioning
- Swagger UI.
- JWT Token Authentication
- Global Error Handler Middleware
- CQRS ( Command Query Responsibility Segregation)
- MediatR
- NHibernate ORM
- Generic Repository Pattern
- Fluent Validation
- SQLSERVER

**Github Project**
You can refer to the below GitHub project for complete implementation.
{% github https://github.com/sunilkumarmedium/CleanArchitectureApp %}

**Onion Architecture**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b49rd4ylhgifcf6hl2iq.png)

> **Clean Architecture**
The architecture defines where the application performs its core functionality and how that functionality interacts with things like the database and the user interface. Clean architecture refers to organizing the project so that it’s easy to understand and easy to change as the project grows.

> **Command Query Responsibility Segregation** is a design pattern to separate the read and write processes of your application. Read operations are called Queries and write operations are called Commands.

**Project Structure**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rr3ha7fsuejc9kyf7s62.png)

**Swagger UI Setup**
The swagger object model and middleware exposes the JSON objects as Endpoints

Installing Swagger packages using NuGet package console with the below command.

`Install-Package Swashbuckle.AspNetCore -Version 5.6.3`

Swagger Configuration with Bearer token Authentication
{% gist https://gist.github.com/sunilkumarmedium/c6768be4d14413b02b0966dbddbd6626 %}

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qazrl8py9i8shhqixa3n.png)

**JWT Token Implementation**

The JWT Bearer Token is used for accessing the WebApi endpoints securely. Below is the JWT configuration
{% gist https://gist.github.com/sunilkumarmedium/7ad8107b62de835deddc732ad64ed38d %}

Token Generation On Successful Login
{% gist https://gist.github.com/sunilkumarmedium/f374d2ab5e679ddda6c2046bd03f5c19 %}

**Global Error Middleware**

Server Internal Error Exceptions and Business Validation Exceptions are handled and the response is returned for use of displaying these validations in UI.
{% gist https://gist.github.com/sunilkumarmedium/0206f4d07281defebc0d083ccb75e685 %}

**NHibernate ORM and Generic Repository Pattern**

NHibernate is used to interact with the database. Singleton Session factory is implemented. Below is the Persistence Layer Service Extension.
{% gist https://gist.github.com/sunilkumarmedium/570ea844459ba191e95cc641b4278edd %}

**User Entity**
{% gist https://gist.github.com/sunilkumarmedium/417ebdecb4da04505995250304426a9d %}

**UserMap**
{% gist https://gist.github.com/sunilkumarmedium/36d9faca55e8f623aa4a5e6bca9e5668 %}

**Fluent Validation**
Package Reference:

`<PackageReference Include=”FluentValidation.DependencyInjectionExtensions” Version=”9.3.0" />`

To define a set of validation rules for a particular object, you will need to create a class that inherits from AbstractValidator<T>, where T is the type of class that you wish to validate.

Below is the sample Create User Validations.
{% gist https://gist.github.com/sunilkumarmedium/6183a0ede703f9acc5b049ae9b2257c6 %}

**Summary**
Hope enjoyed my first article please feel free to download the code and play around. Next article we will get in touch with Angular 11.

**Series Links**
Building an ASP.NET Core WebAPI using Clean Architecture
{% medium https://sunil-kumar-60226.medium.com/building-clean-architecture-application-using-asp-net-core-web-api-and-angular-11-backend-81b57c315dfa %}
Building an Angular 11 Application integrated with WebAPI
{% medium https://sunil-kumar-60226.medium.com/building-single-page-application-using-angular-11-frontend-7170ae9f8aec %}