Click here to Skip to main content
15,881,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I came across the following problem:
Currently I'm refactoring my project using TDD. There is an existed domain that I can't change.

Code example:
public class Product: IEquatable<Product>
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Equals(Product other)
    {
        if (other == null)
            return false;
        if (Id == other.Id && Name == other.Name)
            return true;
        return false;
    }
}

[TestClass]
public class ProductTest
{
    [TestMethod]
    public void Test1()
    {
        var product1 = new Product {Id = 100, Name = "iPhone"};
        var product2 = new Product {Id = 100, Name = "iPhone"};
        Assert.IsTrue(product1.Equals(product2));
    }
}


My goal is to find a quick solution to compare complex objects as structures and not implement IEquatable<t> cause I can't extend bussiness layer. I mean something like Automapper but not to map but just to compare. Please, give some advices.

Thank in advance.
Posted
Updated 7-Jun-11 23:00pm
v5

1 solution

You do not to implement the interface IEquitable for comparison of structures of the same type as you're trying to do. This interface is primarily indented to implement comparison of instances of different types not even related by inheritance.

You can override object.Equals. Additionally, you can implement "==" and "!=" operators (only both). You also will need to override object.GetHashCode (do I have to explain why?).

Will it be considered as your intervention into your "business layer"? It would mean that you need to implement comparison outside the type declarations. If so, you have one more opportunities.

You can implement comparison as extension method.
Here is a detailed description of the technique: http://msdn.microsoft.com/en-us/library/bb383977.aspx[^].

As extension methods are defined in a separate static class without modification of the types to be compared, you can implement such class in your testing units and not in the business layer. Even if your business layer had access to your extension method definition, in all cases this method is completely non-intrusive.

Finally, you can develop a completely separate comparison unit using Reflection. You can use the type System.Type obtained by using the method GetType of any object. Then, using the methods System.Type.GetMembers or System.Type.GetFields and System.Type.GetProperties you can scan all members you want and compare the values using the methods System.Reflection.PropertyInfo.GetValue and System.Reflection.FieldInfo.GetValue.

This method is good because you can access non-public members. It's relatively slow, but you can prefetch already reflected meta-data using Lazy evaluation (http://en.wikipedia.org/wiki/Lazy_evaluation[^]).

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900