Path: redb/redb.Core/Extensions/RedbOptionsBuilder.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.Core/Extensions/RedbOptionsBuilder.cs
----
using Microsoft.Extensions.DependencyInjection;
using redb.Core.Models.Configuration;
using redb.Core.Services;
namespace redb.Core.Extensions;
///
/// Infrastructure interface for database providers and Pro extensions.
/// Similar to EF Core's IDbContextOptionsBuilderInfrastructure.
/// Used by UsePostgres(), UseMsSql(), AddRedbPro(), etc.
///
public interface IRedbOptionsBuilderInfrastructure
{
///
/// Service collection for registering services.
///
IServiceCollection Services { get; }
///
/// Configuration instance to be populated by provider.
///
RedbServiceConfiguration Configuration { get; }
///
/// Whether Pro version is being configured.
/// Used by AddRedbPro() to mark Pro mode.
///
bool IsPro { get; set; }
///
/// Kept for source compatibility. redb Pro needs no key: every Pro feature is on without one, and a value given here
/// unlocks nothing.
///
string? LicenseKey { get; set; }
///
/// Register provider-specific services.
/// Called by UsePostgres(), UseMsSql(), etc.
///
/// Action that registers provider services.
/// Parameters: services, configuration, isPro
void SetProviderRegistration(Action registration);
///
/// Finalize configuration and register all services.
/// Called after UseXxx() and Configure() methods.
///
void Build();
}
///
/// Builder for configuring REDB services.
/// Database providers (UsePostgres, UseMsSql) register their implementations via IRedbOptionsBuilderInfrastructure.
///
public class RedbOptionsBuilder : IRedbOptionsBuilderInfrastructure
{
private readonly IServiceCollection _services;
private readonly RedbServiceConfiguration _configuration = new();
private Action? _providerRegistration;
private bool _isPro;
private string? _licenseKey;
public RedbOptionsBuilder(IServiceCollection services)
{
_services = services;
}
///
/// Kept for source compatibility. redb Pro needs no key: every Pro feature is on without one, and a value given here
/// unlocks nothing.
///
/// Kept for source compatibility; unlocks nothing.
/// Builder for chaining
public RedbOptionsBuilder WithLicense(string licenseKey)
{
_licenseKey = licenseKey;
return this;
}
///
/// Configure RedbServiceConfiguration options.
///
///
/// builder.Configure(c => {
/// c.EnablePropsCache = true;
/// c.PropsSaveStrategy = PropsSaveStrategy.ChangeTracking;
/// });
///
public RedbOptionsBuilder Configure(Action configure)
{
configure(_configuration);
return this;
}
// === IRedbOptionsBuilderInfrastructure explicit implementation ===
// Available to providers/Pro via cast: ((IRedbOptionsBuilderInfrastructure)builder)
IServiceCollection IRedbOptionsBuilderInfrastructure.Services => _services;
RedbServiceConfiguration IRedbOptionsBuilderInfrastructure.Configuration => _configuration;
bool IRedbOptionsBuilderInfrastructure.IsPro
{
get => _isPro;
set => _isPro = value;
}
string? IRedbOptionsBuilderInfrastructure.LicenseKey
{
get => _licenseKey;
set => _licenseKey = value;
}
void IRedbOptionsBuilderInfrastructure.SetProviderRegistration(
Action registration)
{
if (_providerRegistration != null)
throw new InvalidOperationException(
"Database provider already configured. Call UsePostgres/UseMsSql only once.");
_providerRegistration = registration;
}
void IRedbOptionsBuilderInfrastructure.Build()
{
if (_providerRegistration == null)
throw new InvalidOperationException(
"No database provider configured. Call UsePostgres() or UseMsSql() in the options builder.");
// Register configuration
_services.AddSingleton(_configuration);
// Default password hasher: bcrypt, DI-overridable. TryAdd so a user registration made
// before AddRedb wins. Never SimplePasswordHasher here - a bare deployment must not
// fall back to salted SHA256 (external security report, 2026-09-08).
Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions
.TryAddSingleton(_services);
// Save interceptors subscribed at run time. TryAdd: a registry put in before AddRedb is kept, so containers
// that receive the same instance (a host and the named instances it builds) share one set of subscriptions.
Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions
.TryAddSingleton(_services);
// Schema init hosted service — must be registered BEFORE provider services
// so it runs before BackgroundDeletionService that queries _objects table
_services.AddHostedService();
// Register provider-specific services
_providerRegistration(_services, _configuration, _isPro);
}
}