Path: redb/redb.Core/Models/Entities/RedbListItem.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/Models/Entities/RedbListItem.cs ---- using redb.Core.Attributes; using redb.Core.Exceptions; using redb.Core.Models.Contracts; using System; using System.Text.Json.Serialization; using System.Threading; using System.Threading.Tasks; namespace redb.Core.Models.Entities { /// /// REDB list item entity with direct data storage. /// Maps to _list_items table in PostgreSQL. /// public class RedbListItem : IRedbListItem { /// /// Unique item identifier. /// [JsonPropertyName("id")] public long Id { get; set; } /// /// List identifier this item belongs to. /// [JsonPropertyName("id_list")] public long IdList { get; set; } /// /// Item value. /// [JsonPropertyName("value")] public string Value { get; set; } = string.Empty; /// /// Linked object identifier (optional). /// [JsonPropertyName("id_object")] public long? IdObject { get; set; } /// /// Item alias (display name). /// [JsonPropertyName("alias")] public string? Alias { get; set; } // === Linked object: loaded on the reader's scope === // A data object owns no connection. // The item carries no loader delegate - it remembers only the database it came from - and Object loads on the live // redb scope of whoever reads it. The delegates it carried before (a per-item loader over the handing-out scope, a // process-wide fallback) opened a fresh scope and pooled connection per read wherever the right one was missing. // The database (cache domain) the item was materialized from; null for an item built in user code. internal string? _cacheDomain; // Set by the list cache and the props cache: the instance is shared by every scope. internal bool _isShared; // The service that materialized the item, held weakly; asked when no scope is current for the reader, while it // lives. A shared item has none. internal WeakReference? _origin; // The scope current for the reader, else the materializing scope while it lives (owner decision 2026-09-15). private RedbServiceBase? Reader() => RedbAmbientScope.Resolve(_cacheDomain) ?? RedbAmbientScope.LiveOrigin(_origin); private IRedbObject? _object = null; private bool _objectLoaded = false; private readonly object _lazyLoadLock = new(); /// Whether the linked object has been resolved (set, preloaded or lazily loaded). [JsonIgnore] public bool IsObjectLoaded => _objectLoaded; /// The object this item already carries, without loading; null when none is loaded. internal IRedbObject? LoadedObject => _objectLoaded ? _object : null; /// /// Makes this item, a key of a grouping that carried only the id, the item is: its /// fields, the database it came from and its linked object when loaded. See ListItemGroupKeys. /// internal void TakeFrom(RedbListItem source) { Id = source.Id; IdList = source.IdList; Value = source.Value; IdObject = source.IdObject; Alias = source.Alias; _cacheDomain = source._cacheDomain; _origin = source._origin; if (source._objectLoaded) { _object = source._object; _objectLoaded = true; } } /// /// Asynchronous form of : resolves the linked object on the reader's scope without blocking a /// thread. Prefer this - or redb.LoadLinkedObjectsAsync(items) for many items, one query - on hot paths. /// public async Task GetObjectAsync(CancellationToken cancellationToken = default) { if (_objectLoaded) return _object; if (!IdObject.HasValue) return _object; var objectId = IdObject.Value; 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 Publish(await RedbDomainRegistry.InScopeOfAsync(captive, service => service.LoadLinkedObjectAsync(objectId, cancellationToken)).ConfigureAwait(false), readerContext: null); if (reader == null) return Publish(await RedbDomainRegistry.InFreshScopeAsync(_cacheDomain, service => service.LoadLinkedObjectAsync(objectId, cancellationToken), Refusal).ConfigureAwait(false), readerContext: null); var loaded = await reader.LoadLinkedObjectAsync(objectId, cancellationToken).ConfigureAwait(false); return Publish(loaded, reader.Context); } /// /// Linked object, loaded lazily on the live redb scope of whoever reads it. Reading it may hit the database; it is /// deliberately invisible to System.Text.Json so that serializing an item (an HTTP response, an audit record) never /// turns into a load. With no live scope reading here the getter refuses with /// (see RedbServiceConfiguration.LazyLoadWithoutScope). /// [JsonIgnore] [RedbIgnore] public IRedbObject? Object { get { if (_objectLoaded) return _object; if (!IdObject.HasValue) return _object; // The freeze anatomy this shape avoids (a production report, 2026-09-09, reproduced locally): items are shared // through the static list cache, so a lock held ACROSS the load parked every toucher of the item // process-wide behind the first one; and Task.Run needed a second pool thread per touch. So: load OUTSIDE // any lock and on the calling thread, down to ADO.NET; concurrent touchers proceed independently (a racing // duplicate load is harmless: the first published result wins). var objectId = IdObject.Value; var reader = Reader(); if (reader is { LendsScope: true } captive) return Publish(RedbDomainRegistry.InScopeOf(captive, service => service.LoadLinkedObjectSync(objectId)), readerContext: null); if (reader == null) return Publish(RedbDomainRegistry.InFreshScope(_cacheDomain, service => service.LoadLinkedObjectSync(objectId), Refusal), readerContext: null); var loaded = reader.LoadLinkedObjectSync(objectId); return Publish(loaded, reader.Context); } set { _object = value; _objectLoaded = true; IdObject = value?.Id > 0 ? value.Id : null; } } private Exception Refusal() => RedbLazyLoadScopeEndedException.ForListItem(Id, IdObject ?? 0); /// /// Publishes a loaded object on the item and returns what readers get. A shared instance does not keep what one /// transaction saw - the write behind it may roll back (owner decision 2026-09-15); the reader still gets it. /// internal IRedbObject? Publish(IRedbObject? loaded, Data.IRedbContext? readerContext) { // A shared item keeps what a transaction loads while that transaction has written nothing - committed state - // and forgets it if the transaction rolls back; once the transaction has written, it keeps nothing (owner // decisions 2026-09-15, 2026-09-17). The reader still gets it. if (_isShared && readerContext != null && !Data.TransactionWrites.ReadsCommitted(readerContext)) return loaded; // What a shared item keeps is shared too: its references and items load on the reader's scope only, never on // the scope that happened to load it. Marked before publishing, outside the lock; a racing duplicate that // loses is marked for nothing. if (_isShared && loaded != null) global::redb.Core.Utils.LazyReferenceInstaller.MarkShared(loaded); var kept = false; lock (_lazyLoadLock) { if (!_objectLoaded) { _object = loaded; _objectLoaded = true; kept = true; } } if (kept && _isShared && readerContext != null) Data.TransactionHooks.OnCompleted(readerContext, committed => { if (!committed) Forget(loaded); }); return _object; } // What a rolled-back transaction loaded into a shared item is dropped: the next read loads again. private void Forget(IRedbObject? loaded) { lock (_lazyLoadLock) { if (!_objectLoaded || !ReferenceEquals(_object, loaded)) return; _object = null; _objectLoaded = false; } } /// /// Check if item is object reference. /// [JsonIgnore] public bool IsObjectReference => IdObject.HasValue; // === Constructors === /// /// Default constructor for deserialization and mapping. /// public RedbListItem() { } /// /// Constructor for creating item linked to list. /// public RedbListItem(IRedbList list, string? value, string? alias = null, long? idObject = null) { if (list == null) throw new ArgumentNullException(nameof(list)); IdList = list.Id; Value = value; Alias = alias; IdObject = idObject; } /// /// Constructor for creating item with linked object. /// public RedbListItem(IRedbList list, string? value, string? alias, IRedbObject linkedObject) { if (list == null) throw new ArgumentNullException(nameof(list)); if (linkedObject == null) throw new ArgumentNullException(nameof(linkedObject)); IdList = list.Id; Value = value; Alias = alias; IdObject = linkedObject.Id > 0 ? linkedObject.Id : null; _object = linkedObject; _objectLoaded = true; } // === Static factory methods === /// /// Create ListItem for specific list. /// public static RedbListItem ForList(IRedbList list, string? value, string? alias = null, long? idObject = null) { return new RedbListItem(list, value, alias, idObject); } /// /// Create ListItem with linked object. /// public static RedbListItem ForList(IRedbList list, string? value, string? alias, IRedbObject linkedObject) { return new RedbListItem(list, value, alias, linkedObject); } /// /// Get display value. /// public string GetDisplayValue() { if (!string.IsNullOrEmpty(Alias)) return Alias; if (!string.IsNullOrEmpty(Value)) return Value; if (IdObject.HasValue) return $"Object #{IdObject}"; return $"Item #{Id}"; } public override string ToString() { var displayValue = GetDisplayValue(); var aliasStr = !string.IsNullOrEmpty(Alias) && Alias != displayValue ? $" ({Alias})" : ""; return $"ListItem {Id}: {displayValue}{aliasStr} [List: {IdList}]"; } } }