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.
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)
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.
- Install SQl Server Developer
- Install SQL Server Management Studio (SSMS)
- Open SSMS (it must propose to connect to your sql server express which must be running, otherwise open SQL Server and configure it)
- Create a new DB:
- Insert the DB name and press Ok
- Create the table
- Add the Id row and set it as primary key
- Change Identity Specification property of Id so that it is Identity
- Name and save the table
- Repeat the step until you created both Selfie and Wookie table and inserted some rows
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
You will notice an issue when trying to list the available migrations through dotnet eg migrations list
command:
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:
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:
- Migration project: to handle the migration of the infrastructure into the DB
- 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:
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
:
- the infrastructure project must reference the migration project in order to the
dotnet ef
command line to be able to access this folder - 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
- Yous must be changed the outpupath of migration to target the bin folder where the
- remove the table created in sql server management studio
- 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
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
-
Go to the root directory of the application project: BackEndFormation
-
Open a console from there
-
Genere a dev certificate:
dotnet dev-certs https
-
Self trust the certificate:
dotnet dev-certs https --trust
-
In the launchSettings.json
- in the profiles list, there should be the 'BackEndFormation' profile with commandName='Project':
- set the applicationUrl to https://localhost:\<port>
- set the applicationUrl to https://localhost:\<port>
- in the profiles list, there should be the 'BackEndFormation' profile with commandName='Project':
-
Start the server:
dotnet run
There are 4 main web server hosts (without considering the cloud servers):
- Apache: most of the website uses it
- Tomcat: simplification of apache
- IIS: originally for microsoft group, since .NET open source it tends to spread among others
- Nginx: used by the biggest website and having most of the traffic
- Right-clik on your website project > Publish
- Select Folder
and follow the step - Click on edit profile to modify some features:
- 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:
- Click on save
- Publish
- Open command prompt from the root of the website project
- 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>
- Right-clik on your website project > Publish
- Select Web Server IIS
- Select Web Deploy
- Set your configuration and Finish
And now your ISSProfile.pubxml file is ready!
- Trace: the most detailed level
- Debug: the level used to debug the application
- Information: the level used to inform the user
- Warning: the level used to warn the user
- Error: the level used to inform the user that an error occured
- Critical: the level used to inform the user that a critical error occured
- 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.
- Console: the log is displayed in the console
- Debug: the log is displayed in the debug window
- EventSource: the log is displayed in the event viewer
- EventLog: the log is displayed in the event log
- TraceSource: the log is displayed in the trace source
- AzureAppServicesFile: the log is displayed in the azure app service file
- AzureAppServicesBlob: the log is displayed in the azure app service blob
- ApplicationInsights: the log is displayed in the application insights
- Seq: the log is displayed in the seq
- Elasticsearch: the log is displayed in the elasticsearch
- email: the log is sent by email
- etc...
A dialog box without a mediator can be represented as below:
A mediator aims to sanitize this dialog box through a mediator which would handle the communication between each part as below:
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.