Deserialized The Ramblings of a Web Architect

25Feb/106

Convert C# classes to and from MongoDB Documents automatically using .NET reflection

Posted by Bryan Migliorisi

There are a number of C# based MongoDB projects being actively developed right now but one thing that I needed was a way to convert a standard C# class to a MongoDB document for easy insertion.  It isn't hard to manually type out and set each property by hand, but it certainly is not the most efficient way, especially when you know you are going to be doing it a lot.

 

For example:

Lets say I have a class called SomeClass that looks something like this:

class SomeClass {
	public string StringTest;
	public int IntTest;
}

And somewhere in my code, I have an instance of this class named someClassInstance.  If I want to create a MongoDB document from this class, I’d have to do something like this:

Document document = new Document();
document.add('StringTest', someClassInstance.StringTest);
document.add('IntTest', someClassInstance.IntTest);

So that isn't such a big deal, right? But what about when I have a class with many more properties?  Then it starts to get messy and cumbersome.  I thought that there should be an straightforward way to easily convert any class to a mongo-csharp compatible Document object. (I am using Sam Corder’s mongo-csharp driver, so that I am targeting the Document object from that library.)

Default values

I also wanted to have a way to specify what the default values were for each class property so that when we did the conversion, we would (hopefully) not end up with any null values.  Plus, if for some reason there was a document in MongoDB that was missing a particular key-value pair, the DocumentConverter would automatically fill in that empty field with the default value so in the code we should never have any nulls.

This is something that I would like for my own purposes and may not suit everyone’s needs.  If it doesn't, simply leave off the DefaultValueAttribute and you’ll never know the difference.

My proposed solution

I figured the easiest way to accomplish this was to create a class that would encapsulate all the functionality needed to convert to and from Document objects and have my other classes inherit from that one. I imagined that the above code would change to something like this:

class SomeClass : DocumentConverter {
	[Attributes.DefaultValue("Default StringTest value!")]
	public string StringTest;
	[Attributes.DefaultValue(16)]
	public int IntTest;
}

And to do the conversion would be very simple.  To convert from someClass to Document would be:

Document document = someClassInstance.ToMongoDocument();

To convert from a Document object to someClass would be:

SomeClass someOtherClassInstance = new SomeClass();
omeOtherClassInstance .FromMongoDocument(someDocumentObject);

The DefaultAttribute class

using System;
namespace MyApp.Attributes
{
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    class DefaultValueAttribute : Attribute
    {
        private readonly object _value;

        public DefaultValueAttribute(object Value)
        {
            _value = Value;
        }

        public object GetDefaultValue()
        {
            return _value;
        }
    }
}

The DocumentConverter class

Reflection isn't something that I use too often so there may be better ways of accomplishing what I am trying to do, but this is what I’ve got for now.  If there are better ways, please let me know.  Without further ado…

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using MyApp.Classes.Attributes;
using MongoDB.Driver;

namespace MyApp.Classes
{
    public class DocumentConverter
    {
        public void FromMongoDocument(Document document)
        {
            foreach (DictionaryEntry kvp in document)
            {
                object propertyValue;
                if (kvp.Value != null && (kvp.Value.GetType() == typeof(Document)))
                {
                    // We have a document object - Now lets get a reference to the class property's type
                    var propertyType = GetType().GetProperty(kvp.Key.ToString()).PropertyType;

                    // create new instance of that class
                    var propertyInstance = Activator.CreateInstance(propertyType);

                    // call FromMongoDocument on that class and pass in the document
                    MethodInfo method = propertyInstance.GetType().GetMethod("FromMongoDocument");
                    method.Invoke(propertyInstance, new[] { kvp.Value });

                    propertyValue = propertyInstance;
                }
                else
                {
                    // This is not a Document so lets just assign the value
                    propertyValue = kvp.Value;
                }

                GetType().GetProperty(kvp.Key.ToString()).SetValue(this, propertyValue, null);
            }

        }

        public Document ToMongoDocument()
        {
            Document document = new Document();

            foreach (PropertyInfo property in GetType().GetProperties())
            {
                // Get the value of this property
                object propertyValue = property.GetValue(this, null);

                // If this value is null, then lets try to see if there is a default value attribute and assign that
                if (propertyValue == null)
                {
                    object[] attributes = property.GetCustomAttributes(typeof(DefaultValueAttribute), true);
                    foreach (DefaultValueAttribute defaultValue in attributes.Cast())
                    {
                        propertyValue = defaultValue.GetDefaultValue();
                    }
                    document.Add(property.Name, propertyValue);
                }
                else
                {
                    // We have a property, now lets see if this property has a ToMongoDocument method
                    MethodInfo method = propertyValue.GetType().GetMethod("ToMongoDocument");

                    if (method == null)
                    {
                        document.Add(property.Name, property.GetValue(this, null));
                    }
                    else
                    {
                        document.Add(property.Name, method.Invoke(propertyValue, null));
                    }
                }
            }
            return document;
        }
    }
}

