Skip to content

Declarative constraints

The declarative API lets you define constraints in a dedicated class by implementing IConstraint. Chronicle discovers all IConstraint implementations automatically — no registration is needed.

Use declarative constraints when you need:

  • Uniqueness spanning multiple event types with different property names
  • A callback to compose dynamic violation messages
  • More explicit control over constraint names
TypeDescription
Unique propertyEnforce that a property value is unique across one or more event types
Unique event typeEnforce that only one event of a specific type can be appended per event source

Implement IConstraint and call the builder in Define:

using Cratis.Chronicle.Events.Constraints;
public class UniqueProjectName : IConstraint
{
public void Define(IConstraintBuilder builder) =>
builder.Unique(unique =>
unique
.On<ProjectCreated>(e => e.Name)
.RemovedWith<ProjectRemoved>());
}

Chronicle scans all loaded assemblies for types that implement IConstraint and registers them with the Kernel automatically when the client starts. No explicit registration is required.