Path: redb/redb.Postgres/Extensions/RedbServiceExtensions.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Postgres/Extensions/RedbServiceExtensions.cs ---- using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using redb.Core; using redb.Core.Models.Configuration; using redb.Core.Models.Contracts; using redb.Core.Models.Security; using redb.Postgres.Providers; using redb.Core.Providers; using redb.Core.Serialization; namespace redb.Postgres.Extensions { /// /// Extension methods for unified REDB registration following EF Core AddDbContext pattern /// public static class RedbServiceExtensions { /// /// Registers REDB with configuration via lambda (main method). /// Automatically registers all necessary providers and services. /// /// Service collection /// Delegate for configuration setup /// Service collection for method chaining /// /// services.AddRedb(options => { /// options.PropsSaveStrategy = PropsSaveStrategy.ChangeTracking; /// options.EnablePropsCache = true; /// options.PropsCacheMaxSize = 50000; /// }); /// [Obsolete("Use services.AddRedb(o => o.UsePostgres(connectionString)) from redb.Core.Extensions")] public static IServiceCollection AddRedbPostgres( this IServiceCollection services, Action? configure = null) { var config = new RedbServiceConfiguration(); configure?.Invoke(config); // Validation triggers in setters! services.AddSingleton(config); // Auto-register IRedbContext if ConnectionString is provided if (!string.IsNullOrEmpty(config.ConnectionString)) { // Factory registration hands OWNERSHIP to the container: disposing the provider disposes // the NpgsqlDataSource and closes its pool. The old instance-registration form left the // pool open forever - every container restart (module hot-reload, tests) stacked another // orphaned pool of idle sessions until the server-side pruner got them (2026-09-09). services.AddSingleton(_ => Data.NpgsqlDataSourceFactory.Create(config.ConnectionString, config.StringCollation, config.EnableLazyReferences)); services.AddScoped(sp => new Data.NpgsqlRedbContext(sp.GetRequiredService())); } // Register SecurityContext (required for providers) services.AddScoped(sp => AmbientSecurityContext.GetOrCreateDefault()); // Register ALL providers automatically services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // The user configuration service takes its statements from the dialect services.AddSingleton(_ => new Sql.PostgreSqlDialect(config.StringCollation)); // Register user configuration service services.AddScoped(); services.AddScoped(); return services; } /// /// Registers REDB with configuration from appsettings.json /// /// Service collection /// Application configuration /// Section name in appsettings.json (default "RedbService") /// Service collection for method chaining /// /// In appsettings.json: /// "RedbService": { "EnablePropsCache": true, "PropsCacheMaxSize": 50000 } /// services.AddRedb(Configuration); /// [Obsolete("Use services.AddRedb(o => o.UsePostgres(connectionString)) from redb.Core.Extensions")] public static IServiceCollection AddRedbPostgresFromConfig( this IServiceCollection services, IConfiguration configuration, string sectionName = "RedbService") { var config = configuration.GetSection(sectionName).Get() ?? new RedbServiceConfiguration(); services.AddSingleton(config); // Auto-register IRedbContext if ConnectionString is provided if (!string.IsNullOrEmpty(config.ConnectionString)) { // Factory registration hands OWNERSHIP to the container: disposing the provider disposes // the NpgsqlDataSource and closes its pool. The old instance-registration form left the // pool open forever - every container restart (module hot-reload, tests) stacked another // orphaned pool of idle sessions until the server-side pruner got them (2026-09-09). services.AddSingleton(_ => Data.NpgsqlDataSourceFactory.Create(config.ConnectionString, config.StringCollation, config.EnableLazyReferences)); services.AddScoped(sp => new Data.NpgsqlRedbContext(sp.GetRequiredService())); } // Register SecurityContext (required for providers) services.AddScoped(sp => AmbientSecurityContext.GetOrCreateDefault()); // Register providers services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // The user configuration service takes its statements from the dialect services.AddSingleton(_ => new Sql.PostgreSqlDialect(config.StringCollation)); // Register user configuration service services.AddScoped(); services.AddScoped(); return services; } /// /// Registers REDB with ready configuration instance /// /// Service collection /// Ready configuration instance /// Service collection for method chaining /// /// var config = PredefinedConfigurations.Production; /// services.AddRedb(config); /// public static IServiceCollection AddRedb( this IServiceCollection services, RedbServiceConfiguration configuration) { services.AddSingleton(configuration); // Auto-register IRedbContext if ConnectionString is provided if (!string.IsNullOrEmpty(configuration.ConnectionString)) { // Factory registration hands OWNERSHIP to the container: disposing the provider disposes // the NpgsqlDataSource and closes its pool. The old instance-registration form left the // pool open forever - every container restart (module hot-reload, tests) stacked another // orphaned pool of idle sessions until the server-side pruner got them (2026-09-09). services.AddSingleton(_ => Data.NpgsqlDataSourceFactory.Create(configuration.ConnectionString, configuration.StringCollation, configuration.EnableLazyReferences)); services.AddScoped(sp => new Data.NpgsqlRedbContext(sp.GetRequiredService())); } // Register SecurityContext (required for providers) services.AddScoped(sp => AmbientSecurityContext.GetOrCreateDefault()); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); return services; } } }