This ticket is to investigate, devise and implement an improvement for the way in which identity instances may be compared for equality.
Current problem
At present, when comparing two instances of IIdentity
for equality, the developer is likely to use the equality operator: ==
. This is problematic because if the compared variables are presently typed using their interfaces (which is encouraged) then reference equality will be used instead of value equality. Because behind the scenes identity objects are structs, reference equalitywill never be true.
Considerations
Typing identities as interfaces is useful
It is beneficial to type identities as interfaces. This is in order to support generic covariance. If I were to solve this problem by not using interfaces, I would lose the ability to do the following (assuming Cat is a subclass of Animal):
IIdentity<Cat> catId = GetCatId();
IIdentity<Animal> animalId = catId;
Equality operator overloads are incompatible with interfaces
Whilst the variables are typed as interfaces, operator overloads upon the identity type, such as the equality operator, will not be used under any circumstances.
There is some discussion regarding this but I don't see it being implemented any time soon, given that this proposal is already 3 years old.
Unlikely to benefit from using classes instead of structs
Given that the core problem is the mutual exclusivity of covariance (interfaces only) and operator overloading (not interfaces), I doubt that there is a simple solution in swapping from structs to classes for the implementations.
In fact, after some investigation, I am already using classes and not structs for identity objects.
Workaround
As well as being a workaround, this could even be a long-term solution.
Instead of using the equality operator to compare identities, use the static Object.Equals(object, object)
method to compare them.
So, instead of this:
Use this:
Equals(identityA, identityB)
This will use the overridden Equals methods on the underlying identity objects which will return the correct result.