Skip to content

Cratis Chronicle unique event type constraints

A Cratis Chronicle unique event type constraint allows an event type once per event source until a removal event releases the constraint. Use it when repeating the event would contradict the domain, such as initializing the same project twice.

Declare the rule on the event type itself. In .NET, apply [Unique] to the class; the client discovers it on registered event types and sends the constraint definition to the Kernel when it starts.

With the declaration below, the first registration event for a user is accepted. Another registration event of that type for the same event source violates the constraint, while a different source can still register:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
[EventType]
[Unique]
public record ConstraintsModelBoundUniqueEventTypeUserRegistered(string Email, string DisplayName);

View C# snippet source on GitHub

Give the constraint an explicit name when another event will release it. You can also supply the message returned when an append violates the rule:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
[EventType]
[Unique(name: "UniqueUser", message: "A user with this identity has already been registered.")]
public record ConstraintsModelBoundUniqueEventTypeNamedUserRegistered(string Email, string DisplayName);

View C# snippet source on GitHub

Without a custom message, Chronicle uses its default violation message. The name identifies the rule; the message explains the rejected append to its caller.

Declare a removal event when the lifecycle can start again. In .NET, apply [RemoveConstraint] to that event type. Appending it releases the named constraint for its event source, allowing another event of the constrained type:

using Cratis.Concepts;
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
public record ConstraintsModelBoundUniqueEventTypeUserId(Guid Value) : ConceptAs<Guid>(Value);
[EventType]
[RemoveConstraint("UniqueUser")]
public record ConstraintsModelBoundUniqueEventTypeUserRemoved(ConstraintsModelBoundUniqueEventTypeUserId UserId);

View C# snippet source on GitHub

The removal declaration must name the constraint exactly. In .NET, TypeScript, Kotlin, and Java, the default name is the event class name. Elixir defaults to the event type id. An explicit name keeps the connection visible on both events.

In .NET, several removal event types can name the same constraint, so each way of ending a lifecycle can release it. Kotlin and Java accept only one removal event type per constraint name; registration keeps the first one it finds.

The client reads the model-bound declarations and registers constraint definitions. The Kernel checks appends against those definitions and the event history; it does not read .NET attributes itself.

If you need to reserve a value across different event sources instead of limiting event occurrences, use a unique property constraint.