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.
Defining a unique event type constraint
Section titled “Defining a unique event type constraint”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);import io.cratis.chronicle.constraints.Uniqueimport io.cratis.chronicle.events.EventType
@EventType@Uniquedata class ConstraintsModelBoundUniqueEventTypeUserRegistered(val email: String, val displayName: String)import io.cratis.chronicle.constraints.Unique;import io.cratis.chronicle.events.EventType;
@EventType@Uniquerecord ConstraintsModelBoundUniqueEventTypeUserRegistered(String email, String displayName) {}defmodule MyApp.Events.ConstraintsModelBoundUniqueEventTypeUserRegistered do use Cratis.Chronicle.Events.EventType, id: "constraints-model-bound-unique-event-type-user-registered"
defstruct [:email, :display_name]
unique_event_type()endimport { eventType, unique } from '@cratis/chronicle';
@eventType('constraints-model-bound-once-user-registered')@unique()class CmbOnceUserRegistered { email = ''; displayName = '';}Constraint name and violation message
Section titled “Constraint name and violation message”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);// Requires io.cratis:chronicle 6.5.0 or later.import io.cratis.chronicle.constraints.Uniqueimport io.cratis.chronicle.events.EventType
@EventType@Unique(id = "UniqueUser", message = "A user with this identity has already been registered.")data class ConstraintsModelBoundUniqueEventTypeNamedUserRegistered(val email: String, val displayName: String)// Requires io.cratis:chronicle 6.5.0 or later.import io.cratis.chronicle.constraints.Unique;import io.cratis.chronicle.events.EventType;
@EventType@Unique(id = "UniqueUser", message = "A user with this identity has already been registered.")record ConstraintsModelBoundUniqueEventTypeNamedUserRegistered(String email, String displayName) {}defmodule MyApp.Events.ConstraintsModelBoundUniqueEventTypeNamedUserRegistered do use Cratis.Chronicle.Events.EventType, id: "constraints-model-bound-unique-event-type-named-user-registered"
defstruct [:email, :display_name]
# Elixir supports a custom constraint name, but not a custom violation message. unique_event_type(name: "UniqueUser")endimport { eventType, unique } from '@cratis/chronicle';
@eventType('constraints-model-bound-named-user-registered')@unique('UniqueUser', 'A user with this identity has already been registered.')class CmbNamedUserRegistered { email = ''; displayName = '';}Without a custom message, Chronicle uses its default violation message. The name identifies the rule; the message explains the rejected append to its caller.
Releasing a constraint
Section titled “Releasing a constraint”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);import io.cratis.chronicle.constraints.RemoveConstraintimport io.cratis.chronicle.events.EventType
@EventType@RemoveConstraint("UniqueUser")data class ConstraintsModelBoundUniqueEventTypeUserRemoved(val userId: String)import io.cratis.chronicle.constraints.RemoveConstraint;import io.cratis.chronicle.events.EventType;
@EventType@RemoveConstraint("UniqueUser")record ConstraintsModelBoundUniqueEventTypeUserRemoved(String userId) {}defmodule MyApp.Events.ConstraintsModelBoundUniqueEventTypeUserRemoved do use Cratis.Chronicle.Events.EventType, id: "constraints-model-bound-unique-event-type-user-removed"
defstruct [:user_id]
remove_constraint("UniqueUser")endimport { eventType, removeConstraint } from '@cratis/chronicle';
@eventType('constraints-model-bound-once-user-removed')@removeConstraint('UniqueUser')class CmbOnceUserRemoved { userId = '';}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.
How constraints are enforced
Section titled “How constraints are enforced”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.