.NET REST API Flashcards

1
Q

What is the Microsoft Framework used for relational database interaction?

A

Entity Framework Core

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the CLI commands used for building a .Net Rest Api

A

dotnet new sln
dotnet new webapi -o [projname]
dotnet new classlib -f [framework] -o [name]
dotnet sln add [path]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What CLI command is used to add references to neighbouring class libraries?

A

dotnet add app/app.csproj reference lib/lib.csproj

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the main classlibs (aside from the main webapi) required for a rest api?

A

data -> for database interaction
services -> expressive methods for webapi use
test -> for testing
Class libs are a great way to seperate concerns and keep a clean working directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which class defines the configuration & services for the REST API?

A

Startup.cs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which NuGet packages are required to be added

A

Add the Microsoft.EntityFrameworkCore & the Microsoft.EntityFrameworkCore.Design. Also the NewtonSoft.Json package is useful.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you define a data Model

A

Create a class (in the data classlib /Models/) that contains the layout of each table in your DB, with each table being defined as a field. Each field that is another class requires another model.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a database context

A

A database context defines the context, or overall shape, of the database. It is a class that inherits from DbContext (EF CORE) and should have the following shapes

public GoodbooksDbContext() { }
public GoodbooksDbContext(DbContextOptions options) : base(options) { }
public virtual DbSet PluralOfModelName { get; set; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you inject the EF CORE DbContext into the services.

A

In the ConfigureServices Method in Startup.cs:
services.AddDbContext(opts => {
opts.EnableDetailedErrors();
// setup database preference e.g. Postgres
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you setup a service

A

Define an interface with the methods required (i.e. what you want to do with the database), and then implement that interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you inject the EF Core database context into a service?

A

Create a readonly field, define an instance in the constructor parameters, then in the body assign the parameter to the readonly field.

private readonly GoodbooksDbContext _db;
public BookService(GoodbooksDbContext db)
{
  _db = db;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the standard EF Core database Methods for CRUD operations

A

Create -> _db.Add()
Read -> _db.Find() / _db.ToList()
Update -> _db.Update()
Delete -> _db.Delete()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What EF Core method must be invoked on any non-read CRUD operation

A

_db.SaveChanges()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Where should the service be injected?

A

Into any controller class that uses it/ handles any http request. Inject using the interface of the service.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Where are the controllers located?

A

In the main web API project. They handle requests that come in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What attributes can be given to controllers

A

[HttpGet(“/api/hello/”)] // Get method
[HttpPost(“/api/hello/”)] // Post method
[HttpPatch(“/api/hello/{id}”)] // Patch method
[HttpDelete(“/api/hello/{id}”)] // Delete Method
[Route(“{controller}/[action]”)] // if MVC
[Route(“api/[controller]”)] // to class
[ApiController] // to class

17
Q

When does a project need to reference a class library

A

If it uses the namespace. Add a reference with the dotnet CLI, or right click the project in Visual Studio.

18
Q

What is the return type of a webapi controller?

A

ActionResult

19
Q

What methods can be used to return data and status codes from a controller

A
OK(data); // status 200
Created(url, data); // status 201
NoContent(); // status 204
BadRequest(); // status 400
Unauthorized(); // status 401
NotFound(); // status 404
20
Q

What is a Makefile?

A

A makefile can define command line executions within a .NET project.

21
Q

What Makefile definitions should be used?

A

Project Variables

PROJECT_NAME ?= Goodbooks

.PHONY: migrations db

migrations:
cd && dotnet ef –startup-project migrations add $(mname) && cd ..
db:
cd && dotnet ef –startup-project database update && cd ..

— IN THE COMMAND LINE –
make migrations mname=Initial
make db

22
Q

How is a connection to a postgres database initialised?

A

In the Db context service definition in the Startup class, use the options method UseNpgsql.

opts.UseNpgsql(Configuration.GetConnectionString(“projname.dev”));

Then define the connection string in the appsettings.json file:
“projname.dev”: “Host=localhost;Port=5432; Username=username; Password=password; Database=projectname.dev”

23
Q

How is a database model field made nullable?

A

Add a question mark after the datatype -> string?

24
Q

When should migrations and database update be run?

A

Any time the database schema is updated.

25
Q

What Field & attribute can be used to define a relations in a database model?

A

Include the [ForeignKey()] for the relational field and reference a shared ID as a field.