For unit testing purposes, I\'d like to test my class mappings without reading and writing documents into the MongoDB database. To handle special cases such as circular parent
The MongoDB Driver does provide a method for deserializing from Bson to your type. The BsonSerializer
can be found in MongoDB.Bson.dll
, in the MongoDB.Bson.Serialization
namespace.
You can use the BsonSerializer.Deserialize
method. Some example code would be
var obj = new MyClass { MyVersion = new Version(1,0,0,0) };
var bsonObject = obj.ToBsonDocument();
var myObj = BsonSerializer.Deserialize(bsonObject);
Console.WriteLine(myObj);
Where MyClass
is defined as
public class MyClass
{
public Version MyVersion {get; set;}
}
I hope this helps.