Defining Events¶
Events are the heart of any Event Sourcing system: they are the immutable facts that something happened in your domain. In EventSourcingKit, you model each event as a small record that implements the IEventData marker interface and carries an [EventType] attribute.
Modeling an Event¶
An event captures what happened, in the past tense, together with the data that describes it. For a library application, registering a new book and checking one out are two such facts:
using EventSourcingKit;
[EventType("book-registered")]
public record BookRegistered(
Guid BookId,
string Title,
string Author
) : IEventData;
[EventType("book-checked-out")]
public record BookCheckedOut(
Guid BookId,
Guid UserId
) : IEventData;
Two things make these records events:
IEventDatamarks the record as event data. EventSourcingKit discovers everyIEventDatatype in the assemblies you register, so you never wire them up by hand.[EventType("book-registered")]assigns the event's type name, the stable string under which the event is stored and read. It is part of your event store's contract.
Choosing Event Type Names¶
Event type names are free-form strings. Keep them in kebab-case and name them after the fact, not the command that caused it – book-registered, not register-book. Because the name is persisted with every event, treat a change to it as a breaking change: version your events, for example book-registered.v2, rather than renaming in place.
For More Information¶
- Event Types and Discovery explains how discovery works in detail.
- Registering EventSourcingKit is the next step, where these events become available to the event store.