Friday, August 13, 2010

MongoDB for .NET in 10 minutes with NoRM

Technorati Tags: ,

There is a lot of talk lately in the .NET community of using a document database instead of the traditional relational database. Although I’ve kept up on the reading I haven’t invested any time trying it out for myself. Today I decided to give it a shot and considering the amount of time it took me to get things up and running I’m impressed.

First off go install MongoDB. Download Page

The install couldn’t be much simpler for me. Extract the files and run the server.

Then go download the source for NoRM and compile.

Start up a new project and begin define your class(es).

 

Then define your mapping. This step is optional it appears, however, I’m a fan of the NHibernate style of keeping persistence depedencies like identifier types, etc out of your model so I’m starting off this way.

There currently doesn’t appear to be a lot of options for mappings, mostly because there isn’t a need for lots of options. However, I would like to specify DbReference<T> in this class as well. DbReference<T> is a way to specify that the relationship of <T> is another document. Unfortunately it doesn’t appear to be supported (unless I missed something). If I get further along using NoRM this is likely something I would want to add.


Next is a sample usage of the simple model defined.


image


Obviously this sample isn’t ground breaking. However, I was able to download and install, compile NoRM, and put together a sample in under 10 minutes. I would say try doing that with SQL Server. The install alone takes longer than 10 minutes.


Open questions that I have that I intend to cover in the coming weeks.



  • What is NoRM doing under the covers?
  • How easy will it be to extend the behavior I described earlier when relating to other documents?
  • How does searching against a document database change things. Can I do things like p.FirstName.StartsWith(“f”)?
  • How well will a document database fit in with event sourcing. If it does, is it enough to have “persisted index” approach to get the current document(s) or is better to still keep that code close to the command persisting the event?
  • Do you readers have others?

3 comments:

  1. Thanks for posting this. Using a document database is also something I have been meaning to try out myself for quite a while, after having read other .NET bloggers refer to them more and more often. I have lightly dabbled with an object database using db4o (a little, not-really-that-serious project called Postback on Codeplex).

    So great stuff, but I wanted to ask you for clarification on a comment you made about wanting to be able to declare a DbReference<T> in your mappings. It reminds me of a recent blog post from Ayende about RavenDB includes, where he mentioned using "denormalized references" in a document when linking to other documents. Is this what you were hoping NoRM would allow you to specify, or perhaps were you looking for the ability to specify a stronger association between objects/documents?

    But thank you for this quick little introduction; I hope to see more great stuff on this topic here.

    ReplyDelete
  2. Sorry, the link for the Postback project is broken in the previous post; here is the correct link:

    http://postback.codeplex.com/

    ReplyDelete
  3. Mike you're right I wasn't very clear on the topic of DbReference. I'll dedicate my next post to what it is and an example of what I would hope to accomplish with NoRM's API. It is very similar to Ayende's post of using something like .Include("Address.Id") on the query side. NoRM's API requires you do add a property of DbReference<Address> instead of staying POCO which is the part that I don't necessarily like too much.

    ReplyDelete