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.
Basic Join
Section titled “Basic Join”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);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.Joinimport io.cratis.chronicle.projections.SetFromimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class MbJoinsOrderPlaced(val customerId: String, val amount: Double)
@EventTypedata class MbJoinsCustomerCreated(val name: String)
@ReadModel@FromEvent(MbJoinsOrderPlaced::class)data class MbJoinsOrderSummary( @SetFrom("amount", MbJoinsOrderPlaced::class) val amount: Double = 0.0,
@SetFrom("customerId", MbJoinsOrderPlaced::class) val customerId: String = "",
@Join(MbJoinsCustomerCreated::class, on = "customerId", eventPropertyName = "name") val customerName: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.Join;import io.cratis.chronicle.projections.SetFrom;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord MbJoinsOrderPlaced(String customerId, double amount) {}
@EventTyperecord MbJoinsCustomerCreated(String name) {}
@ReadModel@FromEvent(eventType = MbJoinsOrderPlaced.class)class MbJoinsOrderSummary { @SetFrom(propertyPath = "amount", eventType = MbJoinsOrderPlaced.class) public double amount = 0.0;
@SetFrom(propertyPath = "customerId", eventType = MbJoinsOrderPlaced.class) public String customerId = "";
@Join(eventType = MbJoinsCustomerCreated.class, on = "customerId", eventPropertyName = "name") public String customerName = "";}defmodule MyApp.Events.JoinsOrderPlaced do use Cratis.Chronicle.Events.EventType, id: "joins-order-placed-v1"
defstruct [:customer_id, :amount]end
defmodule MyApp.Events.JoinsCustomerCreated do use Cratis.Chronicle.Events.EventType, id: "joins-customer-created-v1"
defstruct [:name]end
defmodule MyApp.ReadModels.JoinsOrderSummary do use Cratis.Chronicle.ReadModels.ReadModel
alias MyApp.Events.{JoinsOrderPlaced, JoinsCustomerCreated}
defstruct [:order_id, :amount, :customer_id, :customer_name]
from JoinsOrderPlaced, set: [order_id: :event_source_id, amount: :amount, customer_id: :customer_id]
join JoinsCustomerCreated, on: "customer_id", set: [customer_name: :name]endimport { eventType, Guid, join, setFrom } from '@cratis/chronicle';
@eventType()export class MbJoinsOrderPlaced { customerId: Guid = Guid.empty; amount = 0;}
@eventType()export class MbJoinsCustomerCreated { name = '';}
export class MbJoinsOrderSummary { id: Guid = Guid.empty;
@setFrom(MbJoinsOrderPlaced, 'amount') amount = 0;
@setFrom(MbJoinsOrderPlaced, 'customerId') customerId: Guid = Guid.empty;
@join(MbJoinsCustomerCreated, 'customerId', 'name') customerName = '';}Parameters
Section titled “Parameters”| 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. |
Join in Children
Section titled “Join in Children”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);Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.Events.MbJoinsChildrenMemberAdded do use Cratis.Chronicle.Events.EventType, id: "mb-joins-children-member-added"
defstruct [:member_id]end
defmodule MyApp.Events.MbJoinsChildrenMemberProfileUpdated do use Cratis.Chronicle.Events.EventType, id: "mb-joins-children-member-profile-updated"
defstruct [:member_id, :display_name]end
defmodule MyApp.ReadModels.MbJoinsChildrenMember do defstruct member_id: nil, display_name: nilend
defmodule MyApp.ReadModels.MbJoinsChildrenTeam do use Cratis.Chronicle.ReadModels.ReadModel, types: [members: {:list, MyApp.ReadModels.MbJoinsChildrenMember}]
defstruct team_id: nil, members: []
children :members, identified_by: :member_id do from MyApp.Events.MbJoinsChildrenMemberAdded, key: :member_id, set: [member_id: :member_id]
# A join inside `children` does not take `:on` — it always matches by the collection's # `identified_by`, so the member who profile changed is found by `member_id` automatically. join MyApp.Events.MbJoinsChildrenMemberProfileUpdated, key: :member_id, set: [display_name: :display_name] endendimport { childrenFrom, eventType, Guid, join, setFrom } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class MbJoinsLineItemAdded { productId: Guid = Guid.empty; quantity = 0;}
@eventType()export class MbJoinsProductUpdated { productName = ''; currentPrice = 0;}
// 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.export class MbJoinsOrderLine { id: Guid = Guid.empty;
@setFrom(MbJoinsLineItemAdded, 'quantity') quantity = 0;
@join(MbJoinsProductUpdated, undefined, 'productName') productName = '';
@join(MbJoinsProductUpdated, undefined, 'currentPrice') price = 0;}
export class MbJoinsOrder { id: Guid = Guid.empty;
@childrenFrom(MbJoinsLineItemAdded, 'productId') @field(Array, { genericArguments: [MbJoinsOrderLine] }) lines: MbJoinsOrderLine[] = [];}Multiple Joins
Section titled “Multiple Joins”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);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.Joinimport io.cratis.chronicle.projections.SetFromimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class MbJoinsMultipleOrderPlaced(val customerId: String)
@EventTypedata class MbJoinsMultipleCustomerCreated(val name: String)
@EventTypedata class MbJoinsCustomerUpdated(val email: String)
@EventTypedata class MbJoinsShippingAddressSet(val address: String)
@ReadModel@FromEvent(MbJoinsMultipleOrderPlaced::class)data class MbJoinsEnrichedOrder( @SetFrom("customerId", MbJoinsMultipleOrderPlaced::class) val customerId: String = "",
@Join(MbJoinsMultipleCustomerCreated::class, on = "customerId") val customerName: String = "",
@Join(MbJoinsCustomerUpdated::class, on = "customerId") val customerEmail: String = "",
// ShippingAddressSet is raised on the order's own event source, so it joins on the // read model's own event source id rather than a separate correlating property. @Join(MbJoinsShippingAddressSet::class) val shippingAddress: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.Join;import io.cratis.chronicle.projections.SetFrom;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord MbJoinsMultipleOrderPlaced(String customerId) {}
@EventTyperecord MbJoinsMultipleCustomerCreated(String name) {}
@EventTyperecord MbJoinsCustomerUpdated(String email) {}
@EventTyperecord MbJoinsShippingAddressSet(String address) {}
@ReadModel@FromEvent(eventType = MbJoinsMultipleOrderPlaced.class)class MbJoinsEnrichedOrder { @SetFrom(propertyPath = "customerId", eventType = MbJoinsMultipleOrderPlaced.class) public String customerId = "";
@Join(eventType = MbJoinsMultipleCustomerCreated.class, on = "customerId") public String customerName = "";
@Join(eventType = MbJoinsCustomerUpdated.class, on = "customerId") public String customerEmail = "";
// ShippingAddressSet is raised on the order's own event source, so it joins on the // read model's own event source id rather than a separate correlating property. @Join(eventType = MbJoinsShippingAddressSet.class) public String shippingAddress = "";}defmodule MyApp.Events.JoinsMultipleOrderPlaced do use Cratis.Chronicle.Events.EventType, id: "joins-multiple-order-placed-v1"
defstruct [:customer_id]end
defmodule MyApp.Events.JoinsMultipleCustomerCreated do use Cratis.Chronicle.Events.EventType, id: "joins-multiple-customer-created-v1"
defstruct [:name]end
defmodule MyApp.Events.JoinsCustomerUpdated do use Cratis.Chronicle.Events.EventType, id: "joins-customer-updated-v1"
defstruct [:email]end
defmodule MyApp.Events.JoinsShippingAddressSet do use Cratis.Chronicle.Events.EventType, id: "joins-shipping-address-set-v1"
defstruct [:address]end
defmodule MyApp.ReadModels.JoinsEnrichedOrder do use Cratis.Chronicle.ReadModels.ReadModel
alias MyApp.Events.{ JoinsMultipleOrderPlaced, JoinsMultipleCustomerCreated, JoinsCustomerUpdated, JoinsShippingAddressSet }
defstruct [:order_id, :customer_id, :customer_name, :customer_email, :shipping_address]
from JoinsMultipleOrderPlaced, set: [order_id: :event_source_id, customer_id: :customer_id]
join JoinsMultipleCustomerCreated, on: "customer_id", set: [customer_name: :name]
join JoinsCustomerUpdated, on: "customer_id", set: [customer_email: :email]
# 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 JoinsShippingAddressSet, on: "order_id", set: [shipping_address: :address]endimport { eventType, Guid, join, setFrom } from '@cratis/chronicle';
@eventType()export class MbJoinsMultipleOrderPlaced { customerId: Guid = Guid.empty;}
@eventType()export class MbJoinsMultipleCustomerCreated { name = '';}
@eventType()export class MbJoinsCustomerUpdated { email = '';}
@eventType()export class MbJoinsShippingAddressSet { address = '';}
export class MbJoinsEnrichedOrder { id: Guid = Guid.empty;
@setFrom(MbJoinsMultipleOrderPlaced, 'customerId') customerId: Guid = Guid.empty;
@join(MbJoinsMultipleCustomerCreated, 'customerId', 'name') customerName = '';
@join(MbJoinsCustomerUpdated, 'customerId', 'email') 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, 'id', 'address') shippingAddress = '';}Recursive Join Processing
Section titled “Recursive Join Processing”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);Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.Events.MbJoinsSourcesLineItemAdded do use Cratis.Chronicle.Events.EventType, id: "mb-joins-sources-line-item-added"
defstruct [:product_id]end
defmodule MyApp.Events.MbJoinsSourcesProductCatalogUpdated do use Cratis.Chronicle.Events.EventType, id: "mb-joins-sources-product-catalog-updated"
defstruct [:name, :description]end
defmodule MyApp.Events.MbJoinsSourcesPricingUpdated do use Cratis.Chronicle.Events.EventType, id: "mb-joins-sources-pricing-updated"
defstruct [:current_price]end
defmodule MyApp.ReadModels.MbJoinsSourcesOrderLine do defstruct product_id: nil, product_name: nil, description: nil, unit_price: 0end
defmodule MyApp.ReadModels.MbJoinsSourcesOrder do use Cratis.Chronicle.ReadModels.ReadModel, types: [lines: {:list, MyApp.ReadModels.MbJoinsSourcesOrderLine}]
defstruct order_id: nil, lines: []
# The lines are keyed by product id, so both joins below resolve implicitly through the # child's own key. children :lines, identified_by: :product_id do from MyApp.Events.MbJoinsSourcesLineItemAdded, key: :product_id, set: [product_id: :product_id]
join MyApp.Events.MbJoinsSourcesProductCatalogUpdated, set: [product_name: :name, description: :description]
join MyApp.Events.MbJoinsSourcesPricingUpdated, set: [unit_price: :current_price] endendimport { childrenFrom, eventType, Guid, join } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class MbJoinsSourcesLineItemAdded { productId: Guid = Guid.empty;}
@eventType()export class MbJoinsSourcesProductCatalogUpdated { name = ''; description = '';}
@eventType()export class MbJoinsSourcesPricingUpdated { currentPrice = 0;}
// Keyed by product id, so both joins below resolve implicitly through the child's own key.export class MbJoinsSourcesOrderLine { id: Guid = Guid.empty;
@join(MbJoinsSourcesProductCatalogUpdated, undefined, 'name') productName = '';
@join(MbJoinsSourcesProductCatalogUpdated, undefined, 'description') description = '';
@join(MbJoinsSourcesPricingUpdated, undefined, 'currentPrice') unitPrice = 0;}
export class MbJoinsSourcesOrder { id: Guid = Guid.empty;
@childrenFrom(MbJoinsSourcesLineItemAdded, 'productId') @field(Array, { genericArguments: [MbJoinsSourcesOrderLine] }) lines: MbJoinsSourcesOrderLine[] = [];}Complete Example
Section titled “Complete Example”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 Modelspublic 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);Kotlin does not support this workflow yet.Java does not support this workflow yet.# Eventsdefmodule MyApp.Events.MbJoinsFullOrderPlaced do use Cratis.Chronicle.Events.EventType, id: "mb-joins-full-order-placed"
defstruct [:customer_id, :placed_at]end
defmodule MyApp.Events.MbJoinsFullCustomerRegistered do use Cratis.Chronicle.Events.EventType, id: "mb-joins-full-customer-registered"
defstruct [:name, :email]end
defmodule MyApp.Events.MbJoinsFullCustomerProfileUpdated do use Cratis.Chronicle.Events.EventType, id: "mb-joins-full-customer-profile-updated"
defstruct [:phone_number]end
defmodule MyApp.Events.MbJoinsFullLineItemAdded do use Cratis.Chronicle.Events.EventType, id: "mb-joins-full-line-item-added"
defstruct [:product_id, :quantity]end
defmodule MyApp.Events.MbJoinsFullProductCreated do use Cratis.Chronicle.Events.EventType, id: "mb-joins-full-product-created"
defstruct [:name, :price]end
defmodule MyApp.Events.MbJoinsFullProductPriceChanged do use Cratis.Chronicle.Events.EventType, id: "mb-joins-full-product-price-changed"
defstruct [:new_price]end
# Read modelsdefmodule MyApp.ReadModels.MbJoinsFullLineItemDetails do defstruct product_id: nil, quantity: 0, product_name: nil, price: 0end
defmodule MyApp.ReadModels.MbJoinsFullOrderDetails do use Cratis.Chronicle.ReadModels.ReadModel, types: [items: {:list, MyApp.ReadModels.MbJoinsFullLineItemDetails}]
alias MyApp.Events.{ MbJoinsFullCustomerProfileUpdated, MbJoinsFullCustomerRegistered, MbJoinsFullLineItemAdded, MbJoinsFullOrderPlaced, MbJoinsFullProductCreated, MbJoinsFullProductPriceChanged }
defstruct order_id: nil, placed_at: nil, customer_id: nil, customer_name: nil, customer_email: nil, customer_phone: nil, items: []
from MbJoinsFullOrderPlaced, set: [order_id: :event_source_id, placed_at: :placed_at, customer_id: :customer_id]
# Join customer information join MbJoinsFullCustomerRegistered, on: "customer_id", set: [customer_name: :name, customer_email: :email]
join MbJoinsFullCustomerProfileUpdated, on: "customer_id", set: [customer_phone: :phone_number]
# Keyed by product id, so the joins below resolve implicitly through the child's own key. children :items, identified_by: :product_id do from MbJoinsFullLineItemAdded, key: :product_id, set: [product_id: :product_id, quantity: :quantity]
# Join product information join MbJoinsFullProductCreated, set: [product_name: :name, price: :price] join MbJoinsFullProductPriceChanged, set: [price: :new_price] endendimport { childrenFrom, eventType, Guid, join, setFrom } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
// Events@eventType()export class MbJoinsFullOrderPlaced { customerId: Guid = Guid.empty; placedAt = new Date();}
@eventType()export class MbJoinsFullCustomerRegistered { name = ''; email = '';}
@eventType()export class MbJoinsFullCustomerProfileUpdated { phoneNumber = '';}
@eventType()export class MbJoinsFullLineItemAdded { productId: Guid = Guid.empty; quantity = 0;}
@eventType()export class MbJoinsFullProductCreated { name = ''; price = 0;}
@eventType()export class MbJoinsFullProductPriceChanged { newPrice = 0;}
// Read Models// Keyed by product id, so the joins below resolve implicitly through the child's own key.export class MbJoinsFullLineItemDetails { id: Guid = Guid.empty;
@setFrom(MbJoinsFullLineItemAdded, 'quantity') quantity = 0;
// Join product information @join(MbJoinsFullProductCreated, undefined, 'name') productName = '';
@join(MbJoinsFullProductCreated, undefined, 'price') @join(MbJoinsFullProductPriceChanged, undefined, 'newPrice') price = 0;}
export class MbJoinsFullOrderDetails { id: Guid = Guid.empty;
@setFrom(MbJoinsFullOrderPlaced, 'placedAt') placedAt = new Date();
@setFrom(MbJoinsFullOrderPlaced, 'customerId') customerId: Guid = Guid.empty;
// Join customer information @join(MbJoinsFullCustomerRegistered, 'customerId', 'name') customerName = '';
@join(MbJoinsFullCustomerRegistered, 'customerId', 'email') customerEmail = '';
@join(MbJoinsFullCustomerProfileUpdated, 'customerId', 'phoneNumber') customerPhone = '';
@childrenFrom(MbJoinsFullLineItemAdded, 'productId') @field(Array, { genericArguments: [MbJoinsFullLineItemDetails] }) items: MbJoinsFullLineItemDetails[] = [];}Event Processing Flow
Section titled “Event Processing Flow”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.
Join vs SetFrom
Section titled “Join vs SetFrom”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.
Join Precedence
Section titled “Join Precedence”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.
Limitations
Section titled “Limitations”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.
Best Practices
Section titled “Best Practices”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.