Declare Chronicle constraints with fluent rules
Declarative constraints keep an append-time rule in a dedicated class rather than on an event type. Use them when several events share one uniqueness rule, when a violation needs a dynamic message, or when you want to control the constraint’s name. In .NET, implement IConstraint and describe the rule through its fluent builder, chaining calls that select the events, properties, and release behavior.
For a rule that belongs directly on an event type or property, start with Model-bound constraints. Both approaches send definitions to the Chronicle Kernel, where the append is checked.
Constraint types
Section titled “Constraint types”| Rule you need | Constraint |
|---|---|
| Reserve a value, such as a project name, so another event source cannot claim it | Unique property |
| Allow an event only once per event source, or once per cycle ended by a release event | Unique event type |
| Stop later appends in a selected scope after a closing event | Closing stream |
Defining a constraint
Section titled “Defining a constraint”The following rule reserves a project’s name when it is created and releases the name when the project is removed. In .NET, put the declaration in Define:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Constraints;
[EventType]public record ConstraintsDeclarativeIndexProjectCreated(string Name);
[EventType]public record ConstraintsDeclarativeIndexProjectRemoved;
public class ConstraintsDeclarativeIndexUniqueProjectName : IConstraint{ public void Define(IConstraintBuilder builder) => builder.Unique(unique => unique .On<ConstraintsDeclarativeIndexProjectCreated>(e => e.Name) .RemovedWith<ConstraintsDeclarativeIndexProjectRemoved>());}Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.Events.ConstraintsDeclarativeIndexProjectCreated do use Cratis.Chronicle.Events.EventType, id: "constraints-declarative-index-project-created" defstruct name: ""end
defmodule MyApp.Events.ConstraintsDeclarativeIndexProjectRemoved do use Cratis.Chronicle.Events.EventType, id: "constraints-declarative-index-project-removed" defstruct []end
defmodule MyApp.Constraints.ConstraintsDeclarativeIndexUniqueProjectName do use Cratis.Chronicle.Events.Constraint
unique name: "ConstraintsDeclarativeIndexUniqueProjectName" do on MyApp.Events.ConstraintsDeclarativeIndexProjectCreated, [:name] removed_with MyApp.Events.ConstraintsDeclarativeIndexProjectRemoved endendimport { constraint, eventType, IConstraint, IConstraintBuilder } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class ConstraintsDeclarativeIndexProjectCreated { @field(String) readonly name: string;
constructor(name: string) { this.name = name; }}
@eventType()class ConstraintsDeclarativeIndexProjectRemoved {}
@constraint()class ConstraintsDeclarativeIndexUniqueProjectName implements IConstraint { define(builder: IConstraintBuilder): void { builder.unique(unique => unique .on(ConstraintsDeclarativeIndexProjectCreated, e => e.name) .removedWith(ConstraintsDeclarativeIndexProjectRemoved)); }}The name belongs to the project event source. Appending that same name again for the same project is allowed; a different project cannot claim it while it is held. Continue with Unique property constraints to choose how long values stay reserved.
Discovery
Section titled “Discovery”The .NET client’s default artifact discovery finds IConstraint implementations in the assemblies it scans and registers their definitions with the Kernel during client startup. You do not need to register each class separately when using that discovery.