Skip to content

Cratis Chronicle projections: map event context

Event context in Cratis Chronicle is metadata stored alongside an event rather than in its business payload. Map it into a read model when a query needs to show when an update occurred, identify its source, or connect it to a related operation without adding those fields to every event contract.

Common EventContext properties used in projection mappings include:

  • EventSourceId - The identifier of the event source that produced the event
  • SequenceNumber - The position of the event in the event sequence
  • Occurred - When the event occurred (timestamp)
  • EventType - Information about the event type
  • EventSourceType - The type of the event source
  • EventStreamType - The type of the event stream
  • EventStreamId - The identifier of the event stream
  • CorrelationId - The correlation identifier linking related operations
  • Causation - The recorded chain of causes that led to the event
  • CausedBy - Identity information about who caused the event

Use ToEventSourceId() when a read model property should retain the source ID. The example also maps Occurred so the read model can report the last login or activity time:

using Cratis.Chronicle.Projections;
public class DecEventContextUserActivityProjection : IProjectionFor<DecEventContextUserActivity>
{
public void Define(IProjectionBuilderFor<DecEventContextUserActivity> builder) => builder
.From<DecEventContextUserLoggedIn>(_ => _
.Set(m => m.UserId).ToEventSourceId()
.Set(m => m.LastLogin).ToEventContextProperty(c => c.Occurred))
.From<DecEventContextUserPerformedAction>(_ => _
.Set(m => m.UserId).ToEventSourceId()
.Set(m => m.LastActivity).ToEventContextProperty(c => c.Occurred));
}

View C# snippet source on GitHub

Use ToEventContextProperty() with an accessor for the metadata you need. Here, the sequence number, occurrence time, and correlation ID are mapped explicitly; matching business properties still use AutoMap.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecEventContextUserAction(string UserId, string ActionType);
public class DecEventContextAuditTrailProjection : IProjectionFor<DecEventContextAuditEntry>
{
public void Define(IProjectionBuilderFor<DecEventContextAuditEntry> builder) => builder
.From<DecEventContextUserAction>(_ => _
.Set(m => m.EventId).ToEventContextProperty(c => c.SequenceNumber)
.Set(m => m.OccurredAt).ToEventContextProperty(c => c.Occurred)
.Set(m => m.CorrelationId).ToEventContextProperty(c => c.CorrelationId));
}

View C# snippet source on GitHub

Declare target properties for the metadata your query returns. The activity model stores timestamps, while the audit-entry model also stores sequence and correlation values:

public record DecEventContextUserActivity(
string UserId,
DateTimeOffset LastLogin,
DateTimeOffset LastActivity);
public record DecEventContextAuditEntry(
ulong EventId,
DateTimeOffset OccurredAt,
string CorrelationId,
string ActionType,
string UserId);

View C# snippet source on GitHub

Context values can also help identify the instance to update. This example uses the occurrence timestamp and sequence number as parts of a composite key:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecEventContextUserActionForKey(string UserId, string ActionType);
public record DecEventContextEventLogKey(DateTimeOffset Date, ulong SequenceNumber);
public record DecEventContextEventLogEntry(
DecEventContextEventLogKey Id,
string ActionType,
string UserId);
public class DecEventContextEventLogProjection : IProjectionFor<DecEventContextEventLogEntry>
{
public void Define(IProjectionBuilderFor<DecEventContextEventLogEntry> builder) => builder
.From<DecEventContextUserActionForKey>(_ => _
.UsingCompositeKey<DecEventContextEventLogKey>(_ => _
.Set(k => k.Date).ToEventContextProperty(c => c.Occurred)
.Set(k => k.SequenceNumber).ToEventContextProperty(c => c.SequenceNumber)));
}

View C# snippet source on GitHub

The same context-mapping methods are available inside child and join mappings. This child-collection example stores a timestamp and sequence number on each activity item:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecEventContextActivityPerformed(string ActivityId, string ActivityType);
public record DecEventContextActivityLogEntry(
string ActivityId,
DateTimeOffset Timestamp,
ulong SequenceNumber);
public record DecEventContextUserWithActivityLog(
IEnumerable<DecEventContextActivityLogEntry> ActivityLog);
public class DecEventContextUserActivityLogProjection : IProjectionFor<DecEventContextUserWithActivityLog>
{
public void Define(IProjectionBuilderFor<DecEventContextUserWithActivityLog> builder) => builder
.Children(m => m.ActivityLog, children => children
.IdentifiedBy(e => e.ActivityId)
.From<DecEventContextActivityPerformed>(_ => _
.UsingKey(e => e.ActivityId)
.Set(m => m.Timestamp).ToEventContextProperty(c => c.Occurred)
.Set(m => m.SequenceNumber).ToEventContextProperty(c => c.SequenceNumber)));
}

View C# snippet source on GitHub

The event payloads keep their business fields; timestamps and sequence numbers come from context instead:

using Cratis.Chronicle.Events;
[EventType]
public record DecEventContextUserLoggedIn(string Username);
[EventType]
public record DecEventContextUserPerformedAction(string UserId, string ActionType);

View C# snippet source on GitHub

Map Occurred, SequenceNumber, and CausedBy when the query needs timing, sequence position, and the recorded identity behind an update. Choose which events and business values the audit view must retain; metadata alone is not a complete audit trail.

Store CorrelationId when you need to look up related events or operations carrying that identifier.

Use Occurred to group or filter results by occurrence time. For weekly keys, pair the ISO week number with its week-year as shown in composite keys.

Store SequenceNumber to retain an event’s position within its event sequence. Copying it into a property does not sort a child collection or establish order across different sequences.

Use EventSourceId to identify the event source associated with an update.

For metadata that should update across the event types your projection already handles, continue with FromEvery. Use FromAll only when unrelated event types in the sequence should contribute too.