MongoDB in C# – Extensions to support Json-mapping or Proxy generation

Note! This is an update to my previous post. For history and explanation of the consuming code see my previous post.

I have made some quick changes to make the JSON API and the Proxy API that I wrote about in the earlier post, easier to use.

I have put the code in an external lib (yes it has a rather bad name). I have ensured that the consumer doesn’t have to reference Castle’s libs etc. unless he/she wants more control (more about this later). The consumer also doesn’t have to reference Json.Net.

First, I have reconstructed my JSON-API to use extensions on the Document-class, so that you see how I think the JSON interaction could be. The method that needs to be updated (tested) to handle more types etc. is in MongoJsonMapper.ConvertToMongoDbValue.

var firstNoteDocument = new Document().UpdateFrom("{
        Title : \"First note using Json.\",
        Body : \"Some nice text.\",
        Tags : [\"MongoDB\", \"Getting started\"],
        Votes : 3}");


var secondNote = new Note {
        Title = "Second note using Serialization.",
        Body = "Some nice text.",
        Votes = 3 };
var secondNoteDocument = new Document().UpdateFrom(secondNote);


For the proxy-API I have created a simple MongoDbProxyBuilder which is a simple default implementation of a proxybuilder that offers the oppurtunity to create proxies of entities that implements IMongoDbEntityProxy. If you want more control you can use the code under ProxyGeneration or role an own implementation that uses Castle. The MongoDbEntityInterceptor then can be used as a starting point for you to extend.

var proxyBuilder = new MongoDbProxyBuilder();

var firstNote = proxyBuilder.ProxyFromClass<Note>();
firstNote.Title = "First note using Proxies.";
firstNote.Tags = new [] { "MongoDB", "Getting started" };
firstNote.Body = "Some nice text.";


What do you think? Anything useful?

I will try to get the time to ensure that DbRef and embeded documents etc. works.

You can download the changes from here.


4 thoughts on “MongoDB in C# – Extensions to support Json-mapping or Proxy generation

  1. Pingback: Getting started with MongoDB – Using Json.Net and Castle Dynamic proxy « Daniel Wertheim

  2. Pingback: My opinion on mappings for MongoDB « Daniel Wertheim

  3. Hi Daniel,

    Im using your extension MongoJsonMapper, and seems UpdateFrom will exclude the _id if it is passed in. Any reason why? What if, I want to perform update for the item with particular objectid.


    • Hi,

      I guess you refer to the line where I assign if “isEmptyKeyField”. It looks like != should be switched to ==, since only then should it be skipped.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s