Skip to content

Registering EventSourcingKit

EventSourcingKit is configured through Microsoft's dependency injection. You register two things: the EventSourcingDB client, which holds the database connection, and EventSourcingKit itself, which scans your code for events and reducers and wires up the event store.

Registering the Services

Register the EventSourcingDB client first, then EventSourcingKit. The order matters, because EventSourcingKit's event store depends on the client:

using EventSourcingDb.DependencyInjection;
using EventSourcingKit;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEventSourcingDb(builder.Configuration);

builder.Services.AddEventSourcingKit(
    builder.Configuration,
    [typeof(BookRegistered).Assembly]
);

var app = builder.Build();
app.Run();

The second argument to AddEventSourcingKit is the list of assemblies to scan. Passing typeof(BookRegistered).Assembly tells EventSourcingKit to discover every event and reducer in the assembly that contains your BookRegistered event.

Providing the Configuration

Both calls read from your configuration. Add an EventSourcingDb section for the connection and an EventSourcingKit section for the library's own options:

{
  "EventSourcingDb": {
    "BaseUrl": "http://localhost:3000",
    "ApiToken": "secret"
  },
  "EventSourcingKit": {
    "Source": "https://library.eventsourcingkit.net"
  }
}
  • BaseUrl and ApiToken are the connection details from Running EventSourcingDB.
  • Source is required. It identifies your application as the origin of the events it writes and is stored with every event. Use a stable URI that names your service. The library.eventsourcingkit.net shown throughout the examples is a fictional domain; use one that identifies your own service.

With this in place, you can inject IEventStore anywhere in your application and start storing events.

For More Information