Path: redb/redb.Core/Providers/AmbientLazyPropsLoader.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/Providers/AmbientLazyPropsLoader.cs ---- using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using redb.Core.Exceptions; using redb.Core.Models.Entities; namespace redb.Core.Providers; /// /// The loader every reference stub carries. It owns no scope and no connection. A load runs on, in this order: /// /// the live redb scope current for the reader (the flow that resolved it, or BeginAccess); /// the scope that materialized the stub (its origin, held weakly), while that scope lives - never for a shared /// instance of a cache, whose stubs carry the origin-less loader of their database; /// otherwise it refuses with , or opens a fresh scope when the /// configuration asks for it. /// /// It replaces the scope-bound loaders on stubs, the detached loader of the props cache and its dead-scope fallback: those /// rode the connection of the materializing scope whoever read, or rented a fresh scope per load. /// internal sealed class AmbientLazyPropsLoader : ILazyPropsLoader { private static readonly ConcurrentDictionary ByDomain = new(); private static readonly ConditionalWeakTable ByOrigin = new(); private readonly string _domain; private AmbientLazyPropsLoader(string domain, RedbServiceBase? origin) { _domain = domain; Origin = origin?.SelfReference; } /// The loader of for a shared instance: the reader's scope only. public static AmbientLazyPropsLoader For(string domain) => ByDomain.GetOrAdd(domain, d => new AmbientLazyPropsLoader(d, null)); /// /// The loader of an instance materialized by : the reader's scope, else the origin while it /// lives. Without an origin, the shared loader of the database. /// public static AmbientLazyPropsLoader For(string domain, RedbServiceBase? origin) => origin == null ? For(domain) : ByOrigin.GetValue(origin, o => new AmbientLazyPropsLoader(domain, o)); /// The origin-less loader of the same database, for an instance that became shared. public AmbientLazyPropsLoader Shared => Origin == null ? this : For(_domain); /// The service that materialized the instances carrying this loader; null for a shared loader. public WeakReference? Origin { get; } /// public string? CacheDomain => _domain; private RedbServiceBase? Reader() => RedbAmbientScope.Resolve(_domain) ?? RedbAmbientScope.LiveOrigin(Origin); /// /// Whether a shared instance keeps what a load on the reader makes now: outside a transaction, or inside one that has /// written nothing yet - what it reads is committed state. Once the reader's transaction has written, what it reads /// may be its own uncommitted work, and a shared instance keeps nothing of it (owner decisions 2026-09-15, 2026-09-17). /// public bool ReaderKeepsLoads => Reader() is not { } reader || reader.LendsScope || Data.TransactionWrites.ReadsCommitted(reader.Context); /// Runs when the reader's transaction ends; false when the reader is in none. public bool OnReaderTransactionCompleted(Action onCompleted) => Reader() is { LendsScope: false } reader && Data.TransactionHooks.OnCompleted(reader.Context, onCompleted); public TProps? LoadProps(long objectId, long schemeId) where TProps : class, new() { var reader = Reader(); // A captive reader (a service of the root provider, outside a transaction) lends its scope factory, never its one connection. if (reader is { LendsScope: true } captive) return RedbDomainRegistry.InScopeOf(captive, service => service.LazyPropsLoader.LoadProps(objectId, schemeId)); return reader != null ? reader.LazyPropsLoader.LoadProps(objectId, schemeId) : RedbDomainRegistry.InFreshScope(_domain, service => service.LazyPropsLoader.LoadProps(objectId, schemeId), () => new RedbLazyLoadScopeEndedException(objectId, schemeId)); } public async Task LoadPropsAsync(long objectId, long schemeId, CancellationToken cancellationToken = default) where TProps : class, new() { var reader = Reader(); if (reader is { LendsScope: true } captive) return await RedbDomainRegistry.InScopeOfAsync(captive, service => service.LazyPropsLoader.LoadPropsAsync(objectId, schemeId, cancellationToken)).ConfigureAwait(false); return reader != null ? await reader.LazyPropsLoader.LoadPropsAsync(objectId, schemeId, cancellationToken).ConfigureAwait(false) : await RedbDomainRegistry.InFreshScopeAsync(_domain, service => service.LazyPropsLoader.LoadPropsAsync(objectId, schemeId, cancellationToken), () => new RedbLazyLoadScopeEndedException(objectId, schemeId)).ConfigureAwait(false); } public Task LoadPropsForManyAsync(List> objects, CancellationToken cancellationToken = default) where TProps : class, new() => ForManyAsync(objects, (loader, list) => loader.LoadPropsForManyAsync(list, cancellationToken)); public Task LoadPropsForManyAsync(List> objects, HashSet? projectedStructureIds, CancellationToken cancellationToken = default) where TProps : class, new() => ForManyAsync(objects, (loader, list) => loader.LoadPropsForManyAsync(list, projectedStructureIds, cancellationToken)); public Task LoadPropsForManyAsync(List> objects, int? propsDepth, CancellationToken cancellationToken = default) where TProps : class, new() => ForManyAsync(objects, (loader, list) => loader.LoadPropsForManyAsync(list, propsDepth, cancellationToken)); public Task LoadPropsForManyAsync(List> objects, HashSet? projectedStructureIds, int? propsDepth, CancellationToken cancellationToken = default) where TProps : class, new() => ForManyAsync(objects, (loader, list) => loader.LoadPropsForManyAsync(list, projectedStructureIds, propsDepth, cancellationToken)); private async Task ForManyAsync(List> objects, Func>, Task> load) where TProps : class, new() { if (objects.Count == 0) return; var reader = Reader(); if (reader is { LendsScope: true } captive) { await RedbDomainRegistry.InScopeOfAsync(captive, async service => { await load(service.LazyPropsLoader, objects).ConfigureAwait(false); return true; }).ConfigureAwait(false); return; } if (reader != null) { await load(reader.LazyPropsLoader, objects).ConfigureAwait(false); return; } await RedbDomainRegistry.InFreshScopeAsync(_domain, async service => { await load(service.LazyPropsLoader, objects).ConfigureAwait(false); return true; }, () => new RedbLazyLoadScopeEndedException(objects[0].id, objects[0].scheme_id)).ConfigureAwait(false); } }