Implementing SOLID REST API using ASP.NET Core

The following article shows how to implementing SOLID REST API using ASP.NET Core. The solution uses generic repository pattern to perform CRUD operations on database, and also xUnit as test runner.

The solution will contain three key namespace:

  • Data access: it will implement information about the domain model and the relationship between entities. It will also contain the information about data context;
  • Domain logic: it will implement the repositories and services used by our web APIs;
  • API: it will implement controllers and middlewares to manage incoming requests;

The project is available on Github.

Project overview

Here is a brief schema of the project structure:

Implementing SOLID REST API using ASP.NET Core

 

The Blog.Turnmeup.DAL  represents the Data access namespace, the Blog.Turnmeup.DL represents the Domain Logic namespace, finally the Blog.Turnmeup.API exposes Rest APIs.

Project Testing

The key parts of the solution are convered by unit tests and integration tests. Test projects will use Moq as mocking library and xUnit as test runner. All tests will be contained in two namespaces:

  • DL.Tests: it will contain domain logic tests;
  • API.Tests: it will contain APIs tests;

Data access using Entity Framework

Data access layer uses Entity Framework Core as ORM.

The project (Blog.Turnmeup.DAL) contains entity classes which describe model data. The namespace also defines  BaseEntity model class which contains some common attributes. BaseEntity is extended by the other entity classes. Domain Logic namespace references models  to retrieve data.

Here is a schema of the project:

Implementing SOLID REST API using ASP.NET Core

In order to generate the database schema you need to run the following command inside the directory of the project:

dotnet ef database update

Domain Logic layer overview

This project (Blog.Turnmeup.DL) implements the Domain logic layer. It uses the Generic repository pattern combined with a generic service class. Here is an overview of the project:

Implementing SOLID REST API using ASP.NET Core

Unit testing

The Domain logic layer is covered by unit tests. The project Blog.Turnmeup.DL.Tests defines the unit tests inside the CourseServiceTests class. Here is the source code of tests:

Model definition

The Domain Logic layer also defines BaseResponseModel.
Blog.Turnmeup.API uses BaseResponseModel to serialize data throughs HTTP response. The Blog.Turnmeup.API will useAutomapper to map Data access layer models with Domain logic models.

API namespace overview

The Blog.Turnmeup.API defines the Controller class which returns data through HTTP. The CourseController handles incoming HTTP requests and call Domain logic layer to retrieve or update data. Here is an schema overview of the project:

Implementing SOLID REST API using ASP.NET Core

Dependency injection and class mapping

The Blog.Turnmeup.API project uses dependency injection to inject concrete classes to interfaces. It also uses Automapper package to map Blog.Turnmeup.DAL to Blog.Turnmeup.DL response models. Dependency injection is managed in the Startup.cs file and classes mapping is managed by the Infrastructure.Mapper class:

Unit testing

The Rest APIs layer is covered by unit tests. The project Blog.Turnmeup.API.Tests defines the unit tests inside the CourseControllerTests class. Tests use the TextFixture class to initialize a Test server and retrieve the dependencies services:

Final thought

You can find the code on Github.
The project can be extended with other model classes and other controller. In the next article will be dicovered the authentication part by using ASP.NET Core Identity:

Unit testing ASP.NET Core Identity

Cover picture by Corrado Zeni.

Cheers 🙂