Skip to main content
.NETStandard2.0

An event source stack based on NEventStore and Akka

Published
Mar 25, 2024
2y ago
Total Downloads
15.4K
15,384

Install

dotnet add package EventSaucing --version 2.0.2
ProductVersions
.NET Standard
netstandard2.0

README

EventSaucing

Add a little EventSauce to your project ;)

This library brings together different stacks to create an Event Sourcing solution.


Reading list before using the project:

Installing

The latest release of EventSaucing is available on NuGet.

Usage

Once referenced in your project you need to configure it in your Startup inside ConfigureServices.

var builder = new ContainerBuilder();

builder.RegisterEventSaucingModules(new EventSaucingConfiguration {
   ConnectionString = // Set your connection string
});

builder.Populate(services); // Populate your services.

var container = builder.Build();

container.StartEventSaucing(); // Start EventSaucing

You can then construct your Aggregates by inheriting from the Aggregate class and including Apply() methods to handle the raised events on the aggregate.

public class FooAggregate : Aggregate {
  public FooAggregate(Guid id) {
    base.Id = id;
  }

  public int Bar { get; set; }

  public void Create(int bar) {
    RaiseEvent(new FooCreated(bar));
  }

  void Apply(FooCreated @event) {			
    Bar = @event.Bar;
  }
}
// A POCO for the event state
public class FooCreated {
  public FooCreated(int bar) {
    Bar = bar;
  }
  public int Bar { get; }
}

You can then create Projectors by inheriting from ProjectorBase and setting up which events to handle. You must also give each projector a unique number. In this example Dapper is used to interact with the persistence store.

[Projector(1)] // The unique projector id.
public class FooProjector: ProjectorBase {
  readonly ConventionBasedCommitProjecter _conventionProjector;

  public FooProjector(IDbService dbService, IPersistStreams persistStreams):base(persistStreams, dbService) {
    var conventionalDispatcher = new ConventionBasedEventDispatcher(c => Checkpoint = c.ToSome())
      .FirstProject<FooCreated>(OnFooCreated)
      .ThenProject<SomeEvent>(OnSomeEventHandler);

    _conventionProjector = new ConventionBasedCommitProjecter(this, dbService, conventionalDispatcher);
  }

  public override void Project(ICommit commit) {
    _conventionProjector.Project(commit);
  }

  private void OnFooCreated(IDbTransaction tx, ICommit commit, FooCreated @event) {
    var sqlParams = new { 
      Id = commit.AggregateId(), 
      Bar = FooCreated.Bar 
    };

    const string sql = @"
      INSERT INTO [dbo].[FooProjector.Foo]
        ([Id]
        ,[Bar])
      SELECT
        @Id
        ,@Bar
      WHERE NOT EXISTS(SELECT * FROM [dbo].[FooProjector.Foo] WHERE Id = @Id);";
    tx.Connection.Execute(sql, (object)sqlParams, tx);
  }

Dependencies

  • Dapper - Used to interact with the sql persistence store.
  • NEventStore - Used for storage of the event stream.
  • Serilog - Used for logging.
  • Akka.NET - Used to deliver deliver events to aggregates & projectors.
  • Autofac - Used for dependency injection.
  • Scalesque - Used to add functional programming in c#.

Changes from Previous Version

No structural changes between v2.0.1 and v2.0.2.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

No SPDX license expression is published. Review the license text and your organisation's policy before redistribution.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.The published metadata declares 14 direct dependency entries across framework groups. Inspect the resolved dependency graph for your target framework.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.