Skip to content

Entity Set References

Entity set references let you constraint sets of entities related to an outgoing entity.

Example

For example if you have an assertion to check that all runs contain at least one assertion

[Assertion]
public Constraint HasAtLeastOneAssertionSelection()
{
    return Check.For(Times.All, (Run run) =>
    Check.For(Times.AtLeastOnce, (AssertionSelection ia) =>
        ia.RunId.IsEqualTo(run.Id)));
}
then you can add an entity set reference to the AssertionSelection entity
public EntitySetReference<AssertionSelection> IncludedAssertions =>
    new(assertionSelection => assertionSelection.RunId.IsEqualTo(Id));
and can now simplify the assertion to be
[Assertion]
public Constraint HasAtLeastOneAssertionSelection()
{
    return Check.For(Times.All, (Run run) =>
        run.IncludedAssertions.IsNotEmpty());
}

Note

When you can be sure that exactly zero or one entities are going to match the connecting constraint, then you can use entity references to model the relation.

Use Cases

Asserting existance of entites matching the connecting constraint

Use IsNotEmpty or IsEmpty to check if any entites matching the connecting constraint exist or not.

run.IncludedAssertions.IsNotEmpty()
run.IncludedAssertions.IsEmpty()

Asserting that entity is (not) contained in set

Use Contains to check if an entity (reference) is contained in a set.

author.Posts.Contains(post)
author.Posts.Contains(post).Not()

Asserting that subset matches constraint

Use For to check if a subset matches a constraint. It can be used like the standalone For constraint, but only considers entities matching the connecting constraint.

author.Posts.For(Times.All, post => post.DeletedAt.IsEqualTo(Constant.Null))