Skip to content

Nested model-bound projections: populate and clear one object

A nested model-bound projection lets events populate, update, or clear one nullable object inside a parent read model. Use it for an optional part of the view, such as a command definition, whose fields change together. Use a child collection when the parent can contain several independently identified items.

The examples below use the .NET [Nested] attribute. TypeScript exposes @nested, and Kotlin and Java expose @Nested.

Mark the nullable parent property as nested, then declare the populate events on the nested type. Add clear events if the relationship can end:

Nested object lifecycle
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record CommandSetForNestedBasic(string Name, string Schema);
[EventType]
public record CommandClearedForNestedBasic;
[FromEvent<CommandSetForNestedBasic>]
public record SliceWithNestedCommandBasic(
[Key] Guid Id,
string Name,
[Nested] CommandItemNestedBasic? Command);
[FromEvent<CommandSetForNestedBasic>]
[ClearWith<CommandClearedForNestedBasic>]
public record CommandItemNestedBasic(
string Name,
string Schema);

View C# snippet source on GitHub

The object starts empty. As events arrive:

  1. The first populate event creates the nested object.
  2. Later populate events update the existing nested object.
  3. A clear event sets the parent property back to null.

Put [Nested] on the parent property that holds the object. Keeping it nullable represents both the initial empty state and the state after a clear event:

Nested property on the parent
using Cratis.Chronicle.Projections.ModelBound;
public record ParentWithNestedProperty(
[Nested] NestedPropertyChild? Child);
public record NestedPropertyChild(
string Name,
string Description);

View C# snippet source on GitHub

The nested type is scanned for its own model-bound projection annotations, including FromEvent, ClearWith, and property mappings.

Apply a clear annotation to the nested type when one event should remove the nested object from the parent.

Clear a nested object
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record CommandSetForNestedClear(string Name, string Schema);
[EventType]
public record CommandClearedForNestedClear;
[FromEvent<CommandSetForNestedClear>]
[ClearWith<CommandClearedForNestedClear>]
public record CommandItemNestedClear(
string Name,
string Schema);

View C# snippet source on GitHub

Use multiple clear annotations when more than one event should clear the same nested object.

Clear a nested object from multiple events
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record CommandSetForNestedMultipleClear(string Name, string Schema);
[EventType]
public record CommandClearedForNestedMultipleClear;
[EventType]
public record SliceArchivedForNestedMultipleClear;
[FromEvent<CommandSetForNestedMultipleClear>]
[ClearWith<CommandClearedForNestedMultipleClear>]
[ClearWith<SliceArchivedForNestedMultipleClear>]
public record CommandItemNestedMultipleClear(
string Name,
string Schema);

View C# snippet source on GitHub

You can also put the clear annotation on the parent property. This is useful when several parents share the nested type but different events end their relationships. To clear one member while keeping the nested object, put the annotation on that member. See Clearing Values for that distinction.

Declare each event that can populate or update the nested type. Matching property names are auto-mapped by default, so a rename event can update Name while a schema-change event updates Schema:

Update a nested object from multiple events
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record CommandSetForNestedMultipleFrom(string Name, string Schema);
[EventType]
public record CommandRenamedForNestedMultipleFrom(string Name);
[EventType]
public record CommandSchemaUpdatedForNestedMultipleFrom(string Schema);
[EventType]
public record CommandClearedForNestedMultipleFrom;
[FromEvent<CommandSetForNestedMultipleFrom>]
[FromEvent<CommandRenamedForNestedMultipleFrom>]
[FromEvent<CommandSchemaUpdatedForNestedMultipleFrom>]
[ClearWith<CommandClearedForNestedMultipleFrom>]
public record CommandItemNestedMultipleFrom(
string Name,
string Schema);

View C# snippet source on GitHub

When event property names differ from nested object property names, add explicit property mappings to the nested type.

Explicit mappings on a nested type
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record CommandSetForNestedExplicit(string CommandName, string JsonSchema);
[EventType]
public record CommandSchemaUpdatedForNestedExplicit(string UpdatedSchema);
[EventType]
public record CommandClearedForNestedExplicit;
[FromEvent<CommandSetForNestedExplicit>]
[FromEvent<CommandSchemaUpdatedForNestedExplicit>]
[ClearWith<CommandClearedForNestedExplicit>]
public record CommandItemNestedExplicit(
[SetFrom<CommandSetForNestedExplicit>(nameof(CommandSetForNestedExplicit.CommandName))]
string Name,
[SetFrom<CommandSetForNestedExplicit>(nameof(CommandSetForNestedExplicit.JsonSchema))]
[SetFrom<CommandSchemaUpdatedForNestedExplicit>(nameof(CommandSchemaUpdatedForNestedExplicit.UpdatedSchema))]
string Schema);

View C# snippet source on GitHub

AutoMap is enabled by default for nested types. Disable it on the nested type when every property should be mapped explicitly.

Disable AutoMap on a nested type
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record CommandSetForNestedNoAutoMap(string CommandName, string Schema);
[EventType]
public record CommandClearedForNestedNoAutoMap;
[FromEvent<CommandSetForNestedNoAutoMap>]
[ClearWith<CommandClearedForNestedNoAutoMap>]
[NoAutoMap]
public record CommandItemNestedNoAutoMap(
[SetFrom<CommandSetForNestedNoAutoMap>(nameof(CommandSetForNestedNoAutoMap.CommandName))]
string Name,
[SetFrom<CommandSetForNestedNoAutoMap>(nameof(CommandSetForNestedNoAutoMap.Schema))]
string Schema);

View C# snippet source on GitHub

A parent can hold more than one nested object. Each nested property points to a type with its own populate and clear events:

Multiple nested objects on one parent
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record CommandSetForNestedMultiple(string Name, string Schema);
[EventType]
public record CommandClearedForNestedMultiple;
[EventType]
public record ValidationConfiguredForNestedMultiple(string Rules, bool IsStrict);
[EventType]
public record ValidationRemovedForNestedMultiple;
public record SliceWithMultipleNestedObjects(
string Name,
[Nested] CommandItemNestedMultiple? Command,
[Nested] ValidationConfigNestedMultiple? Validation);
[FromEvent<CommandSetForNestedMultiple>]
[ClearWith<CommandClearedForNestedMultiple>]
public record CommandItemNestedMultiple(string Name, string Schema);
[FromEvent<ValidationConfiguredForNestedMultiple>]
[ClearWith<ValidationRemovedForNestedMultiple>]
public record ValidationConfigNestedMultiple(string Rules, bool IsStrict);

View C# snippet source on GitHub

A child collection item can hold a nested object with its own lifecycle:

Nested object inside child collection items
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record TaskAddedForNestedChildren(Guid TaskId, string Title);
[EventType]
public record TaskAssignedForNestedChildren(Guid TaskId, string Name, string Email);
[EventType]
public record TaskUnassignedForNestedChildren(Guid TaskId);
public record ProjectWithNestedChildren(
[Key] Guid Id,
string Name,
[ChildrenFrom<TaskAddedForNestedChildren>(key: nameof(TaskAddedForNestedChildren.TaskId))]
IEnumerable<ProjectTaskWithNestedAssignee> Tasks);
public record ProjectTaskWithNestedAssignee(
[Key] Guid TaskId,
string Title,
[Nested] TaskAssigneeNestedChild? Assignee);
[FromEvent<TaskAssignedForNestedChildren>]
[ClearWith<TaskUnassignedForNestedChildren>]
public record TaskAssigneeNestedChild(
string Name,
string Email);

View C# snippet source on GitHub

Joins inside such a nested object are not supported by the kernel. Keep those join mappings on the collection item itself; see Joins.

The parent in this example keeps an optional command definition. Setting the command populates it, later events change selected fields, and clearing it leaves the parent read model in place:

Complete nested object projection
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record SliceCreatedForNestedComplete(string Name);
[EventType]
public record CommandSetForNestedComplete(
Guid CommandId,
string Name,
string Schema,
string Rules,
string StateSchema);
[EventType]
public record CommandRenamedForNestedComplete(Guid CommandId, string Name);
[EventType]
public record CommandDefinitionUpdatedForNestedComplete(
Guid CommandId,
string Schema,
string Rules,
string StateSchema);
[EventType]
public record CommandClearedForNestedComplete;
[FromEvent<SliceCreatedForNestedComplete>]
public record SliceNestedComplete(
[Key] Guid Id,
string Name,
[Nested] CommandItemNestedComplete? Command);
[FromEvent<CommandSetForNestedComplete>]
[FromEvent<CommandRenamedForNestedComplete>]
[FromEvent<CommandDefinitionUpdatedForNestedComplete>]
[ClearWith<CommandClearedForNestedComplete>]
public record CommandItemNestedComplete(
[SetFrom<CommandSetForNestedComplete>(nameof(CommandSetForNestedComplete.CommandId))]
Guid Id,
[SetFrom<CommandSetForNestedComplete>(nameof(CommandSetForNestedComplete.Name))]
[SetFrom<CommandRenamedForNestedComplete>(nameof(CommandRenamedForNestedComplete.Name))]
string Name,
[SetFrom<CommandSetForNestedComplete>(nameof(CommandSetForNestedComplete.Schema))]
[SetFrom<CommandDefinitionUpdatedForNestedComplete>(nameof(CommandDefinitionUpdatedForNestedComplete.Schema))]
string Schema,
[SetFrom<CommandSetForNestedComplete>(nameof(CommandSetForNestedComplete.Rules))]
[SetFrom<CommandDefinitionUpdatedForNestedComplete>(nameof(CommandDefinitionUpdatedForNestedComplete.Rules))]
string Rules,
[SetFrom<CommandSetForNestedComplete>(nameof(CommandSetForNestedComplete.StateSchema))]
[SetFrom<CommandDefinitionUpdatedForNestedComplete>(nameof(CommandDefinitionUpdatedForNestedComplete.StateSchema))]
string StateSchema);

View C# snippet source on GitHub

Event Effect on the nested object
SliceCreatedForNestedComplete Creates the parent read model; the nested command remains null.
CommandSetForNestedComplete Populates the command object.
CommandRenamedForNestedComplete Updates the command name in place.
CommandDefinitionUpdatedForNestedComplete Updates command definition fields in place.
CommandClearedForNestedComplete Sets the command property to null.
Annotation Works on nested type
FromEvent Yes
ClearWith Yes
SetFrom Yes
AddFrom / SubtractFrom Yes
SetFromContext Yes
Increment / Decrement / Count Yes
Join Supported on a root-level nested object, not on a nested object inside a child collection.
Nested recursively Yes
ChildrenFrom inside nested objects Not supported by the kernel.
NoAutoMap Yes

Keep the populate events on the nested type and place clear annotations where the lifecycle is owned: on that type for a shared lifecycle, or on the parent property for an owner-specific one. Use AutoMap while names match and add explicit mappings where they differ.

For multiple independent objects, move to a child collection. Nested objects cannot contain child collections, and removal through a join is not supported for nested objects.