Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

The C# driver for MongoDb works with plain documents. Unfortunately, we don’t live in a document oriented world. Our world is more of an object oriented. There are numerous ways of converting the object structure into documents and I have blogged about this before. If you are interested here is an article which takes about ToDocument() method which can be used to convert objects to documents.

Implementing Business Object to Documents Converter

While making these conversions I thought that maybe I will use an Object Relational Mapping tool to write my classes. So, I used LINQ to SQL framework to create my entity classes or I think I should refer to them as mapping classes. The problem with creating classes using an OR mapper is that it creates a two way relationship between objects. A customer object can have many addresses but why does an address object needs a reference back to the customer when we know that we will never access the address without the customer object.

The main problem comes when you want to serialize these objects. The serialization will return in circular dependency problems. The only way to serialize is to either make the relationship internal or null out the relationship. Both of them are pretty bad techniques.

My conclusion is that when using MongoDb as your data source take the time to manually craft your entities and do not rely on OR mappers to create the relationships for you because most of the OR mappers will create wrong relationships. 

Posted on Tuesday, August 31, 2010 3:22 AM | Back to top


Comments on this post: MongoDb, Serialization and OR Mappers

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net