Generated grafts and scenario helpers
Generated graft methods
Calling the core API with a blueprint works, but repeating the blueprint name adds noise to every test. Mark a blueprint catalog with [BiomeGraft]; the generator adds a {Catalog}Grafts extension class in the same namespace. The Zeeq.Biome project references the generator as an analyzer and includes it when the core project is packaged.
The catalog below uses the district entities from the quick start. Here the class blueprint is named SchoolClass, and the graft uses the shorter name AddClass.
// Generate the Add methods from the catalog so they stay in sync.
[BiomeGraft]
public static class Blueprints
{
// Factory bodies are omitted here.
public static readonly Blueprint<SchoolSeed, District> District = null!;
// Use the shorter domain name at the call site.
[Graft("Class", Plural = "Classes")]
public static readonly ChildBlueprint<SchoolSeed, School, SchoolClass> SchoolClass = null!;
// Keep this variant available for explicit blueprint calls only.
[NoGraft]
public static readonly Blueprint<SchoolSeed, District> ArchivedDistrict = null!;
}
[BiomeGraft] marks a class; [Graft] and [NoGraft] apply to fields or properties. The catalog must be a top-level, non-generic, non-file-local static class; partial is not required. The generator reads public, internal, or protected-internal non-const static readonly fields and static getter-only properties with no setter, whose types are Blueprint, InputBlueprint, ChildBlueprint, or ChildInputBlueprint. Traits, sequences, private members, mutable fields, and other types are skipped. [NoGraft] excludes a member before name and collision diagnostics.
By default the field or property name supplies the singular suffix after Add. Pluralization changes a consonant + y to ies, adds es after s, x, z, ch or sh, and adds s otherwise. [Graft] accepts an optional positional name and an optional settable Plural argument to override either suffix for a domain word, an irregular plural, or a method collision. Both names must be valid C# identifiers. [Graft] changes names only; it does not make an unsupported member eligible.
Here are the root overloads and the singular child overload, with the class's school anchor visible in its receiver.
public static class BlueprintsGrafts
{
// A singular call gives the test one district to use directly.
public static BiomeBuilder<SchoolSeed, District[]> AddDistrict<TAnchor>(
this BiomeBuilder<SchoolSeed, TAnchor> b,
out District district,
Action<District>? customize = null
) => b.AddOne(Blueprints.District, out district, customize);
// Use a count when each district can use the same blueprint defaults.
public static BiomeBuilder<SchoolSeed, District[]> AddDistricts<TAnchor>(
this BiomeBuilder<SchoolSeed, TAnchor> b,
out District[] districts,
int count
) => b.Add(Blueprints.District, out districts, count);
// Use one customizer per district when the rows need different values.
public static BiomeBuilder<SchoolSeed, District[]> AddDistricts<TAnchor>(
this BiomeBuilder<SchoolSeed, TAnchor> b,
out District[] districts,
params Action<District>[] customize
) => b.Add(Blueprints.District, out districts, customize);
}
public static class BlueprintsGrafts
{
public static BiomeBuilder<SchoolSeed, SchoolClass[]> AddClass(
this BiomeBuilder<SchoolSeed, School[]> b, // The receiver limits this graft to school anchors.
out SchoolClass schoolClass,
Action<SchoolClass>? customize = null
) => b.AddOne(Blueprints.SchoolClass, out schoolClass, customize);
}
Generator diagnostics
These rules are enabled by default:
| ID | Severity | Meaning |
|---|---|---|
BIOME001 | Warning | Two members produce an overlapping signature on the same builder. Rename one with [Graft("Name")]. |
BIOME002 | Warning | The singular and plural suffixes are identical. Set a distinct Plural value. |
BIOME003 | Error | The catalog is not a top-level, non-generic, non-file-local static class. |
BIOME004 | Error | A singular or plural name is not a valid C# identifier. |
The generated class follows the catalog's accessibility, and each generated method follows its member's accessibility.
Every eligible blueprint catalog member with valid names produces three overloads. The singular method has one optional Action<T> customizer. The plural methods take either a count or params Action<T>[]. For input blueprints, the customizer type is TInput, while out and the returned builder use output type T.
- Root and root-input grafts are generic in
TAnchor, so they are available after any step with the same seed. Their count or customizer list applies once at the current anchor. - Child and child-input grafts require an anchor of
BiomeBuilder<TSeed, TParent[]>, so they appear only after their declared parent. They do not create missing ancestors. Child grafts fan out for each parent; their count and customizers apply to each parent. - Every method takes
out. A singular child graft requires exactly one parent at runtime. A plural call with no customizers creates one default child per parent, or one root output for a root graft. Each method re-anchors to its output array.
The generated customizers use Action<T> (or Action<TInput> for inputs); no context-aware overload is generated. For a one-off context-aware customizer, use the core AddOne or Add API with the blueprint directly:
await TestBiome
.Start(out _)
.AddOne(
Blueprints.District,
out var district,
// Backdate the district relative to the test's clock.
(d, ctx) => d.CreatedAtUtc = ctx.Ago(TimeSpan.FromDays(30))
)
.BuildAsync();
Grafts let the chain read as a list of entities. These two chains save the same district, school and three-seat class.
await TestBiome
.Start(_db, out _)
// The graft names show the entities that each step builds.
.AddDistrict(out _)
.AddSchool(out _)
.AddClass(out var schoolClass, c => c.Capacity = 3)
.BuildAsync();
await TestBiome
.Start(_db, out _)
// Each core call takes the blueprint for its step.
.AddOne(Blueprints.District, out _)
.AddOne(Blueprints.School, out _)
.AddOne(Blueprints.SchoolClass, out var schoolClass, c => c.Capacity = 3)
.BuildAsync();
AddClass is missing from IntelliSense, add the school first or jump back to an existing one with .Under(school).Graft conventions
- Name a blueprint member for the domain operation, and use
[Graft("Name", Plural = "Names")]when its member name is not the desired call-site word. - Use a context overload only for a type whose tests repeatedly need the clock or parent. Handwrite only that overload or another app-specific exception; ordinary graft methods come from the catalog.
- Never create ancestors inside a child graft or extension. Write a named scenario helper instead.
x => … and (x, ctx) => … lambdas in one call. C# can't infer both. Generated grafts accept one-parameter customizers, so use the core API for a one-off context-aware callback.Context overloads: when a test needs them
The generated overload set covers defaults, simple customizers, and fan-out. When a type's tests repeatedly need the clock or parent, you can handwrite just a named context overload for that type:
public static class SchoolContextExtensions
{
extension<TAnchor>(BiomeBuilder<SchoolSeed, TAnchor> b)
{
// Pass the context through so tests can use the session clock.
public BiomeBuilder<SchoolSeed, District[]> AddDistrict(
out District district,
Action<District, BiomeContext<SchoolSeed>> customize
) => b.AddOne(Blueprints.District, out district, customize);
}
}
await TestBiome
.Start(out _)
// The two-argument lambda selects the context overload.
.AddDistrict(out var olderDistrict, (d, ctx) => d.CreatedAtUtc = ctx.Ago(TimeSpan.FromDays(30)))
.BuildAsync();
C# picks the overload by the lambda's parameter count, so existing d => … calls still compile. A child overload takes Action<T, ChildContext<TSeed, TParent>>. Every lambda in one call must still use the same form.
Scenario helpers
A class-capacity test needs a district, a school, a class and enough students to fill it. If several tests begin there, give the chain a name. A scenario helper is an app-level chain declared on the seed anchor. It returns the leaf builder while keeping the construction steps visible.
public static class SchoolScenarioHelpers
{
// Require the seed anchor because this helper builds its own district.
extension(BiomeBuilder<SchoolSeed, SchoolSeed> b)
{
public BiomeBuilder<SchoolSeed, Student[]> AddFullClass(
out SchoolClass schoolClass,
out Student[] students
)
{
// Match the student count to the capacity so the class is full.
var classStep = b.AddDistrict(out _)
.AddSchool(out _)
.AddClass(out schoolClass, c => c.Capacity = 3);
return classStep.AddStudents(out students, 3);
}
}
}
Each test can now ask for a full class in one line.
await TestBiome
.Start(_db, out _)
// Reuse the setup that fills every seat for a capacity test.
.AddFullClass(out var fullClass, out var students)
.BuildAsync();
var district = NewDistrict(seed);
var school = NewSchool(district);
// Match the roster size to the capacity to exercise the full-class case.
var fullClass = NewClass(school, capacity: 3);
var students = Enumerable.Range(1, 3).Select(seat => NewStudent(fullClass, seat)).ToArray();
_db.AddRange(district, school, fullClass);
_db.AddRange(students);
await _db.SaveChangesAsync();
A helper can also take a parameter. If roster tests need different numbers of students, pass the count through to the final Add.
public static class ClassRosterScenarioHelpers
{
extension(BiomeBuilder<SchoolSeed, SchoolSeed> b)
{
public BiomeBuilder<SchoolSeed, Student[]> AddClassWithRoster(
int studentCount,
out SchoolClass schoolClass,
out Student[] students
) =>
b.AddDistrict(out _)
.AddSchool(out _)
.AddClass(out schoolClass)
// Vary the roster size while keeping the same parent setup.
.AddStudents(out students, studentCount);
}
}
Where to put things
Tests.Common/Biome/
SchoolSeed.cs seed type
TestBiome.cs options + Start overloads (no-DB, DbContext)
Schools/
Blueprints.cs sequences, blueprints, variants (one file: init order)
Traits.cs Districts.Archived, …
ContextExtensions.cs optional context overloads
ScenarioHelpers.cs named multi-step chains
Enrollment/ …