That’s all for now

I hope this is useful for someone.  It is a rough draft of what I threw together last night at around 1AM while half asleep.  So far, it has passed all of my initial tests but if you have suggestions to make it better, please leave some comments here.

23Feb/107

The Current State of MongoDB and C#

Posted by Bryan Migliorisi

As a C# developer, I am often disappointed with the lack of drivers and connectors to cool services like MongoDB.  All the cool languages (and Java) get all the love but C# is often an afterthought.

Luckily for me, there are some kickass developers in the C# community who also share my frustration and as such, they have begun building their own C# MongoDB drivers.

I keep stumbling across more and more C# related MongoDB projects, so I figured I would write up a list and some short descriptions of these projects.

List of C# MongoDB Projects

Each of these projects are still rather new, so expect some features to be missing or not fully functional.  A couple of them are usable in your projects today while the rest are still under heavy development

mongodb-csharp

Originally written by Sam Corder (@SamCorder) with help from a handful of contributers, this is the most complete driver of the bunch. It has been evolving quickly and Sam & team are very quick to resolve any bugs that may arise.

I am using this driver in 2 projects that I am working on and so far things have been great.  It even includes GridFS suport.

From the project description:

Current Features

  • Connect to a server.
  • Query
  • Insert
  • Update
  • Delete
  • All BSON types supported
  • DBRef support
  • Isolation and conversion between BSON types and native .net types.
  • Database, Collection and Cursor objects.
  • Index handling routines (List, Create, Drop)
  • Count
  • Roughly 80% unit test coverage. This can and will be improved on.
  • Paired connections
  • Authentication (Does not reauthorize on auto reconnect yet).
  • Database Commands
  • Basic Linq support
  • GridFS support
  • Map Reduce helpers.
  • hint, explain, $where

They are currently working on connection management features (auto reconnect, connection pooling, etc).

Get involved or check out the code at their mongodb-csharp project page on Github.

mongodb-net

Written by the unnamed developer at DevFuel.com, the mongo-net project aims to be a C# port of the 10Gen\MongoDB official Java driver.  While a lot of work has been done and a load of code written, it is currently unusable.  Over the past couple of weeks a significant amount of progress has been made and functionality is beginning to work but it seems that a functional state is months away.

I would keep an eye on this project, though, as having an API compatible with the official Java driver has its benefits.

Get involved or check out the code at their mongodb-net project page on Google Code.

MongoDB.Emitter

Andrew Rondeau’s MongoDB.Emitter is a pretty cool project that provides a strongly-typed Document mapper for C#.  It works in conjunction with Sam Corder’s mongodb-csharp driver allowing the programmer to define strongly typed interfaces and properties.

I have not tried this yet, but this will be on my list of things to check out.

Get involved or check out the code at their MongoDB.Emitter project page on bitbucket.

CSMongo

Hugo Bonacci (@hugoware) has been working on a driver of his own called CSMongo. CSMongo doesnt support everything that mongodb-csharp does but it does have some interesting features, such as the approach to creating Mongo Documents.  Their approach definitely has a more dynamic feel to it which fites nicely in the unstructured MongoDB world.

I am looking forward to the next version which should have more features including support for Hugo’s own jLinq.

Code doesn't appear to be released yet but you can follow his progress at his blog, Hugoware.

simple-mongodb

Simple-mongdb is another project without public source that is being worked on by Daniel Wertheim (@danielwertheim).  I am not sure if it is even being actively developed but it too has some nice ideas.  The goal of this project is to keep the driver JSON-centric and should be compatible with awesome Newtonsoft’s JSON.net library.

They have a few examples of the proposed API but no code has been released to make said examples work.  This is another one to keep an eye on in the meantime.

Check out the simple-mongodb project page on Google Code.

DocumentConverter

This is a small class I wrote that works with mongodb-csharp.  Its name will likely change at some point if and when it gets packaged up and put on source control.  It exposes two functions that will allow any C# class to convert to and from a MongoDB Document object automatically.  It makes my life a lot easier and it uses System.Reflection to do this.

Read more about DocumentConverter on this blog post.

Conclusion

Well it looks like there is a significant amount of interest in MongoDB from the C# community which is great news because it looks like MongoDB is going to continue to thrive and grow.  My bet is that Sam Corder’s driver will be the most common C# driver, simply because it is so far ahead of the rest but time will tell.  Extensions of Sam’s project, such as MongoDB.Emitter, are equally as cool as the drivers they are built on.

Thanks toeveryone who has contributed to these drivers.  Each of them have some great concepts and I hope that one day we will have a driver that supports all these great ideas and features.

If there are any more projects that I have missed – let me know in the comments!