Skip to content

Fresa/dynamodb.eventstore

Repository files navigation

DynamoDB Eventstore

An event store implementation using Amazon DynamoDB.

More details around the implementation can be found in this article.

Continuous Delivery

Installation

dotnet add package DynamoDB.EventStore

https://www.nuget.org/packages/DynamoDB.EventStore/

Getting Started

Create the DynamoDB table according to how you configure the event store. A programatic example can be found here. Make sure to give the DynamoDB client enough permissions. The event store uses the GetItem, UpdateItem and Query actions.

The following is a minimal example on how to integrate with the event store.

using Amazon.DynamoDBv2;
using DynamoDB.EventStore;

namespace MyEventStore;

public class MyAggregate : Aggregate
{
    public MyAggregate(string id) : base(id)
    {
    }

    protected override Task<MemoryStream> CreateSnapShotAsync(CancellationToken cancellationToken)
    {
        // Make sure not to dispose the stream as it will be used to send the snapshot to DynamoDB. The event store will dispose the stream after it has been sent.
        var stream = new MemoryStream();
        // todo: Snapshot the aggregate state here and write it to the stream.
        return Task.FromResult(stream);
    }

    protected override Task LoadSnapshotAsync(MemoryStream snapshot, CancellationToken cancellationToken)
    {
        // todo: Load the aggregate state from the snapshot here
        return Task.CompletedTask;
    }

    protected override Task LoadEventsAsync(IEnumerable<MemoryStream> events, CancellationToken cancellationToken)
    {
        // todo: Load the events here
        return Task.CompletedTask;
    }
}

class Program
{
    static async Task Main(string[] args)
    {
        using var client = new AmazonDynamoDBClient();
        var config = new EventStoreConfig();
        var eventStore = new EventStore(client, config);

        var aggregate = new MyAggregate("my-aggregate-1");
        await eventStore.LoadAsync(aggregate);
        
        // todo: Apply some changes to the aggregate here

        await eventStore.SaveAsync(aggregate);
    }
}

For more examples, see the integration- and system tests.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

About

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages