Event versioning in event-sourced systems is challenging because events are immutable. This post explains how to use Marten's upcasting feature (introduced in v5.9.0 and v5.10.0) to transform old event schemas into new ones on the fly. Two main approaches are covered: CLR type transformations (keeping old types in the codebase) and raw JSON transformations (using JObject or JsonDocument for better performance). The post walks through a concrete shopping cart example evolving through three schema versions, demonstrating different naming strategies (same name, versioned suffix, meaningful suffix) and both function-based and class-based upcaster registration. The goal is to keep stream aggregation and projection logic clean by only handling the latest schema version.

11m read timeFrom event-driven.io
Post cover image
Table of contents
Different namespace and the same event name with version suffixThe same event name with version suffixGrand finalle