using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Modules.Library.Application.Gateways; using Modules.Library.Database.Database; using Modules.Library.Database.GatewaysImplementations; using Modules.Library.Database.Repositories; using System.Reflection; namespace Modules.Library.Database; public static class ServiceCollectionExtensions { public static IServiceCollection AddDatabase(this IServiceCollection services, string? connectionString) { if (string.IsNullOrWhiteSpace(connectionString)) throw new ArgumentNullException(nameof(connectionString)); services.AddDbContext(options => { options.UseNpgsql(connectionString, q => { q.MigrationsAssembly(Assembly.GetAssembly(typeof(LibraryDbContext))!.GetName().Name); q.MigrationsHistoryTable("__Migrations"); }); //options.UseNpgsql(connectionString); //AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); //Включаем UTC-время. //options.UseLazyLoadingProxies(); // lazy loading }); AddRepositories(services); AddGateways(services); return services; } //public static IServiceCollection AddDatabase(this IServiceCollection services, string connectionString, string? databaseName) //{ // AddMongoDb(services, connectionString, databaseName); // //services.AddScoped(); // services.AddScoped(q => // { // var context = new LibraryDbContext(q.GetRequiredService()); // context.Initialize(); // return context; // }); // AddRepositories(services); // AddGateways(services); // return services; //} private static void AddRepositories(IServiceCollection services) { services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); } private static void AddGateways(IServiceCollection services) { services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); //services.AddScoped(); } }