Integration with MongoDB
It is possible to use value objects (VOs) in MongoDB.
To generate a converter (serializer), add the Bson conversion in the attribute, e.g.
Now that the serializers are generated, you now need to register them. Vogen generates a static class named RegisterBsonSerializersFor[NameOfProject].
By default, this class has a static constructor that automatically registers all BSON serializers when first accessed. However, if you need to run BSON configuration before the serializers are registered, you can use the ManuallyRegisterBsonSerializers customization:
With this flag set, Vogen generates a TryRegister() method without a static constructor, allowing you to control when registration occurs:
Without the flag, the static constructor automatically calls code like:
A MongoDB example is included in the source.
Below is a walkthrough of that sample.
The sample uses MongoDB to read and write entities (a Person) to a MongoDB database in a testcontainer. Note that attributes on the value objects do not specify the BSON serializer; that is specified in global config in ModuleInitializer.cs:
This simple example registers the serializers manually:
… but it could just as easily registered them with the generated register:
(replace Vogen_Examples with the name of your project)
Next, it adds a bunch of Person objects to the database, each containing value objects representing age and name, and then reads them back.