Skip to content

Chronicle projection joins across event streams

Use a join in a Cratis Chronicle projection when one read model needs facts from separate event streams, such as a user and the group they belong to. The read model stores a join key, and Chronicle uses it to find the related events and apply their property mappings.

Use From for the events that establish the read model and Join for related events. In the .NET builder, .On(m => m.GroupId) selects the read model property that identifies the group stream:

using Cratis.Chronicle.Projections;
public class DecJoinsUserProjection : IProjectionFor<DecJoinsUser>
{
public void Define(IProjectionBuilderFor<DecJoinsUser> builder) => builder
.From<DecJoinsUserCreated>()
.From<DecJoinsUserAssignedToGroup>(b => b
.UsingKey(e => e.UserId)
.Set(m => m.GroupId).ToEventSourceId())
.Join<DecJoinsGroupCreated>(j => j
.On(m => m.GroupId))
.Join<DecJoinsGroupRenamed>(j => j
.On(m => m.GroupId));
}

View C# snippet source on GitHub

The model can keep user fields alongside group fields. The following shape separates Name from GroupName, so those fields need different mappings:

public record DecJoinsUser(
string Name,
string Email,
string? GroupId,
string? GroupName,
string? GroupDescription);

View C# snippet source on GitHub

On selects the relationship; it does not rename properties. For this shape, explicitly map the group-created event’s Name to GroupName and Description to GroupDescription, then map the rename event’s NewName to GroupName. For example, add .Set(m => m.GroupName).To(e => e.NewName) inside the rename join. With AutoMap enabled, a group event’s Name would otherwise target the user’s same-named Name property, not GroupName. See AutoMap to control name-based mapping.

Append group-created and group-renamed events with the group’s event source ID. In this example, the assignment event also belongs to the group stream; its UserId selects the user read model, and .ToEventSourceId() records the group stream’s ID as GroupId:

using Cratis.Chronicle.Events;
// User stream events
[EventType]
public record DecJoinsUserCreated(string Name, string Email);
[EventType]
public record DecJoinsUserAssignedToGroup(string UserId, string GroupId);
// Group stream events
[EventType]
public record DecJoinsGroupCreated(string Name, string Description);
[EventType]
public record DecJoinsGroupRenamed(string NewName);

View C# snippet source on GitHub

A From event creates or updates a read model and can set the join key. When a related event arrives, Chronicle matches its key against the read model’s On property and applies the joined mappings. For a default join, that event key is its event source ID.

Chronicle also resolves earlier joined events when processing a From event. This backfill lets a newly established relationship pick up data already present in the joined stream. Joined mappings run after From mappings, so they take precedence when both write the same property.

Set the join key from the event that establishes the relationship. The assignment mapping above uses .UsingKey(e => e.UserId) to select the user and .Set(m => m.GroupId).ToEventSourceId() to store the group ID. If your assignment event belongs to the user stream instead, map GroupId from the event’s group-ID property rather than its event source ID.

Declare a join for each related event type you want to handle. Both group-created and group-renamed joins use .On(m => m.GroupId) because they refer to the same relationship. Their property mappings determine which group fields change.

For a root join, .UsingKey(e => e.X) matches the read model’s On property against that event property instead of the event source ID. If the property is missing or null, Chronicle falls back to the event source ID. Context, constant, and composite key expressions are not supported for root joins: Chronicle logs a warning and uses the event source ID instead.

Append the root before the keyed join event when its key differs from its event source ID. If the joined event arrives first, backfill still looks it up by event source ID, not by the explicit key property.

Do not use arrival order to decide whether a From mapping or a join owns a property. On a From event, Chronicle resolves the latest stored event of each joined type before that event’s sequence number and applies its mappings after the From mappings. A local write cannot clear a value that this join resolution reapplies; there is no dedicated “clear this joined value” operation.

If a field must reflect which fact happened most recently, store each fact separately, such as two timestamps, and compare them. The CHR0042 analyzer warns when a From mapping and a join write the same property.

Use a separate join key for each relationship when the read model needs data from several streams. Map each source into its intended fields, such as GroupName, DepartmentName, and LocationAddress; differently named fields need explicit mappings:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecJoinsMultipleEmployeeAssigned(string GroupId, string DepartmentId, string LocationId);
[EventType]
public record DecJoinsMultipleGroupCreated(string Name);
[EventType]
public record DecJoinsMultipleDepartmentCreated(string Name);
[EventType]
public record DecJoinsMultipleLocationUpdated(string Address);
public record DecJoinsMultipleEmployeeSummary(
string? GroupId,
string? GroupName,
string? DepartmentId,
string? DepartmentName,
string? LocationId,
string? LocationAddress);
public class DecJoinsMultipleEmployeeSummaryProjection : IProjectionFor<DecJoinsMultipleEmployeeSummary>
{
public void Define(IProjectionBuilderFor<DecJoinsMultipleEmployeeSummary> builder) => builder
.From<DecJoinsMultipleEmployeeAssigned>()
.Join<DecJoinsMultipleGroupCreated>(j => j.On(m => m.GroupId))
.Join<DecJoinsMultipleDepartmentCreated>(j => j.On(m => m.DepartmentId))
.Join<DecJoinsMultipleLocationUpdated>(j => j.On(m => m.LocationId));
}

View C# snippet source on GitHub

Inside a .NET Children builder, the join uses the child’s IdentifiedBy property as its target, so do not specify .On(). By default, the joined event’s event source ID selects the child. Here each project-board member is identified by the member stream’s ID, allowing a member-registration event to populate that child’s name:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecJoinsChildMemberAssignedToProject(string ProjectId, string MemberId);
[EventType]
public record DecJoinsChildMemberRegistered(string Name);
public record DecJoinsChildProjectMember(string MemberId, string? Name);
public record DecJoinsChildProjectBoard(IEnumerable<DecJoinsChildProjectMember> Members);
public class DecJoinsChildProjectBoardProjection : IProjectionFor<DecJoinsChildProjectBoard>
{
public void Define(IProjectionBuilderFor<DecJoinsChildProjectBoard> builder) => builder
.Children(m => m.Members, children => children
.IdentifiedBy(e => e.MemberId)
.From<DecJoinsChildMemberAssignedToProject>(b => b
.UsingParentKey(e => e.ProjectId)
.UsingKey(e => e.MemberId))
.Join<DecJoinsChildMemberRegistered>());
}

View C# snippet source on GitHub

Automatic mapping in declaration-language joins

Section titled “Automatic mapping in declaration-language joins”

In the declaration language, a with <Event> block inherits the projection’s automap setting. Put no automap inside that block to use only explicit mappings, or automap to enable name-based mapping when the enclosing projection disables it. The setting also applies during backfill.

Per-join automap and no automap are declaration-language only: the .NET JoinBuilder and the C#, Kotlin, and TypeScript code generators do not express them. Generated code will not preserve a per-join override, so keep the declaration as the source of truth when using one.

A joined event can update several read models that refer to the same key. Consider how many instances share each relationship and how often the joined data changes when choosing what belongs in the projection.

For related data owned by one parent, compare Children and Nested objects. They model collections and optional objects within the parent rather than joining independent streams.