using ChapterX.Application.Abstractions; using ChapterX.Application.Auth; using ChapterX.Domain.Repositories; using ChapterX.Infrastructure.Data.DataContext; using ChapterX.Infrastructure.Repositories; using ChapterX.Infrastructure.Services; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace ChapterX.Infrastructure { public static class DependencyInjection { public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) { var poolSection = configuration.GetSection("ConnectionPool"); var minPoolSize = poolSection.GetValue("MinPoolSize", 1); var maxPoolSize = poolSection.GetValue("MaxPoolSize", 20); var idleLifetime = poolSection.GetValue("ConnectionIdleLifetime", 300); var pruningInterval = poolSection.GetValue("ConnectionPruningInterval", 10); var commandTimeout = poolSection.GetValue("CommandTimeout", 30); var connectionTimeout = poolSection.GetValue("Timeout", 15); var baseConnectionString = configuration.GetConnectionString("Database")!; var connectionString = $"{baseConnectionString};Minimum Pool Size={minPoolSize};Maximum Pool Size={maxPoolSize};" + $"Connection Idle Lifetime={idleLifetime};Connection Pruning Interval={pruningInterval};" + $"Command Timeout={commandTimeout};Timeout={connectionTimeout}"; services.AddDbContextPool(options => options.UseNpgsql(connectionString)); services.AddScoped(sp => sp.GetRequiredService()); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); return services; } } }