Cover image for Implementing SOLID REST API using ASP.NET Core

Implementing SOLID REST API using ASP.NET Core

samueleresca profile image Samuele Resca ・3 min read

Originally posted on my blog

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: https://gist.github.com/samueleresca/74dc45a58f61232fdf74c7ae71b2e8a3

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: https://gist.github.com/samueleresca/59bea93e513b81838ee3a405fadde199

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: https://gist.github.com/samueleresca/8bcc68857041f9c7d2b4bbb4beb9d5f7

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. Cover picture by Corrado Zeni. Cheers :)

Posted on by:

samueleresca profile

Samuele Resca


Samuele Resca is an Microsoft MVP Visual Studio and Development Technologies, Software Engineer, specializing mainly on ASP.NET MVC and in general about everything that revolves around the web. Samuele was born in 1994, and works as a software developer @ YOOX NET-A-PORTER Group He loves the MVC frameworks, ASP.NET MVC, Javascript, Node.js and Typescript.


Editor guide

Course : BaseEntity in API controller


I have fixed on github github.com/samueleresca/Blog.Turnm...

Thank you for the feedback.

Best regards,


Great tutorial, thanks!
But, shouldn't the Repository be inside the DAL layer instead of the DL,
since we are accessing the database inside the repository?


DAL layer in UI layer?


Where ui layer references DAL later?


I removed the DEV sticker from my laptop after this.