Examples

Custom seed

A plain record seed, an interface default, save hooks, frozen options and parallel safety.

Sometimes the shared context is an actor and tenant, rather than rows to save. This example uses a plain record seed and shows how interface defaults and save hooks carry those conventions into the graph.

The seed doesn't have to be your app's entities. Here it is a plain record, CustomSeed(string Actor, int Tenant), and ownership comes from a global default on an interface. Source: tests/Zeeq.Biome.Tests/CustomSeed.

Seed, interface default, blueprint

CustomSeed/Domain.cs
// Use a plain record because the actor and tenant are session context.
internal sealed record CustomSeed(string Actor, int Tenant);

internal interface IHasCreatedBy
{
    string? CreatedBy { get; set; }
}

internal sealed class Note : IHasCreatedBy
{
    public string? CreatedBy { get; set; }
    public string? Id { get; set; }
    public int Number { get; set; }
    public string Text { get; set; } = "note";
}

// Continue numbering across root Add calls in the same session.
internal static readonly Sequence<int> Numbers = Sequence.Of(1, n => n + 1).PerSession();

// Leave ownership unset so the interface default can fill it from the seed.
internal static readonly Blueprint<CustomSeed, Note> Note = Blueprint
    .For<CustomSeed, Note>(ctx => new() { Id = ctx.NewId("note") })
    .Derive(n => n.Number, Numbers);

// Share the seed actor across implementations and preserve explicit owners.
internal static BiomeOptions<CustomSeed> NewOptions() =>
    new BiomeOptions<CustomSeed>().Default<IHasCreatedBy, string?>(
        n => n.CreatedBy,
        (_, ctx) => ctx.Seed.Actor
    );

Defaults and hooks together

The test registers an audit row in BeforeSave, then records the order in which things happen. An explicit CreatedBy still wins over the interface default.

// Record when the store runs so the test can check the order of both hooks.
var events = new List<string>();
var store = new RecordingStore { OnSave = () => events.Add("save") };
var options = TestBiome
    .NewOptions()
    .BeforeSave(
        (ctx, _) =>
        {
            events.Add("before");
            // Audit the seed actor in the same save as the notes.
            ctx.Entities.Add(new Audit(ctx.Seed.Actor));
            return ValueTask.CompletedTask;
        }
    )
    .AfterSave(
        (_, _) =>
        {
            events.Add("after");
            return ValueTask.CompletedTask;
        }
    );
var biome = TestBiome.Start(out var seed, store, options);

await biome
    // Leave the first note unset and give the second its own owner to check both paths.
    .AddNotes(out var notes, _ => { }, n => n.CreatedBy = "custom")
    .BuildAsync();

// notes[0].CreatedBy == seed.Actor; notes[1].CreatedBy == "custom"
// events: "before,save,after"; the Audit row reached the store

Options freeze on first use

Every configuration mutator throws once a session has started, so a running test can't change shared options:

CustomSeedTests.cs
var options = TestBiome.NewOptions();

// Start a session to freeze the options it now shares with later sessions.
TestBiome.Start(out _, options: options);

// Each mutation fails so it cannot change the running session's configuration.
Assert.Throws<InvalidOperationException>(() => options.Ids(p => p));
Assert.Throws<InvalidOperationException>(() => options.TimePrecision(TimeSpan.Zero));
Assert.Throws<InvalidOperationException>(() => options.SeedEntities(_ => []));
// …Default, BeforeSave and AfterSave throw the same way

Parallel-safe by construction

Blueprints, sequences and options are immutable and shared. Sessions hold all the mutable state, so 32 concurrent scenarios don't interfere:

CustomSeedTests.cs
var options = TestBiome.NewOptions();

await Parallel.ForEachAsync(
    Enumerable.Range(0, 32),
    new ParallelOptions { MaxDegreeOfParallelism = 8 },
    async (tenant, ct) =>
    {
        await Task.Yield();
        // Share the options while giving each tenant its own actor and sequence state.
        var seed = new CustomSeed($"actor-{tenant}", tenant);
        var biome = Biome.Start(seed, options);
        await biome.AddNotes(out var notes, 3).BuildAsync(ct);
        // Each session starts at Number 1, keeps its own actor, and gets distinct IDs.
    }
);
This also shows that Biome.Start(seed, options) works without a TestBiome wrapper, handy for a custom seed built inline.

Custom IDs and session sequences

CustomSeedTests.cs
// Use a predictable ID to show that the factory picks up the configured generator.
var options = TestBiome.NewOptions().Ids(prefix => $"{prefix}_known");

await TestBiome
    .Start(out _, options: options)
    // Use separate root adds to show that the note sequence continues in this session.
    .AddNote(out var first)
    .AddNotes(out var rest, 2)
    .BuildAsync();

// first.Number == 1; rest[0].Number == 2; first.Id == "note_known"