Skip to content
Craig Fowler edited this page Oct 24, 2019 · 3 revisions

The equality rules package provides objects which implement IEqualityComparer<T>, allowing comparison of object instances for equality (and getting hash codes). In fact, the return type also implements the interface IGetsEqualityResult<T>. This interface exposes a method which provides a more detailed result, indicating how compared objects differ, instead of a simple boolean.

Building your comparer

In order to get a comparer, you must first build it, specifying the rules which determine how the objects are to be compared. The process is:

  1. Create an instance of the EqualityBuilder<T> class
  2. Add rules to the builder
  3. Create the comparer from the builder

That last step is very simple, you must simply call .Build() from the builder. The return from this function is the reusable instance of IGetsEqualityResult<T>.

Using the comparer

Usage as a 'normal' equality comparer

The interface IGetsEqualityResult<T> implements the built-in IEqualityComparer<T>. If you wish, you may implicitly down-cast and use it like any normal equality comparer.

Getting detailed equality results

The IGetsEqualityResult<T> interface provides one additional method, and that is:

EqualityResult GetEqualityResult(T x, T y);

An EqualityResult object has a boolean property AreEqual indicating whether or not the compared objects are equal or not. It also has a RuleResults property which exposes a collection of individual pass/fail results, for each of the rules with which the equality comparer was built.

This may be used to inspect how objects differ. This may then be used to 'drive' further logic, if desired.