Append events through Cratis Chronicle event sources
Append through an event source definition in Cratis Chronicle when you want the client to resolve routing and default concurrency settings from one declaration. The .NET examples below write to an account’s streams and show how to handle a rejected append. First define an event source if you have not registered one.
Append to a stream
Section titled “Append to a stream”Call Append<TSource> with the definition type, the instance’s event source id and the event. Select a declared stream by name, and supply a stream id when the stream has multiple instances. Here the month identifies one account’s transaction stream:
using Cratis.Chronicle.Events;using Cratis.Chronicle.EventSequences;using Cratis.Chronicle.EventSources;
[EventType]public record AccountOpened(string Owner);
[EventType]public record MoneyDeposited(decimal Amount);
[EventType]public record MoneyWithdrawn(decimal Amount);
[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;
public class AccountWriter(IEventLog eventLog){ public Task<AppendResult> Deposit(EventSourceId accountId, decimal amount, string month) => eventLog.Append<AccountEventSource>( accountId, new MoneyDeposited(amount), eventStream: "Transactions", eventStreamId: month);}AccountEventSource supplies the source name, while the call selects Transactions and supplies the month. The client validates the stream against the definition before sending the event, then records these routing values:
| Value on the event | Taken from |
|---|---|
| Event source type | The definition’s name, Account here |
| Event stream type | The stream name you pass, or the default (All) when you pass none |
| Event stream id | The stream id you pass, or the default when you pass none |
EventContext.EventSource |
The definition’s name |
You can pass eventSourceType or eventStreamType on the definition-less overloads, but not on the typed ones: they have no such parameters, and the definition decides.
Append without a stream
Section titled “Append without a stream”A stream is optional. An event appended without one carries the event source type and the default stream:
using Cratis.Chronicle.EventSequences;
public class AccountOpener(IEventLog eventLog){ // No stream: the event is written with the event source type "Account" and the default stream. public Task<AppendResult> Open(EventSourceId accountId, string owner) => eventLog.Append<AccountEventSource>(accountId, new AccountOpened(owner));}Without a stream declaration to override them, the event source’s own dimensions determine the default concurrency guard. If the source declares no dimensions either, the client uses its ordinary default scope.
Append several events
Section titled “Append several events”AppendMany<TSource> writes a list of events for one event source id, to one stream and one stream id, in one atomic append.
using Cratis.Chronicle.EventSequences;
public class MonthlyPosting(IEventLog eventLog){ // Every event goes to the same event source id, stream and stream id. public Task<AppendManyResult> Post(EventSourceId accountId, string month, decimal deposit, decimal withdrawal) => eventLog.AppendMany<AccountEventSource>( accountId, [new MoneyDeposited(deposit), new MoneyWithdrawn(withdrawal)], eventStream: "Transactions", eventStreamId: month);}Append for several event sources
Section titled “Append for several event sources”To write events for several event sources in one atomic append, give each EventForEventSourceId the definition it goes through with EventSource, and its stream with EventStream and EventStreamId.
using Cratis.Chronicle.EventSequences;
public class Transfers(IEventLog eventLog){ // Two event sources, one atomic append: both events are written, or neither is. public Task<AppendManyResult> Transfer(EventSourceId from, EventSourceId to, decimal amount, string month) => eventLog.AppendMany( [ new EventForEventSourceId(from, new MoneyWithdrawn(amount)) { EventSource = typeof(AccountEventSource), EventStream = "Transactions", EventStreamId = month }, new EventForEventSourceId(to, new MoneyDeposited(amount)) { EventSource = typeof(AccountEventSource), EventStream = "Transactions", EventStreamId = month } ]);}Every event has exactly one event source. The append is all or nothing: if either event fails a constraint or a concurrency check, neither is written. Events in one batch can go through different definitions, and some can skip definitions altogether.
One event source id can need several guards, for example when a batch writes to two months. The .NET client retains each distinct condition and requires a kernel that supports scope conjunctions, meaning it can check all the conditions together. Without that support, the client refuses the batch before sending it. See Concurrency.
Other overloads
Section titled “Other overloads”| API | Use |
|---|---|
Append(Type eventSource, …) and AppendMany(Type eventSource, …) |
When the definition is known only at run time |
AppendPrepared<TSource> |
When you prepared the event content on the same sequence |
EventForEventSourceId.EventSource and .EventStream |
Batches across event sources |
The unit of work’s Transactional sequence has no typed overloads. To stage an event with the unit of work, pass the routing values yourself. They must equal what the definition declares.
What Chronicle rejects
Section titled “What Chronicle rejects”| Problem | What happens | When |
|---|---|---|
| The type is not a discovered definition | UnknownEventSource is thrown |
Before anything is sent |
| The stream is not one the definition declares | EventStreamDoesNotBelongToEventSource is thrown |
Before anything is sent |
An EventForEventSourceId carries an explicit EventSourceType or EventStreamType that contradicts the definition |
EventRoutingContradictsEventSource is thrown |
Before anything is sent |
| The kernel does not know the definition, the stream, or the event source type | The result carries an error that starts with UnknownEventSource, UnknownEventStreamForEventSource or EventSourceTypeDoesNotMatchEventSource |
At append, for example when the definition was not registered yet |
| A constraint or a concurrency scope fails | IsSuccess is false and the result lists the violations |
At append |
using Cratis.Chronicle.EventSequences;using Cratis.Chronicle.EventSources;
public class CarefulDeposits(IEventLog eventLog){ public async Task<string> Deposit(EventSourceId accountId, decimal amount, string stream, string month) { try { var result = await eventLog.Append<AccountEventSource>( accountId, new MoneyDeposited(amount), eventStream: stream, eventStreamId: month);
return result.IsSuccess ? "Deposited" : "Rejected by the kernel"; } catch (EventStreamDoesNotBelongToEventSource) { // The stream is not one of the streams the definition declares. Nothing was sent. return $"'{stream}' is not a stream of the Account event source"; } catch (UnknownEventSource) { // The type is not a discovered definition. Nothing was sent. return "AccountEventSource is not discovered"; } }}The example catches routing mistakes separately from a kernel rejection. Inspect IsSuccess and the result’s violations after awaiting the append: constraint and concurrency violations are returned, not thrown. For a concurrency conflict, read the state again and reconsider the decision before retrying.
Next steps
Section titled “Next steps”Choose which competing writes matter in Concurrency. If the append depends on state you read earlier, follow the guarded withdrawal in Dynamic Consistency Boundary, then Dynamic Consistency Boundary in Chronicle for the wider model. Scenarios combines the pieces in complete examples; Appending events covers the general append APIs.