Search code examples
c#cqrsevent-sourcing

How to retrieve historical events after changes to Domain Event structure


Given event store with fields:

  • AggregateId : integer
  • Payload : blob
  • Version : integer

Which contains events based on:

public class OrderLineAdded
{
    int Id;
    short Quantity;
}

... and then has further events added with an updated structure:

public class OrderLineAdded
{
    int ProductId; // field name has changed
    int Quantity; // field datatype has changed
}

When this historical data is retrieved (for analysis, etc), how do you reconstruct binary payload into meaningful data?

Note: the code above is not an example of a good event/event store implementation. I just want to know how this scenario should be handled.


Solution

  • Rinat Abdullin outlines some domain event versioning strategies in his CQRS bliki that you might find useful to read.

    https://abdullin.com/post/event-sourcing-versioning/

    He suggests two possible approaches:

    1. Use a serializer that can cope with renaming of properties in your event automatically such as Google's ProtoBuf Serializer.
    2. Implement the following interface to manually upgrade events in-memory yourself.
    public interface IUpgradeDomainEvents
    {
        IEnumerable<IDomainEvent> Upgrade(IDomainEvent e, string id);
    }