ServcoX.EventSauce 3.1.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package ServcoX.EventSauce --version 3.1.3                
NuGet\Install-Package ServcoX.EventSauce -Version 3.1.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ServcoX.EventSauce" Version="3.1.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ServcoX.EventSauce --version 3.1.3                
#r "nuget: ServcoX.EventSauce, 3.1.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ServcoX.EventSauce as a Cake Addin
#addin nuget:?package=ServcoX.EventSauce&version=3.1.3

// Install ServcoX.EventSauce as a Cake Tool
#tool nuget:?package=ServcoX.EventSauce&version=3.1.3                

ServcoX.EventSauce

Event Sauce is the light-weight event sourcing library Servco uses internally to store our events in Azure Table Storage. Its for when you want to use event sourcing but your needs (or budget) aren't demanding enough to justify using Kafka.

It's performant a modest scale, and since it backs on Azure Table Storage it's cost is tiny compared to just about everything else. It's also simple, and allows you to build out event sourcing in a way that suits you.

Installation

Grab it from NuGet from dotnet add package ServcoX.EventSauce or dotnet add package ServcoX.EventSauce.DependencyInjection for DI support.

Basic usage

Define your events like this:

public readonly record struct CakeBaked : IEventPayload;
public readonly record struct CakeIced(String Color) : IEventPayload;
public readonly record struct CakeCut(Int32 Slices) : IEventPayload;

Connect to your event store like this:

var eventStore = new EventStore(connectionString: "UseDevelopmentStorage=true;", containerName: "EventSauce", aggregateName: "CAKE");

Write events like this:

var aggregateId = Guid.NewGuid().ToString("N");
await store.WriteEvent(aggregateId, new CakeBaked());
await store.WriteEvent(aggregateId, new CakeIced("BLUE"));
await store.WriteEvent(aggregateId, new CakeCut(3));

And finally, read events back like here:

foreach (var slice in await store.ListSlices())
{
    foreach (var evt in await store.ReadEvents(slice.Id))
    {
        Console.WriteLine($"{evt.Type}: {evt.Payload}");
    }
}

Projections

Once you have events being stored, you can then go a step further and create projections based on them.

Create a projection like this:

public record Cake
{
    public String Id { get; set; } = String.Empty;
    public Int32 Slices { get; set; }
    public String Color { get; set; } = String.Empty;
    public DateTime LastUpdatedAt { get; set; }
}

When you're creating your store, define how to build the projection:

var cakeProjection = store.Project<Cake>(version: 1, builder => builder
    .OnCreation((projection, id) => projection.Id = id)
    .OnEvent<CakeIced>((projection, body, evt) => projection.Color = body.Color)
    .OnEvent<CakeCut>((projection, body, evt) => projection.Slices += body.Slices)
    .OnUnexpectedEvent((projection, evt) => Console.Error.WriteLine($"Unexpected event ${evt.Type} encountered")) // Called for any event that doesn't have a specific handler
    .OnAnyEvent((projection, evt) => projection.LastUpdatedAt = evt.At) // Called for all events - expected and unexpected
    .IndexField(nameof(Cake.Color))
);

Then simply read the projection like this:

var aggregate = await cakeProjection.Read(aggregateId);

Or query on a field that has been indexed (see .Index above):

var aggregates = cakeProjection.Query(nameof(Cake.Color), "BLUE");

When you query a projection it will play out all events that have occured since the last query using the projection definition, rendering the latest projection. The projection is then persisted in the database so that on the next query it needs only project events that have occured since.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ServcoX.EventSauce:

Package Downloads
ServcoX.EventSauce.DependencyInjection

Dependency injection wrapper for Event sauce

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.4 1,767 3/14/2024
4.0.3 191 3/14/2024
4.0.2 253 3/13/2024
4.0.1 151 3/12/2024
4.0.0 164 3/12/2024
3.2.1 244 3/7/2024
3.2.0 531 2/16/2024
3.1.7 1,122 1/12/2024
3.1.6 311 1/12/2024
3.1.5 300 1/12/2024
3.1.4 385 1/12/2024
3.1.3 331 1/12/2024
3.1.2 335 1/11/2024
3.1.1 327 1/11/2024
3.1.0 304 1/11/2024
3.0.1 338 1/11/2024
3.0.0 424 1/10/2024
2.4.10 409 1/10/2024
2.4.9 346 1/10/2024
2.4.8 368 1/10/2024
2.4.7 421 1/9/2024
2.4.6 562 1/2/2024
2.4.5 535 1/2/2024
2.4.4 410 1/2/2024
2.4.3 408 1/2/2024
2.4.2 464 12/28/2023
2.4.1 426 12/28/2023
2.4.0 381 12/28/2023
2.3.0 396 12/27/2023
2.2.0 393 12/27/2023
2.1.0 415 12/27/2023
2.0.0 382 12/27/2023
1.1.0 395 12/25/2023
1.0.0 378 12/22/2023