Friday, 21 May 2010

Using the Norm.BSON Serializer for testing with MongoDB.



By adding the following code to your MongoDocument base class and adjusting the protection of the Norm BsonSerializer from protected to public...

using Norm.BSON;
public byte[] Serialize()
{
    Return BsonSerializer.Serialize(this);
}

You can then make use of the resulting byte array to compare any two documents.

Expect(Document1.Serialize().SequanceEqual(Document2.Serialize),Is.True)

or maybe add a method to your document base class such as...

public bool BSONSequanceEqual(IMongoDocument obj)
{
   if (obj == null)
   {
     throw new ArgumentNullException("obj");
   }


   return this.Serialize().SequenceEqual(obj.Serialize());
}

Theoretically (I believe) this could then be used to override the equals,==,!= operations if required (but I have not tested this!).