Custom type converters for MongoDB

I always try to keep my models seperate from the implementation or configuration. EF Core 2.1 added great support for custom type converters with HasConversion. MongoDB also has a support for custom type conversion, though it is not quite as well documented.

The basic premise revolves around implementing IBsonSerializer<TType>. Let's suppose I have a Bitrate struct I want to convert to a long. Bitrate has a BitsPerSecond property which is the underlying long value. My BitrateSerializer will look something like this:

This being Mongo driver, it is not so easy to add unit tests. I'll cover this another time.

Leave a comment