Skip to content

Cratis Chronicle FromAll: map every event type

FromAll in Cratis Chronicle subscribes a projection to every event type in its event sequence, including types with no explicit From mapping. Use it in the .NET fluent API for results such as per-source event counts. Use FromEvery when only the event types your projection already handles should contribute.

Events handled by the all-events subscription can create or update a read model instance for any event source. The default key is the event source ID. If you configure another all-events key, those events use that key instead.

Define the values that every event should update without listing individual event types:

Declarative FromAll
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record UserCreatedDeclarativeAll(string Name, string Email);
[EventType]
public record UserEmailChangedDeclarativeAll(string Email);
public record UserProfileDeclarativeAll(
string Name,
string Email,
DateTimeOffset LastUpdated);
public class UserProfileDeclarativeAllProjection : IProjectionFor<UserProfileDeclarativeAll>
{
public void Define(IProjectionBuilderFor<UserProfileDeclarativeAll> builder) => builder
.From<UserCreatedDeclarativeAll>()
.From<UserEmailChangedDeclarativeAll>()
.FromAll(_ => _
.Set(m => m.LastUpdated)
.ToEventContextProperty(c => c.Occurred));
}

View C# snippet source on GitHub

Map metadata such as Occurred to retain the time of the latest contributing event. Context is available even when event payloads have no properties in common.

Map context fields with FromAll
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record AccountTouchedDeclarativeAll(string Reason);
public record AccountAuditDeclarativeAll(
DateTimeOffset LastUpdated,
EventSequenceNumber LastEventSequence,
string LastCorrelationId);
public class AccountAuditDeclarativeAllProjection : IProjectionFor<AccountAuditDeclarativeAll>
{
public void Define(IProjectionBuilderFor<AccountAuditDeclarativeAll> builder) => builder
.From<AccountTouchedDeclarativeAll>()
.FromAll(_ => _
.Set(m => m.LastUpdated).ToEventContextProperty(c => c.Occurred)
.Set(m => m.LastEventSequence).ToEventContextProperty(c => c.SequenceNumber)
.Set(m => m.LastCorrelationId).ToEventContextProperty(c => c.CorrelationId));
}

View C# snippet source on GitHub

Use ToEventSourceId() to retain the contributing event’s source ID in a read model property:

Map event source id with FromAll
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record AccountOpenedDeclarativeAll(string OwnerName);
public record AccountSummaryDeclarativeAll(
string AccountId,
string OwnerName);
public class AccountSummaryDeclarativeAllProjection : IProjectionFor<AccountSummaryDeclarativeAll>
{
public void Define(IProjectionBuilderFor<AccountSummaryDeclarativeAll> builder) => builder
.From<AccountOpenedDeclarativeAll>()
.FromAll(_ => _
.Set(m => m.AccountId)
.ToEventSourceId());
}

View C# snippet source on GitHub

Combine FromAll with From to map business fields for selected event types and metadata for every type. An event declared in From runs the all-events mappings through that registration once and uses its configured key.

Unrelated event types still contribute. They can create instances without the business fields set by the event-specific mappings. Choose FromEvery instead if that would leave read models your queries cannot use.

Combine FromAll with event-specific mappings
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record OrderCreatedDeclarativeAll(string OrderNumber);
[EventType]
public record OrderShippedDeclarativeAll(string TrackingNumber);
public record OrderDeclarativeAll(
string OrderNumber,
string Status,
DateTimeOffset LastModified);
public class OrderDeclarativeAllProjection : IProjectionFor<OrderDeclarativeAll>
{
public void Define(IProjectionBuilderFor<OrderDeclarativeAll> builder) => builder
.FromAll(_ => _
.Set(m => m.LastModified)
.ToEventContextProperty(c => c.Occurred))
.From<OrderCreatedDeclarativeAll>(_ => _
.Set(m => m.OrderNumber).To(e => e.OrderNumber)
.Set(m => m.Status)
.ToValue("Placed"))
.From<OrderShippedDeclarativeAll>(_ => _
.Set(m => m.Status)
.ToValue("Shipped"));
}

View C# snippet source on GitHub

A declared Join event type is excluded from the separate all-events subscription so a matched event is not counted twice. Its all-events mappings run through the join instead.

If the join finds no read model instance, those mappings do not fall back to creating an instance under the event source ID. Keep this in mind when using FromAll to count events: a join-only event with no matching instance will not contribute a count.

FromAll includes child-projection event types and has no child-exclusion switch. Child-only events apply its mappings once to their containing parent, using the child’s resolved parent key. Unrelated event types use the configured all-events key. The root all block in the declaration language follows the same subscription scope.

For mappings limited to one projection level, use FromEvery with ExcludeChildProjections(), or every with exclude children in a declaration.

Counting into a dictionary with a dynamic key

Section titled “Counting into a dictionary with a dynamic key”

Use Count, Increment, or Decrement on a dictionary property when you need a separate counter for each context value. The key is resolved for each event from EventContext; this example uses the event type identifier. Dictionary keys are strings. The kernel stores counts using the dictionary’s declared value type, or int64 when the dictionary declares none. See dictionary keys for every key source and for removing keys.

Declarative FromAll with a dynamic dictionary key
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record UserRegisteredForEventCounts(string Name);
[EventType]
public record OrderPlacedForEventCounts(string OrderId);
public record EventTypeCountsReadModel(
Guid Id,
Dictionary<string, int> EventCountByType,
DateTimeOffset LastEventOccurred);
public class EventTypeCountsProjection : IProjectionFor<EventTypeCountsReadModel>
{
public void Define(IProjectionBuilderFor<EventTypeCountsReadModel> builder) => builder
.FromAll(_ => _
.Count(m => m.EventCountByType, c => c.EventType.Id)
.Set(m => m.LastEventOccurred).ToEventContextProperty(c => c.Occurred));
}

View C# snippet source on GitHub

The model-bound [FromAll] attribute does not support this dictionary form. Use the fluent API shown above.

API Use it when
FromAll Every event type in the sequence should contribute, including types not declared by the projection.
FromEvery You want mappings applied to the projection’s own handled event types, without subscribing to unrelated event types.

Prefer event context for shared metadata fields because it has a consistent shape across event types. Use payload properties only when the contributing event contracts intentionally share those properties.