Define Cratis Chronicle event sources and streams
Define an event source in Cratis Chronicle to keep its routing names and default concurrency settings consistent across writers. In the .NET client, an IEventSource class carries that declaration; callers still supply the id of the account, cart or other instance they write to. Once it is registered, you can append through the definition.
Declare the definition
Section titled “Declare the definition”Create a class that implements IEventSource. Use [EventSource] for its name, description and default concurrency dimensions, and add one [EventStream] for each named part of its history. Leave the class body empty:
using Cratis.Chronicle.EventSources;
[EventSource("Account", Description = "A customer's bank account", Concurrency = ConcurrencyDimensions.EventSourceId)][EventStream("Onboarding", Description = "Opening the account and verifying the customer")][EventStream( "Transactions", Description = "Deposits and withdrawals, one stream per month", Concurrency = ConcurrencyDimensions.EventSourceId | ConcurrencyDimensions.EventStreamType | ConcurrencyDimensions.EventStreamId)]public class AccountEventSource : IEventSource;| Attribute | Property | Meaning |
|---|---|---|
[EventSource] |
name (constructor) | The event source type written on every event appended through the definition |
Description |
Shown in the Workbench | |
Concurrency |
The default concurrency dimensions for every stream | |
[EventStream] |
name (constructor) | The event stream type written on events appended to the stream |
Description |
Shown in the Workbench | |
Concurrency |
The dimensions for this stream. When none, the event source’s dimensions apply |
Chronicle reads the attributes during artifact discovery and registers the resulting definition when the event store connects. It never creates an instance of the class, so there is no entity state or constructor to maintain here.
Choose the names
Section titled “Choose the names”The event source name becomes the stored event source type, and each stream name becomes a stored stream type. Observers match those values, so keep them stable when you rename code.
using Cratis.Chronicle.EventSources;
// The event source type is "Loan": an explicit name wins.[EventSource("Loan")]public class BankLoanDefinition : IEventSource;
// The event source type is "ShoppingCart": the "EventSource" suffix is removed from the type name.[EventSource(Description = "A customer's shopping cart")]public class ShoppingCartEventSource : IEventSource;
// The attribute is optional. The event source type is "Invoice", with no description and no concurrency dimensions.public class InvoiceEventSource : IEventSource;- An explicit name in
[EventSource("Loan")]wins. - Without one, the name is the type name with a trailing
EventSourceremoved. A type that is itself calledEventSourcekeeps its name. - The attribute is optional. Without it the definition has no description and declares no concurrency dimensions.
- Names are compared exactly.
Accountandaccountare different event source types.
Two definitions with the same name make discovery throw DuplicateEventSourceName. A definition that declares the same stream twice throws DuplicateEventStreamName.
Choose the concurrency dimensions
Section titled “Choose the concurrency dimensions”ConcurrencyDimensions is a flags enum: combine its values with | to choose the fields that narrow a default concurrency check. An omitted field does not restrict the check. Include EventSourceId when the boundary belongs to one entity; leaving it out makes the condition apply across source ids.
| Flag | The guard covers events with the same |
|---|---|
EventSourceId |
event source id |
EventSourceType |
event source type |
EventStreamType |
event stream type |
EventStreamId |
event stream id |
None |
Nothing is declared. The append keeps the guard it would have had without a definition |
A stream’s declared dimensions replace the event source’s dimensions; they are not added to them. None on a stream means it inherits the source’s settings, not that it disables checking. Use Concurrency to work out which writers each combination makes conflict.
Choose the stream ids
Section titled “Choose the stream ids”A stream declares a type only. The stream id is a value you pass on each append, and Chronicle does not impose a format or a template for it. Keep the convention in one place so every writer produces the same string:
using Cratis.Chronicle.Events;
// Chronicle does not impose a format for an event stream id. Keep the convention in one place.public static class TransactionStreams{ public static EventStreamId ForMonth(DateOnly date) => new($"{date.Year:0000}-{date.Month:00}");}For a stream that exists once per account, such as Onboarding, omit eventStreamId to use the default stream id. There is no month or other instance value to distinguish in that case.
Where Chronicle finds definitions
Section titled “Where Chronicle finds definitions”The client discovers every non-abstract, non-generic class that implements IEventSource in the application’s assemblies, together with its other artifacts. With explicit artifact registration you nominate definitions in the composition root instead. Registration with the kernel happens when the event store connects. How it works has the sequence.
Check what is registered
Section titled “Check what is registered”Read the definition back from the client:
using Cratis.Chronicle;using Cratis.Chronicle.EventSources;
public static class DefinitionInspector{ public static IEnumerable<string> DescribeStreams(IEventStore eventStore) { var definition = eventStore.EventSources.GetFor(typeof(AccountEventSource));
// ConcurrencyFor returns the stream's dimensions when it declares any, otherwise the event source's. return definition.Streams.Select(stream => $"{definition.Name}/{stream.Name}: {definition.ConcurrencyFor(stream)}"); }}After discovery, IEventStore.EventSources lets you look up the definition by type or name. Check that its name, streams and dimensions match what your writers expect. In the Workbench, open the event store’s Event Sources page to inspect the kernel’s registered definitions. It shows the name, description, owner and default concurrency, with stream names and their own dimensions in the detail pane. See Workbench.
Next steps
Section titled “Next steps”With the definition registered, append through it to write its routing onto an event. Before choosing dimensions for a business rule, compare the examples in Concurrency and the decision-model approach in Dynamic Consistency Boundary in Chronicle. For existing callers, Migrate from string routing shows the transition.