Factoring out Business Logic in Services

In this lesson, we will learn how to factor out business logic and other services for adding them to the dependency injection container to get a more modular and maintainable application.

In this lesson, we will modify our customers list example to take advantage of dependency injection.

First of all, the whole DB logic of the customers’ list example must be moved to a different .NET library project that we will call DbProject.

We will move our in-memory database there. DbProject will have internal classes that will not be visible to the ASP.NET Core project. They will just see what we decide to export as public.

The database layer internal logic

Internal classes will include our in-memory database. We don’t need static variables to implement its singleton pattern since we will add it to the dependency injection engine as a singleton. Moreover, we will define it as internal since it must not be accessed directly from outside the database layer. Below is the modified code:

Get hands-on with 1200+ tech skills courses.