Skip to content
atheken edited this page Aug 13, 2010 · 1 revision

Overview

In most use cases, we believe that using C# class definitions to define your document structures will serve you best. There are some cases where it is not possible to know the structure of the data ahead of time, or there are properties on the document that are set by other applications. For these cases, we have created an infrastructure around the concept of a Expando.

The ‘Workin’ without a net model’:

When you use a Expando, you are essentially using an object that has a dictionary of key-value pairs, you may define any arbitrary value on it, so long as it doesn’t violate MongoDB’s requirements for what document properties can be named, and follows our BSON Serializer guidelines. Anytime the content of a collection is unknown, you should be able to do the following:

//open a session
var s = new Session("JunkDrawerDB");
var expandoObjs = s.GetCollection<Expando>("collectionOfUnknowns");
foreach(var exp in expandoObjs)
{
  //look up and print an arbitrary value.
  var value = exp["aPropertyName"] ?? "this property was empty";
  Console.Writeline(value);
}

The Hybrid model:

Another possibility is that you need a hybrid model, where some properties are known at compile-time, but additional properties might need to be saved or accessed at runtime.

For this case, you need to do three things:

  1. For any class that needs this capability, implement the “IExpando” interface.
  2. Wherever you need the IExpando capability, make sure to include the NoRM namespace, as you’ll get some useful extension methods on your collection members
TODO: Example.

If it’s possible to do without the IExpando overhead, you should do so.