Skip to content

Event sourcing in C# and .NET

Event sourcing stores meaningful business changes as events instead of keeping only the latest state. An onboarding feature records UserOnboarded, for example, then derives the current user view from that fact. The recorded history lets you explain how that view was built or build another view later.

Cratis Chronicle is an event-sourcing database and processing runtime. Its Cratis.Chronicle .NET SDK lets you define events as C# records and append them to an event log. A read model is the state your application reads; reducers and projections build it from those events. Events, reducers, reactors, and projections are discovered automatically and registered with the kernel, Chronicle’s server, when your application connects.

The SDK also covers constraints, migrations, namespaces, and hosting integration, including a .NET Aspire resource. Chronicle’s kernel runs on Microsoft Orleans. Storage is pluggable: MongoDB is the default, with PostgreSQL, SQL Server, SQLite, and in-memory providers implemented in the kernel. TypeScript, Kotlin/Java, and Elixir clients reach the same kernel over its gRPC contract; they do not need a .NET application in front of it.

For an ASP.NET Core application, which is what the example below uses:

Terminal window
dotnet add package Cratis.Chronicle.AspNetCore

It brings in the core Cratis.Chronicle SDK. A console app or worker service uses Cratis.Chronicle on its own; the get started guides cover each hosting model.

This excerpt shows the shape of the API. It assumes a Chronicle server is running locally; Get started with Chronicle walks through starting one and building a complete app.

Hook Chronicle into an ASP.NET Core application:

var builder = WebApplication.CreateBuilder(args)
.AddCratisChronicle(options => options.EventStore = "MyApp");
var app = builder.Build();
app.UseCratisChronicle();
app.Run();

Define an event as a record:

[EventType]
public record UserOnboarded(string Name, string Email);

Append it through an injected IEventLog, for example in a minimal API endpoint:

await eventLog.Append(Guid.NewGuid(), new UserOnboarded("Jane Doe", "jane@example.com"));

The first argument to Append identifies the event source: the user whose history this event belongs to. The example creates a new identifier for a new user; later events about that user should use the same identifier.

Appending records the fact. Reducers fold events into read models, while declarative projections describe that mapping without a handwritten fold. Reactors run side effects when events arrive. Read-model processing is asynchronous, so a successful append does not mean a query already includes the new event.

Use event sourcing when the sequence of changes matters, not just today’s values. It lets you rebuild a read model from recorded events, but it cannot recover facts you never recorded. Event design therefore matters: the history needs to contain what future processing will need.

For a settings form that only needs the latest value, updating a row may be simpler. Event sourcing adds event contracts and read-model processing to maintain. Read when to use event sourcing if you are weighing that cost. If the fit is clear, continue with the ASP.NET Core getting started guide to turn these excerpts into a complete application.

Want the command and query side too? See CQRS in C# and .NET.

Looking for another language? See Chronicle in your language for the TypeScript, Kotlin/Java, Elixir, and Go clients.