Skip to content

1. Your first event

To record a book arriving in Cratis Chronicle, define a BookAdded event, give the book an identity, and append the event to the log. By the end of this chapter you will have one recorded fact to inspect, ready to feed the catalog in the next chapter.

Before writing code, make sure your kernel is running and your project has a connected client. The C# append example takes an IEventStore; use the event-store handle from that setup. The other client tabs use their corresponding connection and registration instructions.

A Book row can describe a book you have now. Here we also want to retain the fact that it was added, so we record that change as an event. Name events in the past tense: BookAdded describes something that happened, not a request that might still fail.

In C#, define the event as a record marked with [EventType]. It carries the title and ISBN:

using Cratis.Chronicle.Events;
[EventType]
public record BookAdded(string Title, string Isbn);

View C# snippet source on GitHub

With no explicit name on [EventType], Chronicle uses BookAdded as the event type’s stored identity. Keep that identity when you later rename a class: changing the default name does not rename existing history. Event types explains how to preserve it.

The title and ISBN are required here because this fact says both were known when the book arrived. For other events, decide what was actually known at that moment. Missing information may belong in a later event rather than in a partially filled record.

The event source identifies which book this event belongs to. Later borrow and return events use that same identity, keeping the book’s history together. In C#, use a BookId rather than passing an untyped Guid throughout your application; code that expects a BookId can then distinguish it from a member’s ID:

using Cratis.Chronicle.Events;
public record BookId(Guid Value) : EventSourceId<Guid>(Value)
{
public static BookId New() => new(Guid.NewGuid());
}

View C# snippet source on GitHub

EventSourceId<Guid> wraps a Guid as a domain-specific value using ConceptAs<T>, with conversions to Chronicle’s untyped event-source key. That lets the append example pass a BookId directly. Arc also recognizes event-source ID types when resolving where a command appends events; you do not need Arc for this tutorial. The event source concept explains how events are grouped.

Kotlin, Java, and TypeScript use plain strings for event-source IDs rather than this C# typed wrapper. Elixir can mark a Chronicle.Concept with event_source_id: true; its client tab shows a BookId concept. Those are different client conventions, not the same type-safety guarantee.

Use your connected event store to append BookAdded under the book’s ID. The C# helper creates a BookId, appends the title and ISBN, and returns the ID so you can use it for later events:

using Cratis.Chronicle;
public static class TutorialFirstEventAppend
{
public static async Task<BookId> AddBook(IEventStore eventStore)
{
var book = BookId.New();
await eventStore.EventLog.Append(book, new BookAdded("The Pragmatic Programmer", "978-0135957059"));
return book;
}
}

View C# snippet source on GitHub

In C#, call TutorialFirstEventAppend.AddBook with your IEventStore and await the result. For another client, run its append example after the setup has registered the event type. A successful append validates the event against its registered schema, assigns a sequence number, and stores it in the event log, the ordered history from which observers read.

To see the result, open the workbench at https://localhost:35000, select the event store and namespace your client used, and browse its event log. Find BookAdded with the title The Pragmatic Programmer and ISBN 978-0135957059. Running the helper again creates a new book ID and another event; it does not update the first book.

You defined BookAdded, associated it with a book ID, and appended it to the log. You can inspect the recorded fact, but you have not built a queryable catalog yet. In Building a read model, you will map these events onto the book view the librarian needs.