mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-19 10:22:10 +08:00
93 lines
3.4 KiB
C#
93 lines
3.4 KiB
C#
using System.Linq.Expressions;
|
|
using BuildingBlocks.Core.Model;
|
|
using BuildingBlocks.PersistMessageProcessor.Data;
|
|
using BuildingBlocks.Web;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Query;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
namespace BuildingBlocks.EFCore;
|
|
|
|
public static class Extensions
|
|
{
|
|
public static IServiceCollection AddCustomDbContext<TContext>(
|
|
this IServiceCollection services)
|
|
where TContext : DbContext, IDbContext
|
|
{
|
|
|
|
services.AddOptions<DatabaseOptions>()
|
|
.BindConfiguration(nameof(DatabaseOptions))
|
|
.ValidateDataAnnotations();
|
|
|
|
services.AddDbContext<TContext>((sp, options) =>
|
|
{
|
|
var databaseOptions = services.GetOptions<DatabaseOptions>(nameof(DatabaseOptions));
|
|
|
|
options.UseSqlServer(databaseOptions?.DefaultConnection,
|
|
dbOptions =>
|
|
{
|
|
dbOptions.MigrationsAssembly(typeof(TContext).Assembly.GetName().Name);
|
|
});
|
|
});
|
|
|
|
services.AddScoped<IDbContext>(provider => provider.GetService<TContext>());
|
|
|
|
return services;
|
|
}
|
|
|
|
public static IApplicationBuilder UseMigration<TContext>(this IApplicationBuilder app, IWebHostEnvironment env)
|
|
where TContext : DbContext, IDbContext
|
|
{
|
|
MigrateDatabaseAsync<TContext>(app.ApplicationServices).GetAwaiter().GetResult();
|
|
|
|
if (!env.IsEnvironment("test"))
|
|
SeedDataAsync(app.ApplicationServices).GetAwaiter().GetResult();
|
|
|
|
return app;
|
|
}
|
|
|
|
|
|
// ref: https://github.com/pdevito3/MessageBusTestingInMemHarness/blob/main/RecipeManagement/src/RecipeManagement/Databases/RecipesDbContext.cs
|
|
public static void FilterSoftDeletedProperties(this ModelBuilder modelBuilder)
|
|
{
|
|
Expression<Func<IAggregate, bool>> filterExpr = e => !e.IsDeleted;
|
|
foreach (var mutableEntityType in modelBuilder.Model.GetEntityTypes()
|
|
.Where(m => m.ClrType.IsAssignableTo(typeof(IAudit))))
|
|
{
|
|
// modify expression to handle correct child type
|
|
var parameter = Expression.Parameter(mutableEntityType.ClrType);
|
|
var body = ReplacingExpressionVisitor
|
|
.Replace(filterExpr.Parameters.First(), parameter, filterExpr.Body);
|
|
var lambdaExpression = Expression.Lambda(body, parameter);
|
|
|
|
// set filter
|
|
mutableEntityType.SetQueryFilter(lambdaExpression);
|
|
}
|
|
}
|
|
|
|
private static async Task MigrateDatabaseAsync<TContext>(IServiceProvider serviceProvider)
|
|
where TContext : DbContext, IDbContext
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|