From 7e813048e018e7bf7f891607dc2129ac47939455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20=C3=87etinkaya?= Date: Wed, 24 Jan 2024 15:02:19 +0300 Subject: [PATCH 1/2] feat(persistence): add db migration applier --- NArchitecture.sln | 16 +++++++++++++++- src/corePackages | 2 +- .../Persistence/Contexts/BaseDbContext.cs | 1 - .../Extensions/MigrationCreatorExtensions.cs | 14 -------------- .../Services/IMigrationCreatorService.cs | 5 ----- .../Services/MigrationCreatorService.cs | 19 ------------------- .../Persistence/Persistence.csproj | 2 ++ .../PersistenceServiceRegistration.cs | 19 ++++++++++--------- src/starterProject/WebAPI/Program.cs | 11 ++++++----- src/starterProject/WebAPI/WebAPI.csproj | 1 + 10 files changed, 35 insertions(+), 55 deletions(-) delete mode 100644 src/starterProject/Persistence/MigrationConfigurations/Extensions/MigrationCreatorExtensions.cs delete mode 100644 src/starterProject/Persistence/MigrationConfigurations/Services/IMigrationCreatorService.cs delete mode 100644 src/starterProject/Persistence/MigrationConfigurations/Services/MigrationCreatorService.cs diff --git a/NArchitecture.sln b/NArchitecture.sln index a764e75..0e7d7ac 100644 --- a/NArchitecture.sln +++ b/NArchitecture.sln @@ -40,7 +40,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Application.Tests", "tests\ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core.Test", "src\corePackages\Core.Test\Core.Test.csproj", "{04F34479-7304-45E2-AEA3-5269BA1543E6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core.WebAPI", "src\corePackages\Core.WebAPI\Core.WebAPI.csproj", "{24AE40CB-3961-4DBA-9E7B-E48893CA8DA5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core.WebAPI", "src\corePackages\Core.WebAPI\Core.WebAPI.csproj", "{24AE40CB-3961-4DBA-9E7B-E48893CA8DA5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core.Persistence.DependencyInjection", "src\corePackages\Core.Persistence.DependencyInjection\Core.Persistence.DependencyInjection.csproj", "{E1F0BD02-5781-4309-B469-DFDA36C2462E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core.Persistence.WebApi", "src\corePackages\Core.Persistence.WebApi\Core.Persistence.WebApi.csproj", "{93910DFF-42FA-4A27-9076-45F276E99143}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -104,6 +108,14 @@ Global {24AE40CB-3961-4DBA-9E7B-E48893CA8DA5}.Debug|Any CPU.Build.0 = Debug|Any CPU {24AE40CB-3961-4DBA-9E7B-E48893CA8DA5}.Release|Any CPU.ActiveCfg = Release|Any CPU {24AE40CB-3961-4DBA-9E7B-E48893CA8DA5}.Release|Any CPU.Build.0 = Release|Any CPU + {E1F0BD02-5781-4309-B469-DFDA36C2462E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E1F0BD02-5781-4309-B469-DFDA36C2462E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E1F0BD02-5781-4309-B469-DFDA36C2462E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E1F0BD02-5781-4309-B469-DFDA36C2462E}.Release|Any CPU.Build.0 = Release|Any CPU + {93910DFF-42FA-4A27-9076-45F276E99143}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {93910DFF-42FA-4A27-9076-45F276E99143}.Debug|Any CPU.Build.0 = Debug|Any CPU + {93910DFF-42FA-4A27-9076-45F276E99143}.Release|Any CPU.ActiveCfg = Release|Any CPU + {93910DFF-42FA-4A27-9076-45F276E99143}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -125,6 +137,8 @@ Global {85A9396B-D22C-4671-B912-2E12F2F43946} = {07E15C51-014F-4C05-B709-C273D2D4E78C} {04F34479-7304-45E2-AEA3-5269BA1543E6} = {403A7B6A-EDB3-4B56-8DF2-A92A3247C721} {24AE40CB-3961-4DBA-9E7B-E48893CA8DA5} = {403A7B6A-EDB3-4B56-8DF2-A92A3247C721} + {E1F0BD02-5781-4309-B469-DFDA36C2462E} = {403A7B6A-EDB3-4B56-8DF2-A92A3247C721} + {93910DFF-42FA-4A27-9076-45F276E99143} = {403A7B6A-EDB3-4B56-8DF2-A92A3247C721} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {9208CFCE-156A-49CD-9E43-32CE67AAB957} diff --git a/src/corePackages b/src/corePackages index 0d786b9..581d2db 160000 --- a/src/corePackages +++ b/src/corePackages @@ -1 +1 @@ -Subproject commit 0d786b9222abe2705630cba94b60a30ad6fe2354 +Subproject commit 581d2db1d6bf847799ef62e5618649f95f93b465 diff --git a/src/starterProject/Persistence/Contexts/BaseDbContext.cs b/src/starterProject/Persistence/Contexts/BaseDbContext.cs index 67eddb1..eeab2ae 100644 --- a/src/starterProject/Persistence/Contexts/BaseDbContext.cs +++ b/src/starterProject/Persistence/Contexts/BaseDbContext.cs @@ -19,7 +19,6 @@ public BaseDbContext(DbContextOptions dbContextOptions, IConfiguration configura : base(dbContextOptions) { Configuration = configuration; - Database.EnsureCreated(); } protected override void OnModelCreating(ModelBuilder modelBuilder) => diff --git a/src/starterProject/Persistence/MigrationConfigurations/Extensions/MigrationCreatorExtensions.cs b/src/starterProject/Persistence/MigrationConfigurations/Extensions/MigrationCreatorExtensions.cs deleted file mode 100644 index 584a5f2..0000000 --- a/src/starterProject/Persistence/MigrationConfigurations/Extensions/MigrationCreatorExtensions.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; -using Persistence.MigrationConfigurations.Services; - -namespace Persistence.MigrationConfigurations.Extensions; -public static class MigrationCreatorExtensions -{ - public static Task UseMigrationCreator(this IApplicationBuilder app) - { - var service = app.ApplicationServices.GetRequiredService(); - service.Initialze(); - return Task.CompletedTask; - } -} diff --git a/src/starterProject/Persistence/MigrationConfigurations/Services/IMigrationCreatorService.cs b/src/starterProject/Persistence/MigrationConfigurations/Services/IMigrationCreatorService.cs deleted file mode 100644 index a5a253c..0000000 --- a/src/starterProject/Persistence/MigrationConfigurations/Services/IMigrationCreatorService.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace Persistence.MigrationConfigurations.Services; -public interface IMigrationCreatorService -{ - void Initialze(); -} \ No newline at end of file diff --git a/src/starterProject/Persistence/MigrationConfigurations/Services/MigrationCreatorService.cs b/src/starterProject/Persistence/MigrationConfigurations/Services/MigrationCreatorService.cs deleted file mode 100644 index 4c20187..0000000 --- a/src/starterProject/Persistence/MigrationConfigurations/Services/MigrationCreatorService.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Persistence.Contexts; - -namespace Persistence.MigrationConfigurations.Services; -public class MigrationCreatorService : IMigrationCreatorService -{ - private readonly BaseDbContext _context; - - public MigrationCreatorService(BaseDbContext context) - { - _context = context; - } - - public void Initialze() - { - if (_context.Database.CanConnect()) - _context.Database.Migrate(); - } -} \ No newline at end of file diff --git a/src/starterProject/Persistence/Persistence.csproj b/src/starterProject/Persistence/Persistence.csproj index 7f3d6fe..854a00e 100644 --- a/src/starterProject/Persistence/Persistence.csproj +++ b/src/starterProject/Persistence/Persistence.csproj @@ -13,6 +13,8 @@ + + diff --git a/src/starterProject/Persistence/PersistenceServiceRegistration.cs b/src/starterProject/Persistence/PersistenceServiceRegistration.cs index 46dd7b1..098d03f 100644 --- a/src/starterProject/Persistence/PersistenceServiceRegistration.cs +++ b/src/starterProject/Persistence/PersistenceServiceRegistration.cs @@ -1,9 +1,9 @@ using Application.Services.Repositories; +using Core.Persistence.DependencyInjection; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Persistence.Contexts; -using Persistence.MigrationConfigurations.Services; using Persistence.Repositories; namespace Persistence; @@ -12,14 +12,15 @@ public static class PersistenceServiceRegistration { public static IServiceCollection AddPersistenceServices(this IServiceCollection services, IConfiguration configuration) { - services.AddDbContext(options => options.UseInMemoryDatabase("nArchitecture")); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); + _ = services.AddDbContext(options => options.UseInMemoryDatabase("BaseDb")); + _ = services.AddDbMigrationApplier(buildServices => buildServices.GetRequiredService()); + + _ = services.AddScoped(); + _ = services.AddScoped(); + _ = services.AddScoped(); + _ = services.AddScoped(); + _ = services.AddScoped(); + _ = services.AddScoped(); return services; } diff --git a/src/starterProject/WebAPI/Program.cs b/src/starterProject/WebAPI/Program.cs index f8941f1..3fdbe72 100644 --- a/src/starterProject/WebAPI/Program.cs +++ b/src/starterProject/WebAPI/Program.cs @@ -1,5 +1,6 @@ using Application; using Core.CrossCuttingConcerns.Exceptions.Extensions; +using Core.Persistence.WebApi; using Core.Security; using Core.Security.Encryption; using Core.Security.JWT; @@ -9,7 +10,6 @@ using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using Persistence; -using Persistence.MigrationConfigurations.Extensions; using Swashbuckle.AspNetCore.SwaggerUI; using WebAPI; @@ -45,6 +45,7 @@ }); builder.Services.AddDistributedMemoryCache(); // InMemory + // builder.Services.AddStackExchangeRedisCache(opt => opt.Configuration = "localhost:6379"); // Redis // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle @@ -53,7 +54,7 @@ opt => opt.AddDefaultPolicy(p => { - p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); + _ = p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); }) ); builder.Services.AddSwaggerGen(opt => @@ -80,8 +81,8 @@ // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - app.UseSwagger(); - app.UseSwaggerUI(opt => + _ = app.UseSwagger(); + _ = app.UseSwaggerUI(opt => { opt.DocExpansion(DocExpansion.None); }); @@ -90,7 +91,7 @@ if (app.Environment.IsProduction()) app.ConfigureCustomExceptionMiddleware(); -_ = app.UseMigrationCreator(); +_ = app.UseDbMigrationApplier(); app.UseAuthentication(); app.UseAuthorization(); diff --git a/src/starterProject/WebAPI/WebAPI.csproj b/src/starterProject/WebAPI/WebAPI.csproj index 18fcd2e..8f98297 100644 --- a/src/starterProject/WebAPI/WebAPI.csproj +++ b/src/starterProject/WebAPI/WebAPI.csproj @@ -21,6 +21,7 @@ + From 80153ad3d99df17633b6ee327d1ca25d4ebec5f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20=C3=87etinkaya?= Date: Wed, 24 Jan 2024 15:11:19 +0300 Subject: [PATCH 2/2] style(*): reformat --- src/corePackages | 2 +- src/starterProject/WebAPI/Program.cs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/corePackages b/src/corePackages index 581d2db..15caee5 160000 --- a/src/corePackages +++ b/src/corePackages @@ -1 +1 @@ -Subproject commit 581d2db1d6bf847799ef62e5618649f95f93b465 +Subproject commit 15caee5bd53d271c42aafff6800298cc03f7acc7 diff --git a/src/starterProject/WebAPI/Program.cs b/src/starterProject/WebAPI/Program.cs index ef9d065..fcd8e58 100644 --- a/src/starterProject/WebAPI/Program.cs +++ b/src/starterProject/WebAPI/Program.cs @@ -46,6 +46,7 @@ }); builder.Services.AddDistributedMemoryCache(); // InMemory + // builder.Services.AddStackExchangeRedisCache(opt => opt.Configuration = "localhost:6379"); // Redis // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle