Path: redb/redb.Core/Providers/Base/ListProviderBase.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/Base/ListProviderBase.cs
----
using redb.Core.Attributes;
using redb.Core.Caching;
using redb.Core.Data;
using redb.Core.Models.Configuration;
using redb.Core.Models.Contracts;
using redb.Core.Models.Entities;
using redb.Core.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace redb.Core.Providers.Base
{
///
/// Base class for IListProvider implementations.
/// Contains all business logic for list and list item operations with caching.
/// SQL queries are abstracted via ISqlDialect.
///
public abstract class ListProviderBase : IListProvider
{
protected readonly IRedbContext Context;
protected readonly RedbServiceConfiguration Configuration;
protected readonly ISqlDialect Sql;
protected readonly ILogger? Logger;
protected readonly GlobalListCache ListCache;
///
public Func, CancellationToken, Task>>? LinkedObjectsBatchLoader { get; set; }
///
/// Hand-out preload (PreloadListItemLinkedObjects, default on): resolve the linked
/// objects of the items in ONE batch and publish them, so a later touch of
/// is a field read - no database call, no blocked
/// thread (a hot loop over fresh lazy items froze a production process, 2026-09-09).
/// Items already carrying their object are skipped; a missing id stays lazy.
///
private async Task PreloadLinkedObjectsAsync(IReadOnlyList items, CancellationToken cancellationToken)
{
var batchLoader = LinkedObjectsBatchLoader;
if (batchLoader == null || !Configuration.PreloadListItemLinkedObjects) return;
var ids = new List();
foreach (var item in items)
if (item is { IsObjectLoaded: false, IdObject: long id })
ids.Add(id);
if (ids.Count == 0) return;
var byId = await batchLoader(ids, cancellationToken);
foreach (var item in items)
if (!item.IsObjectLoaded && item.IdObject is long id && byId.TryGetValue(id, out var obj))
item.Publish(obj, Context);
}
///
/// Binds the items to this provider's database, and a not-shared item to this scope as its origin, before they leave
/// it. The item keeps no loader: loads on the scope current for the reader, else on
/// the origin while it lives (owner decision 2026-09-15). An item of the list cache is shared and has no origin.
///
protected List Attach(List items)
{
foreach (var item in items)
Bind(item);
return items;
}
///
protected RedbListItem? Attach(RedbListItem? item)
{
if (item != null)
Bind(item);
return item;
}
private void Bind(RedbListItem item)
{
item._cacheDomain ??= ListCache.Domain;
if (!item._isShared)
item._origin ??= RedbServiceBase.ServiceOf(Context)?.AsOrigin.SelfReference;
}
protected ListProviderBase(
IRedbContext context,
RedbServiceConfiguration configuration,
ISqlDialect sql,
ISchemeSyncProvider schemeSync,
ILogger? logger = null)
{
Context = context ?? throw new ArgumentNullException(nameof(context));
Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
Sql = sql ?? throw new ArgumentNullException(nameof(sql));
ListCache = schemeSync?.ListCache ?? throw new ArgumentNullException(nameof(schemeSync));
Logger = logger;
}
// === CRUD for lists ===
public async Task GetListAsync(long listId, CancellationToken cancellationToken = default)
{
var cached = ListCache.GetList(listId);
if (cached != null) return cached;
var list = await Context.QueryFirstOrDefaultAsync(Sql.Lists_SelectById(), new object[] { listId }, cancellationToken);
if (list == null) return null;
ListCache.CacheList(list);
return list;
}
public async Task GetListByNameAsync(string name, CancellationToken cancellationToken = default)
{
var cached = ListCache.GetListByName(name);
if (cached != null) return cached;
var list = await Context.QueryFirstOrDefaultAsync(Sql.Lists_SelectByName(), new object[] { name }, cancellationToken);
if (list == null) return null;
ListCache.CacheList(list);
return list;
}
public async Task> GetAllListsAsync(CancellationToken cancellationToken = default)
{
var lists = await Context.QueryAsync(Sql.Lists_SelectAll(), System.Array.Empty