Path: redb/redb.Core/IRedbService.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/IRedbService.cs
----
using redb.Core.Query;
using redb.Core.Providers;
using redb.Core.Data;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using redb.Core.Models.Contracts;
using redb.Core.Models.Configuration;
namespace redb.Core
{
///
/// Main REDB service interface - composition of all providers.
///
public interface IRedbService :
ISchemeSyncProvider,
IObjectStorageProvider,
ITreeProvider,
IPermissionProvider,
IQueryableProvider,
IValidationProvider,
IRedbScopeSource
{
// === DATABASE CONTEXT ===
///
/// Database context for direct SQL queries.
/// Replaces EF Core RedbContext.
///
IRedbContext Context { get; }
// === PROVIDERS ===
///
/// Provider for user management.
///
IUserProvider UserProvider { get; }
///
/// Provider for role management.
///
[Obsolete(RedbDeprecation.PermissionsAndRoles)]
IRoleProvider RoleProvider { get; }
///
/// Provider for list management.
///
IListProvider ListProvider { get; }
///
/// Makes this service the scope lazy loads run on, for the returned block (owner decision 2026-09-15): a data
/// object owns no connection, and RedbListItem.Object or the Props of a reference stub load on the live redb
/// scope of whoever reads them. A service is current by itself in the flow that resolved it; UI event handlers
/// (Blazor Server, WebAssembly, MAUI) and callbacks that run in another execution context wrap their reads in
/// using (redb.BeginAccess()) { ... }.
///
IDisposable BeginAccess();
///
/// The block form of the query planner option
/// (): the option turns the query
/// planner on or off for the whole service, this method for the returned block only.
/// using (redb.WithQueryPlanner(false)) { ... } reads the queries inside it with the query planner off, in
/// the aggregate pivot form; true turns the query planner on, and it chooses the streams form where it can.
/// Pro only; on Free, which has no
/// planner, the block changes nothing.
///
/// Scope rules (an , as ):
/// the block holds for the flow that opened it, the awaits inside it included, and for every task started inside
/// it (parallel queries of one block all see it); concurrent requests keep the configuration. What counts is
/// where a query runs, not where it was built: an IRedbQueryable composed inside a block and enumerated
/// after it runs with the value in effect at the enumeration, while a call such as ToListAsync() made inside
/// the block keeps the block's value even if it is awaited after it. Blocks nest; disposing one restores the value
/// before it. Open the block in the method that runs the queries: a value set inside an async method does not flow
/// back to its caller.
///
///
IDisposable WithQueryPlanner(bool enabled) => redb.Core.Query.QueryPlannerScope.Unchanged;
///
/// Loads the linked objects of in one query on this service's connection, publishes them
/// on the items and returns them by id. Items already carrying their object and items without a link are skipped;
/// an object that does not exist is absent from the result and its item stays lazy.
///
Task> LoadLinkedObjectsAsync(
System.Collections.Generic.IEnumerable items, CancellationToken cancellationToken = default);
///
/// Storage maintenance: planner statistics (AnalyzeAsync after bulk writes) and
/// index health (GetIndexStatsAsync). Explicit calls only - never a side effect.
///
IMaintenanceProvider Maintenance { get; }
// === CONFIGURATION ===
///
/// Current service configuration.
///
RedbServiceConfiguration Configuration { get; }
///
/// Cache domain identifier for this service instance.
/// Used to isolate caches between different database connections.
///
string CacheDomain { get; }
///
/// Update configuration.
///
void UpdateConfiguration(Action configure);
///
/// Update configuration via builder.
///
void UpdateConfiguration(Action configureBuilder);
// === SECURITY CONTEXT ===
///
/// Security context for user and permission management.
///
IRedbSecurityContext SecurityContext { get; }
///
/// Set current user.
///
void SetCurrentUser(IRedbUser user);
///
/// Create temporary system context.
///
IDisposable CreateSystemContext();
///
/// Get effective user ID with fallback logic.
///
long GetEffectiveUserId();
// === INITIALIZATION ===
///
/// Initialize REDB system at application startup.
/// Automatically performs:
/// 1. Synchronization of all schemes with RedbSchemeAttribute
/// 2. RedbObjectFactory initialization
/// 3. AutomaticTypeRegistry initialization
/// 4. GlobalPropsCache initialization (if enabled in configuration)
/// 5. Setting global scheme provider for RedbObject
///
/// Assemblies to scan. If not specified - all loaded assemblies are scanned
Task InitializeAsync(params Assembly[] assemblies);
// === DATABASE SCHEMA MANAGEMENT ===
///
/// Ensures the REDB database schema exists.
/// If the core tables are not found, executes the full initialization script.
/// Safe to call on every startup — idempotent.
///
Task EnsureDatabaseAsync();
///
/// Returns the full SQL script that creates the REDB schema.
/// Useful for manual database setup or CI/CD pipelines.
///
string GetSchemaScript();
///
/// Returns the SQL script that brings an existing database up to the module version this
/// build requires — the same text start-up applies automatically when the connected role is
/// allowed to. Hand it to the schema owner when it is not (see
/// ). Idempotent: safe to re-run.
/// Returns null for providers that have no versioned module (SQLite upgrades itself,
/// the database file belongs to the process).
///
string? GetUpgradeScript();
///
/// Problems of the database schema that do not stop the service but need a person, as found by the last
/// , or scheme synchronization: each is logged as
/// an error when found, at every start until resolved. Empty when the schema is sound. Shared by every instance
/// of the service on one database (cache domain): a scoped instance of a health check sees what the start found
/// in its own scope. Meant for a health check (Degraded while not empty); cheap, reads no database.
///
IReadOnlyList SchemaProblems => Array.Empty();
///
/// Looks for schema problems in the database again (read only, no schema lock) and returns the current list,
/// the one holds from then on.
///
Task> CheckSchemaAsync(CancellationToken cancellationToken = default)
=> Task.FromResult(SchemaProblems);
///
/// Compares the indexes of the database with those a fresh database of this build has: the base schema, and with
/// Pro the indexes of the modules in use. Reports every expected index that is missing, INVALID (PostgreSQL, an
/// interrupted concurrent build) or defined otherwise, with what it serves. Read only; indexes the database has
/// beyond these are not reported. Also run at start-up (CheckBaseIndexesOnStart), which logs one Warning.
///
Task CheckBaseIndexesAsync(CancellationToken cancellationToken = default)
=> throw new NotSupportedException("This service does not check its indexes.");
///
/// Builds the expected indexes the check finds missing or INVALID (an INVALID one is dropped first); an index
/// defined otherwise is reported and never touched. limits it to those indexes.
/// PostgreSQL builds with CREATE INDEX CONCURRENTLY, so reads and writes go on; SQL Server with ONLINE = ON on
/// the editions that have it, and on the others builds nothing unless is set,
/// returning the statements instead (an offline build blocks writes to the table while it runs); SQLite builds
/// plainly. Runs under the schema lock. An explicit act: start-up never builds a missing base index. On SQLite,
/// before it builds a unique index on the place of a value it looks for places that hold more than one row; such an
/// index is not built, the places are listed in , nothing is
/// deleted.
///
Task RepairBaseIndexesAsync(IReadOnlyCollection? names = null,
bool allowOffline = false, CancellationToken cancellationToken = default)
=> throw new NotSupportedException("This service does not repair its indexes.");
///
/// Initialize REDB system at application startup, optionally creating the database schema first.
///
/// If true, calls before initialization.
/// If false and the database has no redb schema yet, initialization stops with
/// - creation is opt-in, never a side effect.
/// Assemblies to scan. If not specified — all loaded assemblies are scanned.
Task InitializeAsync(bool ensureCreated, params Assembly[] assemblies);
string dbVersion { get; }
///
/// Database type (e.g., "PostgreSQL").
///
string dbType { get; }
///
/// Database migration version.
///
string dbMigration { get; }
///
/// Database size in bytes, as the server reports it (optional).
///
long? dbSize { get; }
///
/// Asynchronously retrieves the database version using the provider-specific version SQL.
/// Intended for health probes and other non-blocking callers (the synchronous
/// blocks the calling thread and must not be used from request-pipeline code).
///
/// Cancellation token propagated to the underlying ADO.NET call.
/// Version string returned by the database, or null when the query yields no row.
Task GetDbVersionAsync(CancellationToken cancellationToken = default);
}
}