Declare Chronicle closing stream constraints
A closing stream constraint stops later appends in a chosen scope after a domain event, such as delivering a report or closing an accounting period. Declare it with ClosesStreamOn<TEvent> on the .NET constraint builder. Later appends inside the closed scope receive a StreamClosed violation.
The event becomes durable before the closure index is updated. Both happen in one kernel execution turn, so another append cannot interleave, but they are not crash-atomic. A lost update needs index recovery or reindexing. Read Closing events for the guarantees before relying on closure. To put the declaration on an event type instead, use the model-bound closing stream constraint.
Defining a closing constraint
Section titled “Defining a closing constraint”Choose the scope first: its dimensions are the identifiers that must match for an append to be blocked. This example closes every stream id for the report’s event source and stream type, because it selects those two dimensions but not stream id:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Constraints;
[EventType]public record ConstraintsDeclarativeClosingReportDelivered(string Recipient);
public class ConstraintsDeclarativeClosingReportDelivery : IConstraint{ public void Define(IConstraintBuilder builder) => builder.ClosesStreamOn<ConstraintsDeclarativeClosingReportDelivered>( closes => closes.PerEventSourceId().PerEventStreamType());}Use the Per... methods inside the configuration callback to select the dimensions:
| Method | Adds the dimension |
|---|---|
PerEventSourceId() |
Event source id |
PerEventSourceType() |
Event source type |
PerEventStreamType() |
Event stream type |
PerEventStreamId() |
Event stream id |
If you call none of them, the scope includes the append’s event source id, stream type, and stream id together. Selecting only PerEventSourceId() is broader: it blocks every stream for that source. Closure stops new writes; it does not remove stored events or prevent reads and replays.
Take the stream id from a property
Section titled “Take the stream id from a property”Use EventStreamIdFrom when the closing event names the scope in its payload. Here, Period supplies the stream id, so each period can close separately for the same event source. The call also includes the stream id dimension:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Constraints;
[EventType]public record ConstraintsDeclarativeClosingBooksClosed(string Period, decimal ClosingBalance);
public class ConstraintsDeclarativeClosingBooks : IConstraint{ public void Define(IConstraintBuilder builder) => builder.ClosesStreamOn<ConstraintsDeclarativeClosingBooksClosed>( closes => closes.PerEventSourceId().EventStreamIdFrom(@event => @event.Period));}The property must have a non-empty value on every closing or reopening event. The kernel refuses an append where it is missing, null, or empty. Avoid a nullable property; the analyzer CHR0012 warns on nullable event properties.
Reopening events and the constraint name
Section titled “Reopening events and the constraint name”Call ReopenedBy<T>() when a domain event should allow writes again. It removes only this constraint’s closure for the exact scope the event resolves to, not a manual closure or another constraint’s closure. The optional name argument identifies the closure’s owner and defaults to the closing event type identifier:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Constraints;
[EventType]public record ConstraintsDeclarativeClosingMonthClosed(string Month);
[EventType]public record ConstraintsDeclarativeClosingMonthReopened(string Month, string Reason);
public class ConstraintsDeclarativeClosingMonth : IConstraint{ public void Define(IConstraintBuilder builder) => builder.ClosesStreamOn<ConstraintsDeclarativeClosingMonthClosed>( closes => closes .PerEventSourceId() .EventStreamIdFrom(@event => @event.Month) .ReopenedBy<ConstraintsDeclarativeClosingMonthReopened>(), name: "close-month");}When you use EventStreamIdFrom, the reopening event type must carry the same property as the closing event. In this example, reopening one month leaves every other month’s closure unchanged.
Several closing events, one constraint
Section titled “Several closing events, one constraint”Declarations that share a name merge into one constraint, so a scope can be closed by more than one event type. They must use the same dimensions and stream id property, otherwise the client throws ConflictingClosesStreamDeclarations:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Constraints;
[EventType]public record ConstraintsDeclarativeClosingReportDeliveredToClient(string Version);
[EventType]public record ConstraintsDeclarativeClosingReportSuperseded(string Version);
public class ConstraintsDeclarativeClosingVersions : IConstraint{ public void Define(IConstraintBuilder builder) { // Two declarations that share a name merge into one constraint. They must use the same dimensions. builder.ClosesStreamOn<ConstraintsDeclarativeClosingReportDeliveredToClient>( closes => closes.PerEventSourceId().EventStreamIdFrom(@event => @event.Version), name: "close-report-version"); builder.ClosesStreamOn<ConstraintsDeclarativeClosingReportSuperseded>( closes => closes.PerEventSourceId().EventStreamIdFrom(@event => @event.Version), name: "close-report-version"); }}Registering or changing the declaration does not apply it to historical events. If you need to rebuild event-driven closures from the log, review the recovery and redaction limits in Closing events before reindexing.