百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
E

EventSourcing

> DevOps
开源

.NET Core 事件源框架

189 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

.NET Core 事件源框架

EventSourcing

A .NET Core event sourcing framework.

Easy to be integrated in ASP.NET Core web application, Lambda function or Azure function.

Support various of event store:

  • in file system as plain text file (see File system setup instructions)
  • in AWS DynamoDB (see DynamoDB setup instructions)
  • in Azure CosmosDB (see CosmosDB setup instructions)
  • in any relational database supported by EF Core, e.g., Microsoft SQL Server,MySQL, etc. (see EF Core setup instructions)

NuGet packages

  • JKang.EventSourcing
  • JKang.EventSourcing.Persistence.FileSystem
  • JKang.EventSourcing.Persistence.EfCore
  • JKang.EventSourcing.Persistence.DynamoDB
  • JKang.EventSourcing.Persistence.CosmosDB
  • JKang.EventSourcing.Persistence.S3
  • JKang.EventSourcing.Persistence.Caching

Quick Start:

Let's implement a simple gift card management system with the following use cases:

  • Create gift cards with initial credit
  • Debit the gift card specifying amount while overpaying is not allowed

I'm adopting DDD (Domain Driven Design) approach and implement the GiftCard entity as an Rich Domain Aggregate which encapsulates/protects its internal data/state, and contains itself business logics ensuring data integrity.

Step 1 - Create aggregate events

csharp
public sealed class GiftCardCreated : AggregateCreatedEvent
{
    public GiftCardCreated(Guid aggregateId, DateTime timestamp, decimal initialCredit)
        : base(aggregateId, timestamp)
    {
        InitialCredit = initialCredit;
    }

    public decimal InitialCredit { get; }
}
csharp
public class GiftCardDebited : AggregateEvent
{
    public GiftCardDebited(Guid aggregateId, int aggregateVersion, DateTime timestamp, decimal amount)
        : base(aggregateId, aggregateVersion, timestamp)
    {
        Amount = amount;
    }

    public decimal Amount { get; }
}

Notes:

  • It's recommended to implement aggregate event in an immutable way.
  • Inheriting from AggregateEvent or AggregateCreatedEvent is not mandatory, but an aggreagte event must at least implement IAggregateEvent interface.
  • In order to use built-in event stores, please make sure event can be properly serialized using Json.NET.

Step 2 - Create domain aggregate

…

csharp public interface IGiftCardRepository { Task SaveGiftCardAsync(GiftCard giftCard); Task FindGiftCardAsync(Guid id); }

    
```csharp
public class GiftCardRepository : AggregateRepository, 
    IGiftCardRepository
{
    public GiftCardRepository(IEventStore eventStore)
        : base(eventStore)
    { }

    public Task SaveGiftCardAsync(GiftCard giftCard) =>
        SaveAggregateAsync(giftCard);

    public Task FindGiftCardAsync(Guid id) =>
        FindAggregateAsync(id);
}

Step 4 - Register your repository interface and configure event store in dependency injection framework

csharp
services
    .AddScoped();

services
    .AddEventSourcing(builder =>
    {
        builder.UseTextFileEventStore(x =>
            x.Folder = "C:/Temp/GiftcardEvents");
    });

Notes:

  • You can choose other persistence store provided such as CosmosDB or DynamoDB etc.

Step 5 - implement use cases

csharp
// create a new gift card with initial credit 100
var giftCard = new GiftCard(100);

// persist the gift card
await _repository.SaveGiftCardAsync(giftCard);

// rehydrate the giftcard
giftCard = await _repository.FindGiftCardAsync(giftCard.Id);

// payments
giftCard.Debit(40); // ==> balance: 60
giftCard.Debit(50); // ==> balance: 10
giftCard.Debit(20); // ==> invalid operation exception

FAQs

How to programmatically initialize event store?

See this page.

How to use snapshots to optimize performance?

See this page.

How to improve performance using caching?

Consider install the nuget package JKang.EventSourcing.Persistence.Caching and inherit the CachedAggregateRepository class. It leverages Microsoft.Extensions.Caching.Distributed.IDistributedCache to cache aggregate every time after loaded from or saved into repository.

Consider configuring a short sliding expiration (e.g., 5 sec) to reduce the chance of having cache out of date.

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details on:

  • Setting up your development environment
  • Code guidelines and conventions
  • How to submit pull requests
  • Release process

Please feel free to download, fork and/or provide any feedback!

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#cosmosdbcqrsdynamodbevent-sourcing

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理