Skip to content

Up conversion of events

elliotritchie edited this page May 20, 2011 · 8 revisions

Undoubtedly a system will evolve and its events will need to change over time. Properties on existing events may need to be renamed and more properties may need to be added. However you don't want to have to go back and manipulate all of the previously persisted events in your event store and neither should you, the event store should be strictly insert/select only. You also don't want to have your aggregates bloat by having them handle every different type of event that was ever applied to them. This is where event up-conversion can help.

For example a 'CustomerMovedEvent' event may exist within a system. It may contain 'CustomerId', 'Street', 'City', 'County' and 'Postcode' properties. An enhancement to the system may require that the event now exposes a 'Country' property. This requirement can be accommodated by creating a new 'CustomerMovedEventV2' event that contains all of the old event's properties as well as the new 'Country' property. All previously persisted 'CustomerMovedEvent' events will now need to be up-converted to 'CustomerMovedEventV2' events as they are read from the event store.

Here's how you can create an event up-converter with NES:

public class CustomerMovedEventConverter : EventConverter<CustomerMovedEvent, CustomerMovedEventV2>
{
    public override CustomerMovedEventV2 Convert(CustomerMovedEvent @event)
    {
        return EventFactory.Create<CustomerMovedEventV2>(e =>
        {
            e.CustomerId = @event.CustomerId;
            e.Street = @event.Street;
            e.City = @event.City;
            e.County = @event.County;
            e.Postcode = @event.Postcode;
            e.Country = "England"
        });
    }
}

NES will scan your assemblies for all classes inheriting from EventConverter and will automatically register them for you.

Clone this wiki locally