I have come across a very subtle bug in the EntityBase == operator overload method in the following statement:
if (base1.Key != base2.Key)
This does not work if the base1.Key and base2.Key properties don't reference the same Guid object, but have the same value. The statement does work if both Key properties reference the same Guid object. The fix for this bug is to replace the statement with this one:
This second statement works fine in all cases. The reason for this has to do with the fact that the == and != operator overloads are not virtual whereas the Equals method is virtual. So, what happens in the first statement is that the != operator overload for the System.Object class is called and not the overload defined in the Guid class, since the Key property is declared as type object. This causes a reference comparison and not a value comparison. A value comparison is what is needed for this operator overload to work as needed. The second statement will cause the Equals method of the Guid struct to be called and not the one defined for the System.Object class, since it is virtual, and the correct value comparison between the two Guids is done, instead of a reference comparison.
Here is a unit test that demonstrates the issue:
public void EntitiesAreEqualIfDifferentEntityAndKeyObjectsButSameKeyValue()
EntityBase entity1 = new EntityBase();
Guid id2 = new Guid(entity1.Key.ToString());
EntityBase entity2 = new EntityBase(id2);
Assert.IsTrue(entity1 == entity2);
Assert.IsFalse(entity1 != entity2);
This unit test will fail on the second Assert. When I change the EntityBase == operator overload method with the new statement above, this unit test passes.