DevDiary



.NET Core 3.1 MVC REST API - Full Course (YT)

REST API, .NET Core 3, EF Core

This material is public and published on YouTube by "Les Jackson".

It is a 3.5 hour tutorial titled ".NET Core 3.1 MVC REST API - Full Course".

The text below is a brief description of the content.

This course shows you how to build a full REST API using .NET Core 3.1. We’ll employ MVC, REST, the Repository Pattern, Dependency Injection, Entity Framework, Data Transfer Objects, (DTOs), AutoMapper to provide 6 API endpoints that will allow you to Create, Read Update and Delete resources.

Tools used = Visual Studio, SQL Server Management Studio and Postman.



Application Architecture

Model: ShoppingList



Interface: Shopping Repository

Interface: Shopping Repository

An interface is used here because initially, the repository will contain hard-coded mock data.

This is so that the API can be tested using Postman before setting up a database using Entity Framework Core.

Later in the project this repository is switched to one that queries a real database.

This is easy to do because of the decoupling provided by the interface.



MockShoppingRepo: Create List (Hard-Coded)

MockShoppingRepo: Implement Interface



Shopping Repo: Initial Data

Startup: Configure Services

In order to implement dependancy injection for the controller I added the line -

services.AddScoped<IShoppingRepo, MockShoppingRepo()>

Setting the service lifetime to "Scoped" means that instances of the object are created, once per client request.

Singleton means, 'the same object, for every request'.

Transient means, 'new instance created for every request'.



ShoppingListsController: Constructor

Controller: Dependency Injection

The controller inherits from ControllerBase

A private field is defined (_repository).

When an instance of the contoller class is created, it recieves an instance of the IShoppingList repository.

This is set to the private field so the the methods inside the controller can access the properties and methods from the repository.

These methods, declared in the interface have to be defined in the controller.

The details of these methods can be seen in the next two screenshots.

They are both HttpGET except one method accepts an integer parameter.



ShoppingListsController: Get All Items

ShoppingListsController: Get Item By Id



PostMan: Test API, Get All Items

PostMan: Test API, Get Item By Id



Install Packages



ShoppingList Context

AppSettings: Connection String



StartUp: Services Add DbContext

Set Up Database

Installed the Entity Framework Core packages.

Created DbContext file according tot he tutorial.

Created a "connection string".

Configured the connection string in the "Startup.cs" file.

Add data annotations to the properties in the model.

Set up EF migrations and create "Initial" migration to create database.



Update Model (EF Annotations)

Add Migration and Update-Database



Check to See if DB Created

Enter Data into Database



Check Database Table From Visual Studio

Create New Repository (Constructor DI)



ShoppingList Repo: Get Full List

ShoppingList Repo: Get Item By Id



Startup.cs: AddScoped

Startup.cs

The Mock repository is replaced with "ShoppingRepo".

This means the API will now retrieve information from the database.



Test New Repo: Get Full List

Test New Repo: Get Item By Id



Visual Studio: Run API

Visual Studio: Run API



Data Transfer Objects

Data Transfer Objects

One reason that DTO's are used is to keep some of the properties of the internal model private.

This is useful if the model contains sensitive information (DOB, financial info, passwords, etc).

In my app, I do not include the "ShopName" property in the DTOs, so this information does not get sent to the client.



Install AutoMapper

Startup.cs: Add Service (AutoMapper)



Shopping List Read DTO

Shopping List Profile



Shopping Lists Controller: Add AutoMapprer DI



Controller: Update "Get All Items"

Controller: Update "Get Item By Id"



Test DTO: "Get Item By Id"

Test DTO: "Get Item By Id" (404)



Test DTO: "Get All Items"



Interface: Added Two Properties

Implement New Properties



Shopping List Create DTO

Add Map to Profile



Controller: Add "Name" to [HttpGet]

Controller: Add CreateItem()



Test POST Method: Create New Item

Header: Location Generated (REST)



Check Database



Add Property to Interface

Implement New Property



Controller: UpdateItem()

UpdateItem()

The id is used to get the shopping list object from the database.

If the id doesn't correspond to an item in the database then a NotFound message is returned.

Otherwise, the data from the new object (DTO) is copied to the object pulled from the database.

The repository method "UpdateShoppingList" is called but this does nothing. It is there for consistency.

The repository method "SaveChanges" is called.

A "No Conetent" status code is returned.



Replace Item with Id = 8

204, No Content Returned



Check Database

Check Database

Item 8 (Twining Tea) has been updated.

The product name, quantity and price have been changed.



Repository Interface: Delete

Repository: Delete



Controller: Delete



Get Full List

Delete Item with Id = 4 (Special K)



Get Item with Id = 4 (404)

Check Database (Special K Deleted)