refactor: refactor persist message processor

This commit is contained in:
Pc 2023-03-11 19:45:02 +03:30
parent eab6386f3d
commit fe8c0f444d
5 changed files with 15 additions and 31 deletions

View File

@ -5,10 +5,8 @@ using Microsoft.Extensions.DependencyInjection;
namespace BuildingBlocks.PersistMessageProcessor;
using EFCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
public static class Extensions
{
@ -39,38 +37,17 @@ public static class Extensions
return services;
}
public static IApplicationBuilder UseMigration<TContext>(this IApplicationBuilder app, IWebHostEnvironment env)
public static IApplicationBuilder UseMigrationPersistMessage<TContext>(this IApplicationBuilder app, IWebHostEnvironment env)
where TContext : DbContext, IPersistMessageDbContext
{
MigrateDatabaseAsync<TContext>(app.ApplicationServices).GetAwaiter().GetResult();
using var scope = app.ApplicationServices.CreateScope();
if (!env.IsEnvironment("test"))
{
SeedDataAsync(app.ApplicationServices).GetAwaiter().GetResult();
}
var persistMessageContext = scope.ServiceProvider.GetRequiredService<PersistMessageDbContext>();
persistMessageContext.Database.Migrate();
var context = scope.ServiceProvider.GetRequiredService<TContext>();
context.Database.Migrate();
return app;
}
private static async Task MigrateDatabaseAsync<TContext>(IServiceProvider serviceProvider)
where TContext : DbContext, IPersistMessageDbContext
{
using var scope = serviceProvider.CreateScope();
var persistMessageContext = scope.ServiceProvider.GetRequiredService<PersistMessageDbContext>();
await persistMessageContext.Database.MigrateAsync();
var context = scope.ServiceProvider.GetRequiredService<TContext>();
await context.Database.MigrateAsync();
}
private static async Task SeedDataAsync(IServiceProvider serviceProvider)
{
using var scope = serviceProvider.CreateScope();
var seeders = scope.ServiceProvider.GetServices<IDataSeeder>();
foreach (var seeder in seeders)
{
await seeder.SeedAllAsync();
}
}
}

View File

@ -100,7 +100,7 @@ public static class InfrastructureExtensions
});
app.UseCorrelationId();
app.UseHttpMetrics();
app.UseMigration<PersistMessageDbContext>(env);
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
app.UseCustomHealthCheck();
app.MapMetrics();
app.MapGet("/", x => x.Response.WriteAsync(appOptions.Name));

View File

@ -30,6 +30,7 @@ using Serilog;
namespace Flight.Extensions.Infrastructure;
using BuildingBlocks.PersistMessageProcessor.Data;
using Microsoft.AspNetCore.HttpOverrides;
public static class InfrastructureExtensions
@ -105,6 +106,7 @@ public static class InfrastructureExtensions
});
app.UseCorrelationId();
app.UseHttpMetrics();
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
app.UseMigration<FlightDbContext>(env);
app.MapMetrics();
app.UseCustomHealthCheck();

View File

@ -26,6 +26,7 @@ using Serilog;
namespace Identity.Extensions.Infrastructure;
using BuildingBlocks.PersistMessageProcessor.Data;
using Configurations;
using Microsoft.AspNetCore.HttpOverrides;
@ -101,6 +102,7 @@ public static class InfrastructureExtensions
{
options.EnrichDiagnosticContext = LogEnrichHelper.EnrichFromRequest;
});
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
app.UseMigration<IdentityContext>(env);
app.UseCorrelationId();
app.UseHttpMetrics();

View File

@ -28,6 +28,8 @@ using Serilog;
namespace Passenger.Extensions.Infrastructure;
using BuildingBlocks.PersistMessageProcessor.Data;
public static class InfrastructureExtensions
{
public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder builder)
@ -95,6 +97,7 @@ public static class InfrastructureExtensions
{
options.EnrichDiagnosticContext = LogEnrichHelper.EnrichFromRequest;
});
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
app.UseMigration<PassengerDbContext>(env);
app.UseCorrelationId();
app.UseHttpMetrics();