1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41// TODO: Rewrite the order of the steps now including the DTOs and Repositories
// 1 - Create a Model => root / Models / Model.cs
// 2 - Create a DbContext => root / Data / AppDbContext.cs
// 3 - Add the database sets here (DbSet<T>)
// 4 - Add the connection string to the appsettings.json file or appsettings.Development.json file
// "ConnectionStrings" : { "NameOfYourConnection" : "your_connection_string" }
// 5 - Register the DbContext as a service
builder.Services.AddDbContext<AppDbContext>(options =>
{
var connectionString = builder.Configuration.GetConnectionString("NameOfYourConnection");
options.<TheSqlAdapter>(connectionString);
});
// 6 - Add-Migrations NameOfTheMigration => (create the migration files)
// 7 - Update-Database => (push the schema to the database)
// Code First Approach
// 8 - Create a Repository => root / Repositories / ... /NameRepository.cs
// inject and use the DbContext in the service
// 9 - Register the Repository as a service in the DI Container, the in the Program.cs file => root / Program.cs
// builder.Services.AddScoped<INameRepository, NameRepository>();
// 10 - Create a Service => root / Services / ... / NameService.cs
// inject and use the repository in the service
// 11 - Register the Service as a service in the DI Container, the in the Program.cs file => root / Program.cs
// builder.Services.AddScoped<INameService, NameService>();
// NOTE: Reposotories and Services should be created from an interface and injected as interfaces, in case you want to change the implementation later
// - even implement a Generic repository pattern
// ( optional, but recommended )- Create DTOs => root / DTOs / ... / EntityNameDTO.cs
// DTOs can be implemented in the Service layer and the Controller can just get the filtered data
// 12 - Create a Controller => root / Controllers / NameController.cs
// inject and use the service in the controller