Skip to content

Cratis Chronicle projections: remove children with RemovedWithJoin

RemovedWithJoin<TEvent>() in Cratis Chronicle removes matching child items across the parent read models maintained by a projection. Use it when a related entity changes independently: disbanding a group, for example, should remove that group’s membership from every user’s collection without listing the users in the event.

Understanding RemoveWithJoin vs RemovedWith

Section titled “Understanding RemoveWithJoin vs RemovedWith”

RemovedWith<TEvent>() targets a child under a particular parent, using the event’s parent and child keys. It fits a user leaving one group. The keys can come from event content; the event does not have to originate on the parent’s event source.

RemovedWithJoin<TEvent>() uses a child key without requiring a parent key. It removes matching children across parents in this projection, which fits a group being disbanded for all its members.

Declare RemovedWithJoin<>() inside Children(...), alongside the events that add and enrich the child:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecRemoveWithJoinBasicUserCreated(string Name);
[EventType]
public record DecRemoveWithJoinBasicUserAddedToGroup(string UserId, string GroupId);
[EventType]
public record DecRemoveWithJoinBasicGroupCreated(string Name);
[EventType]
public record DecRemoveWithJoinBasicGroupDeleted;
public record DecRemoveWithJoinBasicUserGroup(string GroupId, string Name, DateTimeOffset JoinedAt);
public record DecRemoveWithJoinBasicUser(string Name, IEnumerable<DecRemoveWithJoinBasicUserGroup> Groups);
public class DecRemoveWithJoinBasicUserProjection : IProjectionFor<DecRemoveWithJoinBasicUser>
{
public void Define(IProjectionBuilderFor<DecRemoveWithJoinBasicUser> builder) => builder
.From<DecRemoveWithJoinBasicUserCreated>()
.Children(m => m.Groups, children => children
.IdentifiedBy(e => e.GroupId)
.From<DecRemoveWithJoinBasicUserAddedToGroup>(_ => _
.UsingParentKey(e => e.UserId)
.Set(m => m.JoinedAt).ToEventContextProperty(c => c.Occurred))
.Join<DecRemoveWithJoinBasicGroupCreated>()
.RemovedWithJoin<DecRemoveWithJoinBasicGroupDeleted>());
}

View C# snippet source on GitHub

The membership event routes to a user through UsingParentKey(e => e.UserId). The child collection identifies each group through IdentifiedBy(e => e.GroupId), while Join supplies the group’s details. The deletion handler removes children matching its resolved key across users in this projection.

Keys default to the event source ID. The adding event’s child key and the deletion event’s removal key must resolve to the same group identifier. When that identifier is in event content instead, configure UsingKey(...) as in the explicit-key example below. Use UsingParentKey(...) when the parent identifier also comes from event content.

When the projection processes the removal event, Chronicle resolves its key from the event source ID or the configured key expression. It then removes children whose IdentifiedBy value matches that key from the configured collection across parent instances.

This is a child-collection operation, not a search through every projection in the system. Root-level RemovedWithJoin removal is not supported.

Here, the assignment and cancellation events both carry ProjectId. UsingKey(e => e.ProjectId) makes each event resolve to the same child identifier, even if their event source IDs differ:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecRemoveWithJoinExplicitEmployeeHired(string Name);
[EventType]
public record DecRemoveWithJoinExplicitEmployeeAssignedToProject(string EmployeeId, string ProjectId);
[EventType]
public record DecRemoveWithJoinExplicitProjectCreated(string Name);
[EventType]
public record DecRemoveWithJoinExplicitProjectCancelled(string ProjectId);
public record DecRemoveWithJoinExplicitEmployeeProject(string ProjectId, string Name, DateTimeOffset AssignedAt);
public record DecRemoveWithJoinExplicitEmployee(string Name, IEnumerable<DecRemoveWithJoinExplicitEmployeeProject> Projects);
public class DecRemoveWithJoinExplicitEmployeeProjection : IProjectionFor<DecRemoveWithJoinExplicitEmployee>
{
public void Define(IProjectionBuilderFor<DecRemoveWithJoinExplicitEmployee> builder) => builder
.From<DecRemoveWithJoinExplicitEmployeeHired>()
.Children(m => m.Projects, children => children
.IdentifiedBy(e => e.ProjectId)
.From<DecRemoveWithJoinExplicitEmployeeAssignedToProject>(_ => _
.UsingParentKey(e => e.EmployeeId)
.UsingKey(e => e.ProjectId)
.Set(m => m.AssignedAt).ToEventContextProperty(c => c.Occurred))
.Join<DecRemoveWithJoinExplicitProjectCreated>()
.RemovedWithJoin<DecRemoveWithJoinExplicitProjectCancelled>(_ => _
.UsingKey(e => e.ProjectId)));
}

View C# snippet source on GitHub

A membership can end because one user leaves or because the group is disbanded. Declare both handlers on the membership collection:

