-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
Kieron Lanning edited this page Apr 16, 2026
·
1 revision
<ItemGroup>
<PackageReference Include="EventSourcing" />
<PackageReference Include="Purview.EventSourcing.SourceGenerator" PrivateAssets="all" />
</ItemGroup>Add a provider package based on your persistence choice, for example:
<PackageReference Include="EventSourcing.SqlServer.Events" />
<PackageReference Include="EventSourcing.SqlServer.Snapshot" />using Purview.EventSourcing.Aggregates;
[GenerateAggregate]
public partial class OrderAggregate : AggregateBase
{
public string CustomerId { get; private set; } = default!;
public decimal Total { get; private set; }
[GenerateAggregateEvent]
public partial void CreateOrder(string customerId);
[GenerateAggregateEvent]
public partial void AddLineItem(string productId, string productName, int quantity, decimal unitPrice);
}The source generator creates:
- event classes
- partial method bodies
- event registration
- apply methods
builder.Services.AddSqlServerEventStore();
builder.Services.AddSqlServerSnapshotQueryableEventStore();{
"ConnectionStrings": {
"eventstore-sqlserver": "Server=.;Database=MyApp;Trusted_Connection=True;"
}
}public sealed class OrderService(IEventStore store)
{
public async Task CreateAsync(string id, string customerId, CancellationToken cancellationToken)
{
var order = await store.CreateAsync<OrderAggregate>(id, cancellationToken: cancellationToken);
order.CreateOrder(customerId);
await store.SaveAsync(order, cancellationToken);
}
}public sealed class OrderQueries(IQueryableEventStore store)
{
public Task<long> CountAsync(CancellationToken cancellationToken) =>
store.CountAsync<OrderAggregate>(null, cancellationToken);
}await using var transaction = transactionFactory.Create();
transaction.Enlist(order, store);
transaction.Enlist(inventory, store);
var result = await transaction.CommitAsync(cancellationToken);See Transactions for the detailed behavior model.