Skip to content

FromEvery: update audit fields from handled events

FromEvery updates a read model property whenever the projection processes a handled event. Use it to maintain a last-modified timestamp or identify the operation that last changed the model without repeating the mapping for each event type.

An event-specific mapping runs only for its declared event type. An every-event mapping runs across the projection’s handled types; it does not subscribe to unrelated types in the sequence. Use From All when you need that wider subscription.

Event context is the metadata carried alongside an event, such as occurrence time, sequence number, and correlation id. Its shared shape lets you maintain audit fields without requiring unrelated event payloads to contain the same properties:

Update an audit timestamp from every event
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record InventoryProductRegisteredForEvery(string ProductName);
[EventType]
public record InventoryItemsAdjustedForEvery(int Quantity);
[FromEvent<InventoryProductRegisteredForEvery>]
[FromEvent<InventoryItemsAdjustedForEvery>]
public record InventoryStatusFromEvery(
[Key] Guid Id,
string ProductName,
[FromEvery(contextProperty: nameof(EventContext.Occurred))]
DateTimeOffset LastUpdated);

View C# snippet source on GitHub

Map more than one context field when readers need to know both when the model changed and which operation changed it. These fields describe the latest handled event, not a history of changes:

Track audit metadata from every event
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record AuditableInventoryChangedForEvery(string Reason);
[FromEvent<AuditableInventoryChangedForEvery>]
public record AuditableInventoryStatusFromEvery(
[Key] Guid Id,
[FromEvery(contextProperty: nameof(EventContext.Occurred))]
DateTimeOffset LastModified,
[FromEvery(contextProperty: nameof(EventContext.SequenceNumber))]
EventSequenceNumber LastEventSequence,
[FromEvery(contextProperty: nameof(EventContext.CorrelationId))]
string LastCorrelationId);

View C# snippet source on GitHub

Every-event mappings can also read from event payload properties. Use this only when each event in the projection carries the same property name and compatible value type.

Read a shared event property from every event
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
public enum OrderStateFromEvery
{
New,
Confirmed,
Shipped
}
[EventType]
public record OrderConfirmedForEvery(OrderStateFromEvery Status);
[EventType]
public record OrderShippedForEvery(OrderStateFromEvery Status);
[FromEvent<OrderConfirmedForEvery>]
[FromEvent<OrderShippedForEvery>]
public record OrderStatusFromEvery(
[Key] Guid Id,
[FromEvery(property: nameof(OrderConfirmedForEvery.Status))]
OrderStateFromEvery CurrentStatus);

View C# snippet source on GitHub

If a client supports convention-based property mapping, omitting the source property uses the read model property name.

Use the read model property name by convention
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record ProductRenamedForEveryConvention(string Name, int Version);
[EventType]
public record ProductPriceChangedForEveryConvention(decimal Price, int Version);
[FromEvent<ProductRenamedForEveryConvention>]
[FromEvent<ProductPriceChangedForEveryConvention>]
public record ProductVersionFromEveryConvention(
[Key] Guid Id,
string Name,
decimal Price,
[FromEvery]
int Version);

View C# snippet source on GitHub

Keep business changes in event-specific mappings and share the audit mapping across them. Chronicle applies both while processing the event:

Combine specific mappings with every-event metadata
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record UserRegisteredForEvery(string Name, string Email);
[EventType]
public record UserNameChangedForEvery(string NewName);
[EventType]
public record UserEmailChangedForEvery(string NewEmail);
[FromEvent<UserRegisteredForEvery>]
[FromEvent<UserNameChangedForEvery>]
[FromEvent<UserEmailChangedForEvery>]
public record UserProfileFromEvery(
[Key] Guid Id,
[SetFrom<UserRegisteredForEvery>(nameof(UserRegisteredForEvery.Name))]
[SetFrom<UserNameChangedForEvery>(nameof(UserNameChangedForEvery.NewName))]
string Name,
[SetFrom<UserRegisteredForEvery>(nameof(UserRegisteredForEvery.Email))]
[SetFrom<UserEmailChangedForEvery>(nameof(UserEmailChangedForEvery.NewEmail))]
string Email,
[FromEvery(contextProperty: nameof(EventContext.Occurred))]
DateTimeOffset LastUpdated);

View C# snippet source on GitHub

When the name-change event is processed, the name mapping updates the business field and the every-event mapping records the event’s occurrence time in the last-modified field.

If you move the mapping into a standalone projection, the intent stays the same. Both styles send an every-event definition to Chronicle:

Declarative projection with every-event metadata
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections;
[EventType]
public record InventoryRegisteredDeclarativeForEvery(string ProductName);
[EventType]
public record InventoryAdjustedDeclarativeForEvery(int Quantity);
public record InventoryStatusDeclarativeFromEvery(
[property: Key] Guid Id,
string ProductName,
DateTimeOffset LastUpdated);
public class InventoryStatusDeclarativeProjection : IProjectionFor<InventoryStatusDeclarativeFromEvery>
{
public void Define(IProjectionBuilderFor<InventoryStatusDeclarativeFromEvery> builder) => builder
.From<InventoryRegisteredDeclarativeForEvery>()
.From<InventoryAdjustedDeclarativeForEvery>()
.FromEvery(_ => _
.Set(m => m.LastUpdated)
.ToEventContextProperty(c => c.Occurred));
}

View C# snippet source on GitHub

Use case Typical source
Last modification time Event context occurrence time
Last event sequence Event context sequence number
Last operation id Event context correlation id
Current status shared by all events Event payload property

Prefer context properties for audit metadata. Use payload properties only when the event contracts are intentionally consistent. If a property should update only for selected event types, use an event-specific mapping instead.

In .NET, a root [FromEvery] includes events handled only by child projections, including nested child collections. Chronicle uses the child’s resolved parent key to update the containing parent’s audit fields. It does not create a root document keyed by the child identifier.

A child-only event advances a root [FromEvery] timestamp even without a root [FromEvent] for that event. In the declaration language, every includes child events by default; use exclude children to keep mappings local to their level.

Put [FromEvery] on a child’s own property when the child needs its own audit value. That mapping applies on the creating event and later events handled by the child. The same ownership rule applies to nested objects and [FromAll]: the object declaring the property owns the mapping. Parent fields are never copied into a newly added child.

For compatibility with clients that still put child mappings at the root, see Child and nested properties. A property declared on both root and child cannot be disambiguated until the client is upgraded.

For child or nested every-mappings on join-only events, use the declarative alternative in model-bound limitations.

  • Only one every-event mapping can target the same read model property.
  • Payload-property mappings are skipped for events that do not carry the source property. A source property that is present with a null value still maps null.
  • Model-bound [FromEvery] has no child-exclusion option. Use a declarative projection with ExcludeChildProjections() when you need to exclude child projections.