public class DecRemoveWithJoinGroupMembershipProjection : IProjectionFor<DecRemoveWithJoinUserProfile>
{
public void Define(IProjectionBuilderFor<DecRemoveWithJoinUserProfile> builder) => builder
.From<DecRemoveWithJoinUserRegistered>(_ => _
.Set(m => m.UserId).ToEventSourceId()
.Set(m => m.RegisteredAt).ToEventContextProperty(c => c.Occurred))
.Children(m => m.Memberships, children => children
.IdentifiedBy(e => e.GroupId)
.From<DecRemoveWithJoinUserJoinedGroup>(_ => _
.UsingParentKey(e => e.UserId)
.UsingKey(e => e.GroupId)
.Set(m => m.JoinedAt).ToEventContextProperty(c => c.Occurred))
.Join<DecRemoveWithJoinGroupCreated>()
.RemovedWith<DecRemoveWithJoinUserLeftGroup>(_ => _
.UsingParentKey(e => e.UserId)
.UsingKey(e => e.GroupId))
.RemovedWithJoin<DecRemoveWithJoinGroupDisbanded>());
}

View C# snippet source on GitHub

The user-left handler supplies both UserId and GroupId, so it removes one membership. The group-disbanded handler supplies the group key, so it removes that group from every user’s membership collection in this projection.

public class DecRemoveWithJoinDeveloperProjectsProjection : IProjectionFor<DecRemoveWithJoinDeveloperProfile>
{
public void Define(IProjectionBuilderFor<DecRemoveWithJoinDeveloperProfile> builder) => builder
.From<DecRemoveWithJoinDeveloperOnboarded>(_ => _
.Set(m => m.DeveloperId).ToEventSourceId()
.Set(m => m.OnboardedAt).ToEventContextProperty(c => c.Occurred))
.Children(m => m.CurrentProjects, children => children
.IdentifiedBy(e => e.ProjectId)
.From<DecRemoveWithJoinDeveloperAssignedToProject>(_ => _
.UsingParentKey(e => e.DeveloperId)
.UsingKey(e => e.ProjectId)
.Set(m => m.AssignedAt).ToEventContextProperty(c => c.Occurred))
.Join<DecRemoveWithJoinProjectInitiated>()
.RemovedWith<DecRemoveWithJoinDeveloperUnassignedFromProject>(_ => _
.UsingParentKey(e => e.DeveloperId)
.UsingKey(e => e.ProjectId))
.RemovedWithJoin<DecRemoveWithJoinProjectCancelled>()
.RemovedWithJoin<DecRemoveWithJoinProjectCompleted>());
}

View C# snippet source on GitHub

The same distinction applies to project assignments:

  • An unassignment removes one project from one developer’s collection.
  • A cancellation removes that project from all developer collections in the projection.
  • A completion removes it from those collections in the same way.

Each parent keeps its own collection of memberships or assignments. The child identifier is the group or project ID shared with the removal event:

public record DecRemoveWithJoinUserProfile(
string UserId,
string Username,
string Email,
DateTimeOffset RegisteredAt,
IEnumerable<DecRemoveWithJoinGroupMembership> Memberships);
public record DecRemoveWithJoinGroupMembership(
string GroupId,
string GroupName,
string GroupType,
DateTimeOffset JoinedAt,
string Role);
public record DecRemoveWithJoinDeveloperProfile(
string DeveloperId,
string Name,
IEnumerable<string> Skills,
DateTimeOffset OnboardedAt,
IEnumerable<DecRemoveWithJoinProjectAssignment> CurrentProjects);
public record DecRemoveWithJoinProjectAssignment(
string ProjectId,
string ProjectName,
string Priority,
DateTimeOffset Deadline,
DateTimeOffset AssignedAt,
string Role,
int Allocation);

View C# snippet source on GitHub

Individual removal events carry the parent and child IDs. Group-wide and project-wide removal events can use their event source ID as the child key:

using Cratis.Chronicle.Events;
[EventType]
public record DecRemoveWithJoinUserRegistered(string Username, string Email);
[EventType]
public record DecRemoveWithJoinUserJoinedGroup(string UserId, string GroupId, string Role);
[EventType]
public record DecRemoveWithJoinUserLeftGroup(string UserId, string GroupId);
[EventType]
public record DecRemoveWithJoinGroupCreated(string GroupName, string GroupType);
[EventType]
public record DecRemoveWithJoinGroupDisbanded;
[EventType]
public record DecRemoveWithJoinDeveloperOnboarded(string Name, IEnumerable<string> Skills);
[EventType]
public record DecRemoveWithJoinDeveloperAssignedToProject(string DeveloperId, string ProjectId, string Role, int Allocation);
[EventType]
public record DecRemoveWithJoinDeveloperUnassignedFromProject(string DeveloperId, string ProjectId);
[EventType]
public record DecRemoveWithJoinProjectInitiated(string ProjectName, string Priority, DateTimeOffset Deadline);
[EventType]
public record DecRemoveWithJoinProjectCancelled;
[EventType]
public record DecRemoveWithJoinProjectCompleted;

View C# snippet source on GitHub

Use RemovedWithJoin<>() when an event about one entity should remove its representation from many parent collections. This updates derived read models; it does not delete the source events or enforce a write-time relationship constraint.

Choose by the scope of the removal, not just by which stream emitted the event. Use RemovedWith<>() when you know the parent and child. Use RemovedWithJoin<>() when the child key should match across parents.

Parent instances can be updated by different event-source partitions. Do not treat removal across their collections as a single atomic change. See eventual consistency in projections when deciding what callers may read while processing catches up.

If you are still choosing the child identifier or routing membership events to a parent, start with child projections.