Path: redb/redb.Examples/Examples/ExampleBase.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.Examples/Examples/ExampleBase.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Attribute to mark example classes with metadata.
/// Used for parsing examples into ExampleProps for website.
///
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public sealed class ExampleMetaAttribute(
string id,
string title,
string category,
ExampleTier tier = ExampleTier.Free,
int difficulty = 1,
params string[] tags) : Attribute
{
/// Unique example ID (E001, E010).
public string Id { get; } = id;
/// Example title for display.
public string Title { get; } = title;
/// Category for navigation (CRUD, Query, Trees, etc).
public string Category { get; } = category;
/// License tier (Free, Pro, Enterprise).
public ExampleTier Tier { get; } = tier;
/// Difficulty level 1-5.
public int Difficulty { get; } = difficulty;
/// Tags for filtering (SaveAsync, Where, etc).
public string[] Tags { get; } = tags;
/// Display order on website (set via property).
public int Order { get; set; }
/// Related API methods for linking to documentation (e.g., "IRedbService.Query", "ITreeQueryable.WhereRoots").
public string[] RelatedApis { get; set; } = [];
}
///
/// Base class for all examples.
///
public abstract class ExampleBase
{
///
/// The example ids named on the command line, uppercased; null when the whole suite runs.
/// Set by Program before any example executes. Lets an example distinguish "I was asked
/// for by name" from "the sweep reached me" - E203 seeds only in the former case.
///
public static IReadOnlySet? ExplicitlyRequestedIds { get; set; }
///
/// Run the example and return result.
///
public abstract Task RunAsync(IRedbService redb);
///
/// Get metadata from attribute.
///
public ExampleMetaAttribute? GetMeta()
{
return GetType().GetCustomAttributes(typeof(ExampleMetaAttribute), false)
.FirstOrDefault() as ExampleMetaAttribute;
}
///
/// Helper to create success result with count.
///
protected static ExampleResult Ok(string id, string title, ExampleTier tier, long elapsedMs, int count, string[] output, string? sql = null)
{
return new ExampleResult
{
Id = id,
Title = title,
Tier = tier,
Success = true,
ElapsedMs = elapsedMs,
Count = count,
Output = output,
Sql = sql
};
}
///
/// Helper to create success result without count (for setup/CRUD).
///
protected static ExampleResult Ok(string id, string title, ExampleTier tier, long elapsedMs, string[] output, string? sql = null)
{
return new ExampleResult
{
Id = id,
Title = title,
Tier = tier,
Success = true,
ElapsedMs = elapsedMs,
Output = output,
Sql = sql
};
}
///
/// Helper to create failure result.
///
protected static ExampleResult Fail(string id, string title, ExampleTier tier, long elapsedMs, string error)
{
return new ExampleResult
{
Id = id,
Title = title,
Tier = tier,
Success = false,
ElapsedMs = elapsedMs,
Output = [],
Error = error
};
}
}