4

I want to compare two objects without implementing the Equals() method.

What are the downsides of comparing them in this way: 1. serilizing them with Json 2. comparing the results

thanks!

Adibe7
  • 3,469
  • 7
  • 30
  • 36
  • Related: http://stackoverflow.com/questions/2046121/how-to-compare-two-object-in-unit-test-how-to-compare-two-collection-in-unit-tes/2047576#2047576 – Mark Seemann Jan 03 '11 at 15:55
  • We did something similar recently. One thing to watch out for is the ordering of items in collections as this may not be deterministic. You would be better off using reflection and recursively traversing the object if you do not need to compare your object value with a persisted value. – SharePoint Newbie Jan 03 '11 at 15:58

4 Answers4

9

What are the downsides of comparing them in this way

Loss of speed. Transforming objects into JSON strings and then comparing them is much slower than doing a property by property equals.

Implementing Equals() is always the best way to compare two objects for equality.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • And if i'm doing it in test code only? and with the same Json serializer? – Adibe7 Jan 03 '11 at 15:44
  • @Adibe, why are you only doing it in test code? Why not just use equals? But, for testing purposes, it should be fine. – jjnguy Jan 03 '11 at 15:45
  • 1
    Implementing and maintaining the Equals method is a big overhead for me. I don't need the comparison because these object are "garbage In Garbage Out" objects, and i only need the comparison to perform tests on my mock. – Adibe7 Jan 03 '11 at 15:48
3

The downside is that you need to serialize them, which is potentially slow, and definitely slower than implementing Equals.

You may also end up with part of the objects that you need to compare not being seriazlied and therefore not getting true comparison.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

There is some overhead to the serialization process to convert objects to json. You'd have to test to see if the overhead is acceptable for your situation.

That aside, the source of the json object is a concern. I've seen a couple different json serializers format objects differently (e.g. quoting property names vs. not quoting them). Things like this could yield you untrue results.

JamesEggers
  • 12,885
  • 14
  • 59
  • 86
1

Maybe with a class like that you can do the work (BsonDocument is a class from MongoDBDriver):

    public class Comparer
    {
        private object first, second;

        public Comparer(object first, object second)
        {
            this.first = first;
            this.second = second;
        }

        public List<string> Compare()
        {
            if (first.GetType() != second.GetType())
            {
                return null;
            }

            BsonDocument firstDoc = first.ToBsonDocument();
            BsonDocument secondDoc = second.ToBsonDocument();

            return Compare(firstDoc, secondDoc);
        }

        private List<string> Compare(BsonDocument first, BsonDocument second)
        {
            List<string> changedFields = new List<string>();

            foreach (string elementName in first.Names)
            {
                BsonElement element = first.GetElement(elementName);

                if (element.Value.IsBsonDocument)
                {
                    BsonDocument elementDocument = element.Value.AsBsonDocument;

                    BsonDocument secondElementDocument =
                        second.GetElement(element.Name).Value.AsBsonDocument;

                    if (elementDocument.ElementCount > 1 &&
                        secondElementDocument.ElementCount ==
                        elementDocument.ElementCount)
                    {
                        foreach (string value in (Compare(elementDocument,
                                                       secondElementDocument)))
                        {
                            changedFields.Add(value);
                        }
                    }

                    else
                    {
                        changedFields.Add(element.Name);
                    }
                }

                else if (element.Value != second.GetElement(element.Name).Value)
                    changedFields.Add(element.Name);
            }

            return changedFields;
        }
}
Coconut
  • 111
  • 1
  • 12