Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 702
Arnold Zokas Avatar asked Oct 08 '22 18:10

Arnold Zokas


2 Answers

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);
}
like image 79
Ben Smith Avatar answered Oct 12 '22 10:10

Ben Smith


The only method I've seen to handle backwards compatibility is to version each change to your event. Some people create new versions in a different namespace, so that the namespace reflects the version/feature information:

namespace Foo.ProjectA.Release1
{
  public interface ISomeEvent {...}
}

namespace Foo.ProjectA.Release3
{
  public interface ISomeEvent {...}
}

Alternatively, create a new version of the class with the version/feature info in the class name:

namespace Foo.ProjectA
{
  public interface ISomeEvent {...}
  public interface ISomeEvent_MoreRefinedVersion {...}
}

Personally, I prefer the former approach, unless the new version adds more specific semantic meaning to the event.

like image 25
RyanR Avatar answered Oct 12 '22 10:10

RyanR