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