Cratis Chronicle closing stream constraints
A Cratis Chronicle closing stream constraint stops further appends within a chosen scope after a domain event closes it. In .NET, apply [ClosesStream] to the closing event type; later appends inside that scope receive a StreamClosed violation.
The client discovers the attribute on registered event types and registers the constraint with the Kernel. Closure follows the durable append in the same Kernel grain turn, but the two writes are not crash-atomic. An explicit reindex can repair a missing closure update. See Closing events for the guarantees and when to prefer this workflow over CompleteStream.
Defining a closing constraint
Section titled “Defining a closing constraint”Choose which events the closure should stop. A scope is the combination of identifiers that must match a later append for the closure to reject it:
using Cratis.Chronicle.EventSequences;using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Constraints;
[EventType][ClosesStream(Dimensions = ClosedStreamDimensions.EventSourceId | ClosedStreamDimensions.EventStreamType)]public record ConstraintsModelBoundClosingReportDelivered(string Recipient);Dimensions combines ClosedStreamDimensions flags: EventSourceId, EventSourceType, EventStreamType, and EventStreamId. If you leave it unset, the closure covers the append’s event source id, stream type, and stream id. Appends outside that combination remain unaffected.
Take the stream id from a property
Section titled “Take the stream id from a property”When the closing event carries the stream id in its payload, set EventStreamIdFrom to that property name. The closure uses the property’s value instead of the append’s stream id, and includes the EventStreamId dimension automatically:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Constraints;
[EventType][ClosesStream(EventStreamIdFrom = nameof(Period))]public record ConstraintsModelBoundClosingBooksClosed(string Period, decimal ClosingBalance);The property must have a non-empty value on every append. The kernel refuses an append where it is missing, null or empty. Avoid a nullable property; the analyzer CHR0012 warns on nullable event properties.
Name and reopening events
Section titled “Name and reopening events”Set Name to identify which constraint owns the closure; it defaults to the event type name. Add event types to ReopenedBy when the domain allows that scope to open again. Each reopening event removes only this constraint’s closure for the exact scope it resolves to:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Constraints;
[EventType][ClosesStream(Name = "close-month", EventStreamIdFrom = nameof(Month), ReopenedBy = [typeof(ConstraintsModelBoundClosingMonthReopened)])]public record ConstraintsModelBoundClosingMonthClosed(string Month);
[EventType]public record ConstraintsModelBoundClosingMonthReopened(string Month, string Reason);If you set EventStreamIdFrom, every reopening event type must carry that property too. Otherwise, the client throws PropertyDoesNotExistOnEventType when it discovers the constraint. The property must also have a non-empty value when you append the reopening event.
Reopening does not remove an operator’s manual closure or another constraint’s closure. Those closures can still prevent the reopening event from being appended.
Attribute reference
Section titled “Attribute reference”| Property | Meaning |
|---|---|
Dimensions |
Dimensions of the closed scope. Defaults to event source id, stream type and stream id |
EventStreamIdFrom |
Payload property that provides the stream id |
ReopenedBy |
Event types that reopen this constraint’s scope |
Name |
Owner name. Defaults to the event type name. Cannot be empty |
EventSequences |
Event sequences the declaration applies to. Empty means all |
Declarations that share a Name merge into one constraint and must use the same dimensions and stream id property, otherwise the client throws ConflictingClosesStreamDeclarations.
Registering or changing the declaration does not close scopes for events already in the log. Only later appends apply the declaration unless you explicitly reindex the history.