DEV Community

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

Posted on

Implementing SOLID REST API using ASP.NET Core

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 :)

Discussion (7)

Collapse
johndoe59265913 profile image
John doe

Course : BaseEntity in API controller

Collapse
samueleresca profile image
Samuele Resca Author

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

Thank you for the feedback.

Best regards,
Samuele

Collapse
juslwk profile image
Justin Lim

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?

Collapse
alexeyzimarev profile image
Alexey Zimarev

I removed the DEV sticker from my laptop after this.

Collapse
samueleresca profile image
Samuele Resca Author

Why?

Collapse
johndoe59265913 profile image
John doe

DAL layer in UI layer?

Collapse
samueleresca profile image
Samuele Resca Author

Where ui layer references DAL later?