using Microsoft.EntityFrameworkCore; using Pole.Core; using Pole.Core.EventBus.EventStorage; using Pole.Core.EventBus.Transaction; using Pole.EventStorage.PostgreSql; using System; using System.Collections.Generic; using System.Text; namespace Microsoft.Extensions.DependencyInjection { public static class PolePostgreSqlStartupConfigExtensions { public static StartupConfig AddEntityFrameworkEventStorage(this StartupConfig config) where TContext : DbContext { return config.AddEntityFrameworkEventStorage(opt => { }); } public static StartupConfig AddEntityFrameworkEventStorage(this StartupConfig config, Action configure) where TContext : DbContext { if (configure == null) throw new ArgumentNullException(nameof(configure)); EFOptions eFOptions = new EFOptions(); configure(eFOptions); Action postgreSqlOptionsConfig = postgreSqlOptions => { postgreSqlOptions.DbContextType = typeof(TContext); postgreSqlOptions.Schema = eFOptions.Schema; using var scope = config.Services.BuildServiceProvider().CreateScope(); var provider = scope.ServiceProvider; using var dbContext = (DbContext)provider.GetRequiredService(typeof(TContext)); postgreSqlOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; }; config.Services.Configure(postgreSqlOptionsConfig); config.Services.AddScoped(); config.Services.AddSingleton(); config.Services.AddSingleton(); return config; } } }