Skip to content

Ystalard/BackEndFormation

Repository files navigation

BackEndFormation

This project will follow the udemy formation maîtriser les web api rest avec aspnet core dotnet

This is a wep api rest project in asp.net.

Introduction

REST uses http

image

the TDD principle

TDD_Global_Lifecycle It consists in generating the unitary test to validate the implementation (the unitary tests behaves like running the project in debug mode on a particular feature)

ORM for Object Relational Mapping

image The ORM is like a translator between object of the application (in C#) to the database (DB). The ORM is like a cache, if nothing is saved on the database before stopping the application then any cached modification would be lost The ORM needs a provider in order to understand the language used by the DB. image

Creation of the Database

  1. Install SQl Server Developer
  2. Install SQL Server Management Studio (SSMS)
  3. Open SSMS (it must propose to connect to your sql server express which must be running, otherwise open SQL Server and configure it)
  4. Create a new DB:
    image
  5. Insert the DB name and press Ok
    image

Create a new table

  1. Create the table
    image
  2. Add the Id row and set it as primary key
    image
  3. Change Identity Specification property of Id so that it is Identity
    image
  4. Name and save the table
    image
  5. Repeat the step until you created both Selfie and Wookie table and inserted some rows
    • The Selfie table:
      image
    • The Wookie table:
      image

Migration

Instead of creating the DB in order to fit the context of our code, we can consider the context to be the reference and migrate it to the DB. This avoid to be dependent of the type of DB(MongoDB, Microsoft SQL server, Oracle, etc...) For this, we can use the dotnet tool cli (command line interface). All the packages are referenced here.
When it comes to migration we shall think of dotnet ef

  • Install dotnet ef: dotnet tool install --global dotnet-ef --version 8.0.1
    image

troubleshooting

You will notice an issue when trying to list the available migrations through dotnet eg migrations list command:
image So, adding the Microsoft.entityFrameWorkCore.Design to the Migration projet is required. You can add it in the BackendFormation.Core.Selfies.Data.Migration through nuget package interface on Visual Studio 2022. Then, if you get the below issue it means you are trying to access the DbContext on the migration project:
image But it can't be done in the migration project as the DbContext is defined in the Infrastructures project. we created two project to split the code properly:

  1. Migration project: to handle the migration of the infrastructure into the DB
  2. Infrasctructures project: to set up the context manipulated by the ORM. It is this project which handle the DB context.

When executing the dotnet eg migrations list command in Infrastructures project you should get the below issue:
image

This will be handled on the next commit by implementing the IDesignTimeDbContextFactory interface. For more details, you can check this topic

Once the design interface is created we can start the migration process first by initializing the migration: dotnet ef migrations add InitDatabase --project=..\BackEndFormation.Core.Selfies.Data.Migration:

  1. the infrastructure project must reference the migration project in order to the dotnet ef command line to be able to access this folder
  2. but this reference of migration in infrastructure project can't be added as a reference as Migration already depends on it!
    • Yous must be changed the outpupath of migration to target the bin folder where the dotnet ef command will build the Infrastucture project
  3. remove the table created in sql server management studio
  4. generate the db from the migration:
    • cd BackEndFormation.Core.Selfies.Infrastructures
    • dotnet ef database update: if it can't find the assembly then check where is the folder where this command execute the build and past the dll directly in this folder

Update a migration

When a new property is added to a data.domain class then it must be added to the migration with below command:
dotnet ef migrations add AddDescription --startup-project . --project=..\BackEndFormation.Core.Selfies.Data.Migration

Run a dot net server

  1. Go to the root directory of the application project: BackEndFormation

  2. Open a console from there

  3. Genere a dev certificate: dotnet dev-certs https

  4. Self trust the certificate: dotnet dev-certs https --trust

    • click on yes:
      image
  5. In the launchSettings.json

    • in the profiles list, there should be the 'BackEndFormation' profile with commandName='Project':
  6. Start the server: dotnet run

Publication

Host servers

There are 4 main web server hosts (without considering the cloud servers):

  1. Apache: most of the website uses it
  2. Tomcat: simplification of apache
  3. IIS: originally for microsoft group, since .NET open source it tends to spread among others
  4. Nginx: used by the biggest website and having most of the traffic

Publish in a folder with VS 2022

  1. Right-clik on your website project > Publish
    image
  2. Select Folder
    imageand follow the step
  3. Click on edit profile to modify some features:
    image
  4. Choose Deployment Mode: Self-contained to be independent from the server you aim to publish. For instance, if you need a framework then with Self-contained mode you will install it on the server when publishing. For instance:
    image
  5. Click on save
    • The profile of the publication can now be accessed and modify from the .pubxml file just created:
      image
  6. Publish
    image

Publish from command line

  1. Open command prompt from the root of the website project
  2. run dotnet publish -c Release

It can also be configured from command line see the documentation. But you can also publish from the command line with the configuration set from a .pubxml file with this command: dotnet publish -p:PublishProfile=<publishFolder>

Publish on IIS with VS 2022

  1. Right-clik on your website project > Publish
    image
  2. Select Web Server IIS
    image
  3. Select Web Deploy
  4. Set your configuration and Finish
    image

And now your ISSProfile.pubxml file is ready!

Log

Log levels

  1. Trace: the most detailed level
  2. Debug: the level used to debug the application
  3. Information: the level used to inform the user
  4. Warning: the level used to warn the user
  5. Error: the level used to inform the user that an error occured
  6. Critical: the level used to inform the user that a critical error occured
  7. None: the level used to disable the log

The threshold is the minimum level of log to be displayed, it is set to the lowest level by default and can be changed to a higher level from Logging:LogLevel:Default in appsettings.json file. Also, the appsettings.json file can be overriden by the appsettings.Development.json file or any other environment file. This allows to have the same configuration for all the environment and to override it for a specific environment.

Log providers

  1. Console: the log is displayed in the console
  2. Debug: the log is displayed in the debug window
  3. EventSource: the log is displayed in the event viewer
  4. EventLog: the log is displayed in the event log
  5. TraceSource: the log is displayed in the trace source
  6. AzureAppServicesFile: the log is displayed in the azure app service file
  7. AzureAppServicesBlob: the log is displayed in the azure app service blob
  8. ApplicationInsights: the log is displayed in the application insights
  9. Seq: the log is displayed in the seq
  10. Elasticsearch: the log is displayed in the elasticsearch
  11. email: the log is sent by email
  12. etc...

Middleware

image

Mediator

A dialog box without a mediator can be represented as below:
image

A mediator aims to sanitize this dialog box through a mediator which would handle the communication between each part as below:
image

CQRS

CQRS means Command Query Responsibility Segregation. It is a pattern which consist in separating the command and the query in the application. The command is the action which modify the state of the application and the query is the action which read the state of the application. This pattern is used to simplify the code and to make it more readable and maintainable. It is also used to improve the performance of the application by separating the read and the write of the application.
image

About

this project will follow a formation on udemy

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages