Nested Objects
Nested objects define a single nullable child object within a projection. Unlike children, which manage collections of items, nested targets a scalar property that holds one optional object — set when a specific event occurs and cleared (set to null) when another event occurs.
Basic Syntax
Section titled “Basic Syntax”nested {PropertyName} from {SetEvent} clear with {ClearEvent}Simple Example
Section titled “Simple Example”projection Slice => SliceReadModel from SliceCreated Name = name
nested command from CommandSetForSlice Name = commandName Schema = schema clear with CommandClearedForSliceThis creates a command property on SliceReadModel that is populated from CommandSetForSlice and set back to null when CommandClearedForSlice is appended.
From Event
Section titled “From Event”The from block inside nested works exactly like a top-level from block — you can map properties explicitly or rely on AutoMap:
nested command from CommandSetForSlice Name = commandName Schema = schema Rules = validationRulesClear With
Section titled “Clear With”Use clear with to specify which event nulls the nested object:
nested command from CommandSetForSlice clear with CommandClearedForSliceWhen a CommandClearedForSlice event is appended, the command property on the parent is set to null.
Multiple From Events
Section titled “Multiple From Events”The nested object can be updated by more than one event:
nested command from CommandSetForSlice Name = commandName Schema = schema
from CommandRenamed Name = newName
from CommandSchemaUpdated Schema = updatedSchema
clear with CommandClearedForSliceEach from can map a different subset of properties. Subsequent events update only the properties they map — they do not replace the entire nested object.
With AutoMap
Section titled “With AutoMap”Apply AutoMap to avoid explicit property mappings when names match:
nested command automap
from CommandSetForSlice from CommandUpdated clear with CommandClearedForSliceEvery Block in Nested
Section titled “Every Block in Nested”Use an every block inside nested to apply mappings across all events that affect the nested object:
nested command every LastModified = $eventContext.occurred
from CommandSetForSlice Name = commandName Schema = schema
from CommandUpdated Schema = updatedSchema
clear with CommandClearedForSliceLastModified is updated on every event that touches the command property.
Nested Within Nested
Section titled “Nested Within Nested”A nested object can itself contain another nested object:
nested command from CommandSetForSlice Name = commandName
nested validation from ValidationConfigured Rules = rules clear with ValidationRemoved
clear with CommandClearedForSliceNested Within Children
Section titled “Nested Within Children”A nested block can appear inside a children block:
children tasks id taskId from TaskAdded key taskId parent projectId Title = title
nested assignee from TaskAssigned Name = assigneeName Email = assigneeEmail clear with TaskUnassignedRead Model Structure
Section titled “Read Model Structure”The nested property maps to a nullable property on the parent read model:
public class SliceReadModel{ public string Name { get; set; } public CommandItem? Command { get; set; } // nullable — null until set}
public class CommandItem{ public string Name { get; set; } public string Schema { get; set; }}Examples
Section titled “Examples”Slice with Optional Command
Section titled “Slice with Optional Command”projection Slice => SliceReadModel from SliceCreated Name = name
nested command automap
from CommandSetForSlice from CommandRenamed Name = newName
clear with CommandClearedForSliceEmployee with Optional Active Contract
Section titled “Employee with Optional Active Contract”projection Employee => EmployeeReadModel from EmployeeHired Name = name Department = department
nested activeContract from ContractStarted ContractId = contractId StartDate = startDate EndDate = endDate Type = contractType
from ContractExtended EndDate = newEndDate
clear with ContractEndedProduct with Optional Promotion
Section titled “Product with Optional Promotion”projection Product => ProductReadModel from ProductListed Name = name BasePrice = price
nested promotion from PromotionApplied Label = promotionName DiscountPercent = discount ValidUntil = expiresAt
clear with PromotionRemovedBest Practices
Section titled “Best Practices”- Nullable property: Always define the nested read model property as nullable (
T?) — it starts null and is only populated when thefromevent fires - Single responsibility: Model the nested type around one coherent concept; if it needs its own lifecycle or collection behaviour, use children instead
- Clear semantics: Provide a
clear withevent only when the nested object has a meaningful absent state — do not use it to model soft-deletion of the parent - AutoMap selectively: Use AutoMap when event property names align with the read model; use explicit mappings when the event contains extra fields you do not want on the nested object
See Also
Section titled “See Also”- Children — collections of items managed independently within a parent
- From Event — how event mapping works
- Auto-Map — automatic property mapping
- Removal — removing root read models