Skip to content

Model-bound joins: map facts from related event sources

A model-bound join copies facts from a related event source into a read model. Use it when an order needs a customer’s name or a line item needs product details, so the query can read those values from the model. In .NET, [Join<TEvent>] declares the source event and the model property used to find a match.

The on property holds the related event source’s identifier. Chronicle matches that value to the joined event’s event source id, then copies the selected event property into the model:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record MbJoinsOrderPlaced(Guid CustomerId, decimal Amount);
[EventType]
public record MbJoinsCustomerCreated(string Name);
public record MbJoinsOrderSummary(
[Key]
Guid OrderId,
[SetFrom<MbJoinsOrderPlaced>]
decimal Amount,
[SetFrom<MbJoinsOrderPlaced>]
Guid CustomerId,
[Join<MbJoinsCustomerCreated>(
on: nameof(CustomerId),
eventPropertyName: nameof(MbJoinsCustomerCreated.Name))]
string CustomerName);

View C# snippet source on GitHub

Parameter Meaning
on Property on the read model holding the related identifier. If omitted at the root, it defaults to the annotated property itself. Specify it when the identifier is stored in another property.
eventPropertyName Property to copy from the event. If omitted, it defaults to the annotated read model property’s name.

A child collection item can join related data independently of its parent. If the child has an identifiedBy property, omitting on uses that property as the join target:

[EventType]
public record MbJoinsLineItemAdded(Guid ProductId, int Quantity);
[EventType]
public record MbJoinsProductUpdated(string ProductName, decimal CurrentPrice);
public record MbJoinsOrder(
[Key]
Guid OrderId,
[ChildrenFrom<MbJoinsLineItemAdded>(key: nameof(MbJoinsLineItemAdded.ProductId))]
IEnumerable<MbJoinsOrderLine> Lines);
// The line's key is the product id, so the join to ProductUpdated (raised on that
// same product's event source) resolves implicitly through the child's own key.
public record MbJoinsOrderLine(
[Key] Guid ProductId,
[SetFrom<MbJoinsLineItemAdded>]
int Quantity,
[Join<MbJoinsProductUpdated>(eventPropertyName: nameof(MbJoinsProductUpdated.ProductName))]
string ProductName,
[Join<MbJoinsProductUpdated>(eventPropertyName: nameof(MbJoinsProductUpdated.CurrentPrice))]
decimal Price);

View C# snippet source on GitHub

Use separate join attributes when different events supply different fields on the same model:

[EventType]
public record MbJoinsMultipleOrderPlaced(Guid CustomerId);
[EventType]
public record MbJoinsMultipleCustomerCreated(string Name);
[EventType]
public record MbJoinsCustomerUpdated(string Email);
[EventType]
public record MbJoinsShippingAddressSet(string Address);
public record MbJoinsEnrichedOrder(
[Key]
Guid OrderId,
[SetFrom<MbJoinsMultipleOrderPlaced>]
Guid CustomerId,
[Join<MbJoinsMultipleCustomerCreated>(on: nameof(CustomerId))]
string CustomerName,
[Join<MbJoinsCustomerUpdated>(on: nameof(CustomerId))]
string CustomerEmail,
// ShippingAddressSet is raised on the order's own event source, so it joins on the
// read model's own key rather than a separate correlating property.
[Join<MbJoinsShippingAddressSet>(on: nameof(OrderId))]
string ShippingAddress);

View C# snippet source on GitHub

Join annotations are also processed on child types. This keeps each child’s related-data mappings beside the properties they populate:

[EventType]
public record MbJoinsSourcesLineItemAdded(Guid ProductId);
[EventType]
public record MbJoinsSourcesProductCatalogUpdated(string Name, string Description);
[EventType]
public record MbJoinsSourcesPricingUpdated(decimal CurrentPrice);
public record MbJoinsSourcesOrder(
[Key]
Guid OrderId,
[ChildrenFrom<MbJoinsSourcesLineItemAdded>(key: nameof(MbJoinsSourcesLineItemAdded.ProductId))]
IEnumerable<MbJoinsSourcesOrderLine> Lines);
// Keyed by product id, so both joins below resolve implicitly through the child's own key.
public record MbJoinsSourcesOrderLine(
[Key] Guid ProductId,
[Join<MbJoinsSourcesProductCatalogUpdated>(eventPropertyName: nameof(MbJoinsSourcesProductCatalogUpdated.Name))]
string ProductName,
[Join<MbJoinsSourcesProductCatalogUpdated>(eventPropertyName: nameof(MbJoinsSourcesProductCatalogUpdated.Description))]
string Description,
[Join<MbJoinsSourcesPricingUpdated>(eventPropertyName: nameof(MbJoinsSourcesPricingUpdated.CurrentPrice))]
decimal UnitPrice);

