Fan-out
Per parent, in parent order
A class-capacity report may need two classes with three students each. Add the students while the anchor holds both classes. Counts and customizers apply to each parent, and the result is one flat array in parent order.
await TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchool(out _)
.AddClasses(out var classes, 2)
// Three students per class, in class order. The first three belong to classes[0].
.AddStudents(out var students, 3)
.BuildAsync();
Customizers per parent
Each customizer creates one entity per parent. Here each class gets a student named Maya in the first seat and a student with a default name in the second seat.
await TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchool(out _)
.AddClasses(out _, 2)
.AddStudents(
out var students,
// Each class gets Maya in seat 1 and a default name in seat 2.
s => s.FullName = "Maya Chen",
_ => { }
)
.BuildAsync();
A staffing report needs a small class and a large class at each school. The two customizers express the two capacities, while the blueprint supplies the links and timestamps.
await TestBiome
.Start(_db, out _)
.AddDistrict(out _)
.AddSchools(out var schools, 2)
// Each school gets a 12-seat class and a 36-seat class, in that order.
.AddClasses(out var classes, c => c.Capacity = 12, c => c.Capacity = 36)
.BuildAsync();
// Start with the district and schools already built in memory.
var now = DateTimeOffset.UtcNow;
now = new DateTimeOffset(now.Ticks - now.Ticks % 10, TimeSpan.Zero);
var classes = new List<SchoolClass>();
foreach (var school in schools)
{
foreach (var capacity in new[] { 12, 36 })
{
classes.Add(
new SchoolClass
{
Id = $"cls_{Guid.CreateVersion7():N}",
DistrictId = school.DistrictId,
SchoolId = school.Id,
Name = "Algebra I",
Capacity = capacity,
CreatedAtUtc = now,
}
);
}
}
_db.Add(district);
_db.AddRange(schools);
_db.AddRange(classes);
await _db.SaveChangesAsync();
Parent-aware customizers
A child customizer in the two-parameter form receives a ChildContext:
| Member | Meaning |
|---|---|
ctx.Parent | This child's parent, typed. |
ctx.ParentIndex | The parent's position in the anchor array. |
ctx.Index | The child's position under that parent. |
await TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchools(out _, s => s.Code = "NH", s => s.Code = "SH")
.Add(
Blueprints.Class,
out var classes,
// Use the current parent’s code to distinguish the two class names.
(schoolClass, ctx) => schoolClass.Name = $"{ctx.Parent.Code} Algebra I"
)
.BuildAsync();
ctx.NewId for IDs.A base-context trait works for children too
A trait written against BiomeContext<TSeed> is assignable to the child's ChildContext form. For example, use a shared trait to backdate a student's enrollment and a child-aware customizer to record where the second student was added.
var positions = new List<(int Parent, int Child)>();
// This trait only needs the clock, so it can accept the base context.
Action<Student, BiomeContext<SchoolSeed>> returning = (student, ctx) =>
student.EnrolledAtUtc = ctx.Ago(TimeSpan.FromDays(30));
await TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchool(out _)
.AddClasses(out _, 2)
.Add(
Blueprints.Student,
out var students,
// Use the same trait here alongside a customizer that needs the child context.
returning,
(_, ctx) =>
{
positions.Add((ctx.ParentIndex, ctx.Index)); // Adds (0, 1) and (1, 1).
}
)
.BuildAsync();
Singular child adds need one parent
out value can identify one child. After AddClasses(out var classes, 2), use the plural Add for both classes or select one with .Under(classes[0]). A singular child Add with zero or multiple parents throws.