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]
. This static class has a static method named TryRegister
, which registers the serializers if they're not already registered, e.g.:
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.