View C# snippet source on GitHub

This order model joins customer details at the root and product details on each line item:

// Events
[EventType]
public record MbJoinsFullOrderPlaced(Guid CustomerId, DateTimeOffset PlacedAt);
[EventType]
public record MbJoinsFullCustomerRegistered(string Name, string Email);
[EventType]
public record MbJoinsFullCustomerProfileUpdated(string PhoneNumber);
[EventType]
public record MbJoinsFullLineItemAdded(Guid ProductId, int Quantity);
[EventType]
public record MbJoinsFullProductCreated(string Name, decimal Price);
[EventType]
public record MbJoinsFullProductPriceChanged(decimal NewPrice);
// Read Models
public record MbJoinsFullOrderDetails(
[Key]
Guid OrderId,
[SetFrom<MbJoinsFullOrderPlaced>]
DateTimeOffset PlacedAt,
[SetFrom<MbJoinsFullOrderPlaced>]
Guid CustomerId,
// Join customer information
[Join<MbJoinsFullCustomerRegistered>(
on: nameof(CustomerId),
eventPropertyName: nameof(MbJoinsFullCustomerRegistered.Name))]
string CustomerName,
[Join<MbJoinsFullCustomerRegistered>(
on: nameof(CustomerId),
eventPropertyName: nameof(MbJoinsFullCustomerRegistered.Email))]
string CustomerEmail,
[Join<MbJoinsFullCustomerProfileUpdated>(
on: nameof(CustomerId),
eventPropertyName: nameof(MbJoinsFullCustomerProfileUpdated.PhoneNumber))]
string CustomerPhone,
[ChildrenFrom<MbJoinsFullLineItemAdded>(key: nameof(MbJoinsFullLineItemAdded.ProductId))]
IEnumerable<MbJoinsFullLineItemDetails> Items);
// Keyed by product id, so the joins below resolve implicitly through the child's own key.
public record MbJoinsFullLineItemDetails(
[Key] Guid ProductId,
[SetFrom<MbJoinsFullLineItemAdded>]
int Quantity,
// Join product information
[Join<MbJoinsFullProductCreated>(eventPropertyName: nameof(MbJoinsFullProductCreated.Name))]
string ProductName,
[Join<MbJoinsFullProductCreated>(eventPropertyName: nameof(MbJoinsFullProductCreated.Price))]
[Join<MbJoinsFullProductPriceChanged>(eventPropertyName: nameof(MbJoinsFullProductPriceChanged.NewPrice))]
decimal Price);

View C# snippet source on GitHub

If CustomerRegistered and ProductCreated arrive first, their data is available when OrderPlaced creates the order and LineItemAdded adds a line. Chronicle resolves the matching stored events to fill in the customer and product fields.

Later, ProductPriceChanged updates the price on matching line items, and CustomerProfileUpdated updates the phone number on matching orders. A join therefore handles both data already present when the model is populated and related events that arrive afterward.

Use SetFrom when a handled event directly updates the model selected by that event’s configured key. Use Join when the model stores a reference to a related event source and needs values from its events. The join copies those values into the read model, a form of denormalization that avoids looking them up separately at query time.

When a Join and a local mapping (SetFrom or an auto-mapped FromEvent property) target the same property, the joined value takes precedence. When processing a local event that resolves the join, Chronicle finds the last matching joined event stored before that event’s sequence number and applies the joined properties after the local mappings. A locally written value stays in place while no matching joined event is available.

Use a declarative projection for join-context values or child and nested every-mappings on join-only events. The model-bound limitations show these alternatives and mappings for a reused CLR type.

Choose an on property that stores the related event source’s identifier, and allow for missing data when a matching event has not arrived. Nullable properties can make that absence visible to readers.

For a single object with its own populate-and-clear lifecycle, continue with Nested Objects. For independently identified items, use Children.