Path: redb/redb.Core/RedbAmbientScope.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/RedbAmbientScope.cs ---- using System; using System.Threading; namespace redb.Core; /// /// The redb scope a lazy load runs on: a data object owns no connection, and /// or the Props of a reference stub load on the live scope of whoever /// reads them. A service makes itself current for the flow that resolved it (its constructor) and for a block /// (); a read takes the nearest live service of its database. /// /// A frame holds its service weakly and is live while the service's context is not disposed: an ended scope never /// answers, and a flow that outlives it simply finds the next live frame - or none. The old model opened a fresh scope and /// pooled connection per read wherever no loader of the right scope was attached (production reports, 2026-09-09 and 2026-09-15). /// /// internal static class RedbAmbientScope { private sealed class Frame { private readonly WeakReference _service; public Frame(RedbServiceBase service, Frame? parent) { _service = new WeakReference(service); Domain = service.CacheDomain; Parent = parent; } public string Domain { get; } public Frame? Parent { get; } public RedbServiceBase? Live => _service.TryGetTarget(out var service) && !service.IsScopeEnded ? service : null; } private sealed class Restore : IDisposable { private readonly Frame? _previous; private bool _disposed; public Restore(Frame? previous) => _previous = previous; public void Dispose() { if (_disposed) return; _disposed = true; Current.Value = _previous; } } private static readonly AsyncLocal Current = new(); /// The cache domain of the nearest live service of the calling flow; null when none is current. public static string? CurrentDomain => FirstLive(Current.Value)?.Domain; /// Makes current for the rest of the calling flow. public static void Enter(RedbServiceBase service) => Current.Value = new Frame(service, FirstLive(Current.Value)); /// Makes current for a block; disposing restores what was current before. public static IDisposable Push(RedbServiceBase service) { var previous = Current.Value; Current.Value = new Frame(service, FirstLive(previous)); return new Restore(previous); } /// /// The nearest live service of in the calling flow. With no domain - an item built in user /// code - the nearest live service, provided every live service in the flow reads the same database: an item of /// unknown origin is never read from a database it may not belong to. Null when no live redb scope reads here. /// public static RedbServiceBase? Resolve(string? domain) { RedbServiceBase? found = null; for (var frame = Current.Value; frame != null; frame = frame.Parent) { if (domain != null && frame.Domain != domain) continue; var live = frame.Live; if (live == null) continue; if (domain != null) return live; if (found == null) { found = live; continue; } if (found.CacheDomain != live.CacheDomain) throw new InvalidOperationException( "A list item that carries no database is read while live redb scopes of two databases " + $"('{found.CacheDomain}', '{live.CacheDomain}') are current, so which one it belongs to is unknown. " + "Load its object explicitly on the right service: redb.LoadAsync(item.IdObject) or " + "redb.LoadLinkedObjectsAsync(items)."); } return found; } /// /// The service behind - the scope that materialized an instance - if it still lives. Asked /// only when no scope is current for the reader, and never for a shared instance (owner decision 2026-09-15). /// public static RedbServiceBase? LiveOrigin(WeakReference? origin) => origin != null && origin.TryGetTarget(out var service) && !service.IsScopeEnded ? service : null; // A flow that creates a scope per iteration would otherwise chain every ended one behind the new frame. private static Frame? FirstLive(Frame? frame) { while (frame != null && frame.Live == null) frame = frame.Parent; return frame; } }