Path: redb/redb.Core/Attributes/RedbSchemeAttribute.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/Attributes/RedbSchemeAttribute.cs
----
using System;
namespace redb.Core.Attributes
{
///
/// Attribute for configuring the REDB schema for a properties class.
///
/// By default the schema is named after the CLR type's . Set
/// to pin an explicit name instead, decoupling the database identity of the
/// schema from C# namespace/class refactoring.
///
///
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class RedbSchemeAttribute : Attribute
{
///
/// Explicit schema name in the database. When null or empty, the schema is named after the
/// CLR type's — the legacy default.
///
/// Setting this on an existing project renames the schema in the database on the next
/// sync: the scheme row is looked up by the explicit name, then by FullName, then by the short
/// type name, and the first match is renamed in place (the scheme id, and therefore all objects
/// and structures, are untouched). Because an older binary would not find the scheme under its
/// new name and would create a second one, every consumer of the database must be updated
/// together.
///
///
/// Must follow C# identifier rules — Latin letters, digits, _, . and +,
/// no reserved words, 128 characters max. Validated before any SQL is issued; use
/// for human-readable titles.
///
///
/// Multi-node note. The name (and alias) live in this attribute — in code — so changing
/// them means a redeploy, which restarts every node and rebuilds each process's static scheme
/// caches fresh. Cross-node consistency is achieved by that restart; there is no online rename
/// on live nodes without restart, and it is not supported.
///
///
public string? Name { get; set; }
///
/// Schema alias (human-readable name). Never used as the schema name — it is free-form and may
/// contain spaces, punctuation and non-Latin letters. Synchronised from this attribute on every
/// sync: changing it updates the database, removing it sets the alias back to null.
///
public string? Alias { get; set; }
///
/// Previous names of the schema, oldest first, so that the sync renames the stored schema instead of creating a
/// new one and leaving every object under the old one.
///
/// The sync looks the schema up by its name first; when it is not there, by these names, and renames the first
/// one found in place (the schema id, and with it every object and structure, stays). Use it when an explicit
/// changes (RenamedFrom = ["orders"]), or when a class without an explicit name is
/// renamed or moved to another namespace (its previous FullName). When the schema name and a previous one are
/// both stored, the sync stops with .
///
///
/// The previous names are kept in the database (_schemes._renamed_from): a node that still runs a class
/// with a previous name stops at its sync with instead of
/// creating the old schema again.
///
///
public string[]? RenamedFrom { get; set; }
///
/// The version of this Props class, raised by the programmer with a change that takes data away: a property
/// removed, or a property's type changed (and with a change inside a nested class of the scheme, which belongs
/// to it). A class without the attribute, or without this value, has version 0.
///
/// The sync records the version in the database (_schemes._version) and deletes a structure or
/// changes its type only for a class whose version is higher than the recorded one. A class of the same
/// version adds what it lacks and leaves the rest as it is, with a warning; a class of a lower version (an
/// older build of the application next to a newer one) changes nothing. So a node that still runs an older
/// build cannot take the fields or the types of a newer build away. The version recorded never goes down.
///
///
/// Adding a property needs no new version.
/// (on in the Development profile) lets a class of the same version delete and change too, for a local
/// database during development.
///
///
public int Version { get; set; }
///
/// Parameterless constructor
///
public RedbSchemeAttribute()
{
}
///
/// Constructor with alias. The positional argument is always the alias, never the schema
/// name — an explicit name can only be set through the named parameter, so
/// existing declarations cannot change meaning by accident.
///
/// Schema alias
public RedbSchemeAttribute(string alias)
{
Alias = alias ?? throw new ArgumentNullException(nameof(alias));
}
///
/// Get the schema name for the type: the explicit when set, otherwise the
/// full class name (namespace.class).
///
/// Class type
/// Schema name
///
/// Deliberately does no validation — this sits on hot paths (assembly rescan, cache warm-up,
/// every sync). Explicit names are validated by
/// at the points that write to the database.
///
public string GetSchemeName(Type type)
{
return !string.IsNullOrWhiteSpace(Name)
? Name!
: type.FullName ?? type.Name;
}
}
}