Skip to content

Cratis Chronicle FromEventSequence: select a sequence

FromEventSequence() in Cratis Chronicle selects the event sequence a projection reads. Use it when the events you need are stored outside the default event-log sequence. Event type mappings still decide which events contribute, but matching events in other sequences do not feed this projection.

Defining a projection with specific event sequence

Section titled “Defining a projection with specific event sequence”

Call FromEventSequence() on the projection builder, then declare the event types to handle. This example reads order events from "order-management":

using Cratis.Chronicle.Projections;
public class DecFromEventSequenceOrderProjection : IProjectionFor<DecFromEventSequenceOrder>
{
public void Define(IProjectionBuilderFor<DecFromEventSequenceOrder> builder) => builder
.FromEventSequence("order-management")
.From<DecFromEventSequenceOrderCreated>()
.From<DecFromEventSequenceOrderUpdated>()
.From<DecFromEventSequenceOrderShipped>();
}

View C# snippet source on GitHub

The projection handles the declared order event types only within "order-management". An event of the same type appended to another sequence will not update this read model.

The method accepts an EventSequenceId; a string name can be converted to that type. Use a shared constant when several projections or append paths must refer to the same sequence:

public static class DecFromEventSequenceEventSequences
{
public const string OrderManagement = "order-management";
}
public class DecFromEventSequenceOrderProjectionWithConstant : IProjectionFor<DecFromEventSequenceOrder>
{
public void Define(IProjectionBuilderFor<DecFromEventSequenceOrder> builder) => builder
// Using a constant instead of a raw string keeps the sequence identifier consistent
// wherever it is referenced.
.FromEventSequence(DecFromEventSequenceEventSequences.OrderManagement)
.From<DecFromEventSequenceOrderCreated>();
}

View C# snippet source on GitHub

Selecting a sequence does not require a special read model shape. Declare the properties needed by the order query:

public record DecFromEventSequenceOrder(
string OrderNumber,
string CustomerId,
decimal TotalAmount,
DecFromEventSequenceOrderStatus Status,
DateTimeOffset CreatedAt,
DateTimeOffset? ShippedAt);
public enum DecFromEventSequenceOrderStatus
{
Created,
Processing,
Shipped,
Delivered,
Cancelled
}

View C# snippet source on GitHub

These are the event contracts handled by the order projection. Their mappings work as usual; the sequence selection determines where Chronicle reads them from.

using Cratis.Chronicle.Events;
[EventType]
public record DecFromEventSequenceOrderCreated(
string OrderNumber,
string CustomerId,
decimal TotalAmount);
[EventType]
public record DecFromEventSequenceOrderUpdated(
string OrderNumber,
decimal NewTotalAmount);
[EventType]
public record DecFromEventSequenceOrderShipped(
string OrderNumber,
DateTimeOffset ShippedAt);

View C# snippet source on GitHub

A projection reads from one selected sequence. Its observer tracks progress using sequence numbers from that sequence, and matching event types elsewhere are outside that subscription.

Sequence order is append order, not necessarily occurrence-time order. Sequence numbers identify positions within one sequence; they do not establish an order across several sequences. See event sequences and observer offsets for how reading resumes.

Define separate projections when order management and shipping read from different sequences. Each builder selects its own source and maps the events relevant to its read model:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record DecFromEventSequencePackageCreated(string PackageId);
[EventType]
public record DecFromEventSequencePackageShipped(string PackageId, DateTimeOffset ShippedAt);
[EventType]
public record DecFromEventSequencePackageDelivered(string PackageId, DateTimeOffset DeliveredAt);
public record DecFromEventSequenceShipping(
string PackageId,
DateTimeOffset? ShippedAt,
DateTimeOffset? DeliveredAt);
// Projection for order management events
public class DecFromEventSequenceMultiOrderProjection : IProjectionFor<DecFromEventSequenceOrder>
{
public void Define(IProjectionBuilderFor<DecFromEventSequenceOrder> builder) => builder
.FromEventSequence("order-management")
.From<DecFromEventSequenceOrderCreated>(_ => _
.Set(m => m.Status).ToValue(DecFromEventSequenceOrderStatus.Created));
}
// Projection for shipping events from a different sequence
public class DecFromEventSequenceShippingProjection : IProjectionFor<DecFromEventSequenceShipping>
{
public void Define(IProjectionBuilderFor<DecFromEventSequenceShipping> builder) => builder
.FromEventSequence("shipping-management")
.From<DecFromEventSequencePackageCreated>()
.From<DecFromEventSequencePackageShipped>()
.From<DecFromEventSequencePackageDelivered>();
}

View C# snippet source on GitHub

Choose the sequence used by the code that appends the events your query needs. For example, a shipping projection should read the shipping sequence if that is where shipment events are recorded.

Selecting a sequence does not configure tenant isolation or authorization, and it does not merge events from several sequences.

For local event mappings without an explicit sequence selection, the projection uses event-log. Leave that default when your application appends the required events to the event log. Adding more From mappings does not make the projection read other sequences.

Sequence selection defines the input to the projection; it is not a performance guarantee. Consider the selected sequence’s event volume and the work performed by the mappings when evaluating processing cost.

Once the source is selected, use FromEvery for mappings shared by the event types the projection handles, or FromAll when every event type in that sequence should contribute.