How to convert a BsonDocument into a strongly typed object with the official MongoDB C# driver? -



How to convert a BsonDocument into a strongly typed object with the official MongoDB C# driver? -

for unit testing purposes, i'd test class mappings without reading , writing documents mongodb database. handle special cases such circular parent / kid references , read properties, i've used bsoncclassmap.registerclassmap< mytype>(...) custom mappings overriding default automap(); generated mappings.

does know how convert bson document desired typed object without making round trip database? driver doing when going , info store. goal utilize same logic mongodb c# driver using internally test serialization / c# domain object bson document.

i'm able utilize bson extension method tobsondocument() convert c# object bsondocument. piece i'm lacking reverse of process - bsondocument.toobject< mytype>();

is possible latest version of official mongodb c# driver? seems should - i'm wondering if i'm blind , missing obvious.

the mongodb driver provide method deserializing bson type. bsonserializer can found in mongodb.bson.dll, in mongodb.bson.serialization namespace.

you can utilize bsonserializer.deserialize<t>() method. illustration code be

var obj = new myclass { myversion = new version(1,0,0,0) }; var bsonobject = obj.tobsondocument(); var myobj = bsonserializer.deserialize<myclass>(bsonobject); console.writeline(myobj);

where myclass defined as

public class myclass { public version myversion {get; set;} }

i hope helps.

c# mongodb

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -