EventSourcing is a .NET library that implements basis for event sourcing pattern.
Documentation is currently W.I.P.
Install the package via .NET CLI:
dotnet add package Sl4vP0weR.EventSourcingDefine type that can be a source for events.
public record User(string Name, string Surname, string PhoneNumber) : IEventSourceDefine event representing state changes:
public record UserCreatedEvent(DateTime CreatedAt, User User) : IEvent;Define event handler as a type to listen events:
// suitable for any type of event.
public class AuditEventHandler : IEventHandler<IEvent>
{
public int Order => int.MaxValue; // should run after other handlers
public void Handle(IEvent @event) => PerformAudit(@event);
private void PerformAudit(IEvent @event)
{
// save to the file/database, or send as a message to the message broker/web api, or whatever suits your needs.
...
}
}Create a method event handler instance:
IEventHandler<IEvent> handler = DelegateEventHandler<IEvent>.From(PerformAudit);or class event handler instance:
IEventHandler<IEvent> handler = new AuditEventHandler();and listen to the incoming global events:
handler.Listen();or to the specific source
IEventSource user = new User("Frederic", "Chopin", "123456789");
handler.Listen(user);Create an event:
User user = new User("Frederic", "Chopin", "123456789");
var @event = new UserCreatedEvent(DateTime.UtcNow, user);Raise event:
@event.Raise();or with specific sources (still raising global):
@event.Raise(user);or asynchronous approach:
await @event.RaiseAsync(user);This project is licensed under the MIT License.
See the LICENSE file for details.