Membership
Membership setup combines several different needs: aliases under each user, expiry based on invitation time, and an organization created with its membership. This example shows when a child blueprint, a derived default, or an aggregate fits each choice.
Identity and membership: User with UserAlias children, PendingInvitation with a derived expiry, and an Organization built from an input as an aggregate (organization + membership). Source: tests/Zeeq.Biome.Tests/Membership.
Every scenario exists twice in one MembershipTests partial class: MembershipTests.WithBiome.cs uses Biome, and MembershipTests.Typical.cs sets up the same entities by hand with identical assertions. Read them side by side to see what Biome removes.
The blueprints
internal static readonly Blueprint<MembershipSeed, User> User = Blueprint.For<
MembershipSeed,
User
>(ctx => new() { Id = ctx.NewId("user") });
// Give tests a VIP option while preserving the ordinary user blueprint.
internal static readonly Blueprint<MembershipSeed, User> VipUser = User.With(u =>
u.IsVip = true
);
internal static readonly ChildBlueprint<MembershipSeed, User, UserAlias> Alias = Blueprint
.ChildOf<MembershipSeed, User, UserAlias>((user, _) => new() { User = user })
// Fill missing normalization after the test chooses the alias value.
.Derive(a => a.NormalizedValue, (a, _) => a.Value.ToUpperInvariant());
internal static readonly Blueprint<MembershipSeed, PendingInvitation> Invitation = Blueprint
.For<MembershipSeed, PendingInvitation>(ctx => new() { CreatedAt = ctx.Now })
// Use the creation time after the test has had a chance to change it.
.Derive(i => i.ExpiresAt, (i, _) => i.CreatedAt.AddDays(7));
Aliases fan out per user
Two users × two alias customizers = four aliases. An explicit NormalizedValue survives the fill-only Derive.
await TestBiome
.Start(out _)
.AddUsers(out var users, 2)
.AddAliases(
out var aliases,
// Each user gets one alias with derived normalization and one with an explicit value.
a => a.Value = "Alice",
a =>
{
a.Value = "Bob";
a.NormalizedValue = "manual";
}
)
.BuildAsync();
// aliases.Length == 4; aliases[0].NormalizedValue == "ALICE";
// aliases[3].NormalizedValue == "manual"
var alice = new User { Id = $"user_{Guid.CreateVersion7():N}" };
var bob = new User { Id = $"user_{Guid.CreateVersion7():N}" };
var users = new[] { alice, bob };
var aliases = new List<UserAlias>();
foreach (var user in users)
{
aliases.Add(
new UserAlias
{
User = user,
Value = "Alice",
NormalizedValue = "ALICE", // Apply the default normalization for each user.
}
);
aliases.Add(
new UserAlias
{
User = user,
Value = "Bob",
NormalizedValue = "manual",
}
);
}
db.AddRange(seed.Owner);
db.AddRange(users);
db.AddRange(aliases);
await db.SaveChangesAsync();
"ALICE" by hand, and the rule can drift.Derived expiry follows customized creation time
var created = new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero);
await TestBiome
.Start(out _)
.AddInvitations(
out var invitations,
// The first deadline follows CreatedAt. The second keeps the explicit one-day expiry.
i => i.CreatedAt = created,
i =>
{
i.CreatedAt = created;
i.ExpiresAt = created.AddDays(1);
}
)
.BuildAsync();
// invitations[0].ExpiresAt == created.AddDays(7) (derived)
// invitations[1].ExpiresAt == created.AddDays(1) (explicit, kept)
Variants never mutate the original
await TestBiome
.Start(out _)
// Compare the VIP variant and the original blueprint in the same session.
.AddOne(Blueprints.VipUser, out var vip)
.AddUser(out var ordinary)
// Supply an empty string so the normalization derive leaves it as entered.
.AddAlias(out var alias, a => a.NormalizedValue = "")
.BuildAsync();
// vip.IsVip is true; ordinary.IsVip is false; alias.NormalizedValue == ""
NormalizedValue = "" is kept, while leaving it null lets the Derive fill it. See Customizing.An organization as an aggregate
One input becomes an Organization plus its OrganizationMembership. At save, the parts replace the wrapper, after the seed owner:
internal sealed record OrganizationGraph(
Organization Organization,
OrganizationMembership Membership
) : IBiomeAggregate
{
// Save the organization before the membership that references it.
public IEnumerable<object> GetEntities() => [Organization, Membership];
}
internal static readonly InputBlueprint<
MembershipSeed,
OrganizationInput,
OrganizationGraph
> Organization = Blueprint.FromInput<MembershipSeed, OrganizationInput, OrganizationGraph>(
() => new(),
(input, ctx) =>
{
// Build both rows from one input and link the membership to the seed owner.
var org = new Organization { Slug = input.Slug };
return new(
org,
new()
{
Organization = org,
User = ctx.Seed.Owner,
IsDefault = input.IsDefault,
}
);
}
);
var store = new RecordingStore();
var biome = TestBiome.Start(out var seed, store);
await biome
// Customize one input per organization and get back a graph for each one.
.AddOrganizations(
out var graphs,
input =>
{
input.Slug = "alpha";
input.IsDefault = true;
},
input => input.Slug = "beta"
)
.BuildAsync();
// The store holds the seed owner followed by the two parts of each graph.
// biome.All<OrganizationGraph>() still returns both wrappers.