best event sourcing db strategy

后端 未结 6 796
误落风尘
误落风尘 2020-12-22 23:09

I want to setup a small event sourcing lib. I read a few tutorials online, everything understood so far.

The only problem is, in these different tutorials, there are

6条回答
  •  有刺的猬
    2020-12-22 23:29

    I think best solution will be to go with #2. And even you can save your current state together with the related event at the same time if you use a transactional db like mysql.

    I realy dont like and recommend the solution #1.

    If your concern for #1 is about event versioning/upgrading; then declare a new class for each new change. Dont be too lazy; or be obsess with reusing. Let the subscribers know about changes; give them the event version.

    If your concers for #1 is about something like querying/interpreting events; then later you can easily push your events to an nosqldb or eventstore at any time (from original db).

    Also; the pattern I use for eventsourcing lib is something like that:

    public interface IUserCreated : IEventModel
    {
    
    }
    
    public class UserCreatedV1 : IUserCreated
    {
        public string Email { get; set; }
        public string Password { get; set; }
    }
    
    public class UserCreatedV2 : IUserCreated
    {
        // Fullname added to user creation. Wrt issue: OA-143
    
        public string Email { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    public class EventRecord where T : IEventModel
    {
        public string SessionId { get; set; } // Can be set in emitter.
        public string RequestId { get; set; } // Can be set in emitter.
        public DateTime CreatedDate { get; set; } // Can be set in emitter.
        public string EventName { get; set; } // Extract from class or interface name.
        public string EventVersion { get; set; } // Extract from class name
        public T EventModel { get; set; } // Can be set in emitter.
    }
    
    public interface IEventModel { }
    

    So; make event versioning and upgrading explicit; both in domain and codebase. Implement handling of new events in subscribers before deploying origin of new events. And; if not required, dont allow direct consuming of domain events from external subscribers; put an integration layer or something like that.

    I wish my thoughts will be useful for you.

提交回复
热议问题