Skip to content

MongoDB and .NET Follow-up: NoRM

In December I posted about MongoDB and using it within .NET applications. I had always intended to post one (or more) follow-up posts but various things conspired against me causing it never to happen. Well, now that time is over and I thought it was about time I explored another MongoDB .NET driver.

In my original post I discussed using Sam Corder's MongoDB-CSharp driver to connect to MongoDB. That driver represents a MongoDB document essentially as a dictionary-like structure. You would have a set of key/value pairs, with each value being a simple type such as int or string, an array, another sub-document or any other supported type. This worked fine but I was never completely comfortable with it. It seemed to me your app had to be tied rather too closely to the driver (and therefore MongoDB itself) for comfort. In my demo app, even the views had to be aware of how the Document class worked!

POCOs Please

The ideal scenario would be to have all the domain objects represented as proper objects - preferably without anything (attributes etc) tying them to any particular persistance mechanism. It would also be nice to have the ability to configure how MongoDB stores the objects through some sort of mapping. IIt would also be awesome to have the ability to query MongoDB for these objects using LINQ.

Well... the good news is that you can have all that. Andrew Theken and a bunch of great developers have created NoRM. NoRM allows you to define your domain entities as lovely POCOs and store them in MongoDB, configure the object/MongoDB mappings via a configuration API or mapping classes and query them using LINQ.

Code Code Code

When I wrote the original post, I put together a small sample 'notes' MVC2 application to demonstrate use of the MongoDB-CSharp driver. Rather than try to illustrate the difference with using NoRM through code snippets and examples (there are plenty of those already), I thought I'd update my little app to use NoRM.

Essentially, the new version of the app is a straight port to NoRM. However, I have tweaked it a little bit in the process (I wasn't really loving the NotesRepository very much - a post for another time) and tidied up a bit. I've also updated both versions of the app to use Autofac 2.2.

Both versions of this application live inside my experimental repository on GitHub. To get the code, either clone the repository or grab a download and you will find both apps side-by-side. Feel free to have a poke around and see what I've done. Also, don't forget to have MongoDB running when you fire the apps up.

As I said before, the app is not as fully-featured as it probably should be (you still can't edit notes for example) and it could definitely be argued that using an IoC container is overkill. However I wanted to play around in the context of an application which could grow without any pain rather than keeping it a teeny-tiny demo only. Think of it of an unfinished app with scope for more features.

Final Thoughts

I completely love NoRM. It makes interacting with MongoDB from .NET pretty much 100% painless. Out of the box it contains a sane set of conventions for mapping your objects to MongoDB and naming the fields/collections for you. However you can override this easily using mapping classes. I have taken advantage of this to make sure both versions of the demo app use the exact same data inside MongoDB.

Hopefully my little app makes it clear how flexible, powerful and enjoyable the .NET/MongoDB/NoRM combination can be, and inspires others to go and have a play. Of course, I've barely scratched the surface with what I've put together and no doubt there will be some very cool stuff built up on great new technologies such as these.