Quick Look to .Net Core Rest API with Service, Data Layer and PostgreSql CF Modeling

My aim is with this article to get you to result quickly while learning.

Lets create from scratch.
I will not explain, how to install/open visual studio 2019 of course but if you need that information i wish to say let me know but don’t. Please google it, check from youtube if you can not figure it out then let me know..

Here are the steps first create a new project

It will show you a project types list. From this list you need to choose project in below or if this project not in your list you can search it from TOP search bar.

I used .Net 5.0 but if you do not have it .Net Core 3.x also works.

Congratulations. You just created .Net Core Api.

Fresh solution

Now we need additional layers for our application architecture.

Adding Service Layer
This layer will contain application business logic.

Service Layer

You already familiar with this screen from the beginning. Only the difference is we are going to select Class Library from this list.

After adding service layer

Adding Data Layer
This layer will contain application dbcontext and migration files. Again we are going to add Class Library as we did in Service step.

After adding data layer

Adding Model Layer
This layer will contain application DTOs (Data Transfer Object) and Entity Models. Again we are going to add Class Library as we did in Service step.

Adding Database Context to Data Layer
Before adding DBContext you need add these Nuget packages to the QuickLookExample.Data layer.

Then we are going to create a class under Data layer. You can name it what ever you want. As you see i gave QuickLookDbConext

Then you need to configure your DbConext like below. i did not put that step but application won’t find Order model when you try to use it. Because of dependency between them, you need to add Model layer to Data layer.

Open appsettings.Development.json from Api layer. then add connection string.

Configuring database dependecy injection

Now we are ready to scaffold to database

Add-Migration <comment>

Just added migration these changes did not apply to database yet.

Update-Database

After execute “Update-Database” command, we can see our database under PostgreSql Database Tree.

Adding Services

In this example, i will not separate interfaces and Classes. I assume you already know “What are the interface and Class”

In first glance your service should looks like above

Using dbContext in Service

Getting records from table using DbContext

Adding Order service to Dependency Injection

Implementing Order Service to Controller

Result

You can download the project from here

https://github.com/hasanhuseyinozkaya/QuickLookExample.git

Do not forget to follow me :)

Cheers!

Code and Electronic person. Fan of digital transformation and IoT