I have some Entity Framework objects which are also identified by their own ID.
I prefer to use reference comparison which is probably more reliable, especially for objects not on the DB yet.
Which is the gap between Int32 ID comparison and ByRef object comparison in terms of performance?
The performance of comparing a reference and comparing an
intwill be about the same. TheIDcomparison requires an additional property access call on the stack. However, the performance difference between the two would be negligible.However, this should not be the determining factor as to how you determine equality. Equality should be determined based on what the object represents. If the object is defined by it’s
IDproperty, then theIDproperty should be used to determine equality. If the object is defined by the composition of its values, then you should determine equality by comparing each of its component values.To better understand what I’m saying, consider the following class as an example:
Imagine that you’re app uses the
Ladyclass like:In this case, the two objects might actually refer to the same person. Perhaps
JaneSmithwas created and cached before Jane got married and changed her name. But, now that her name has changed, and now both a value and reference comparison will fail. However, if we use ID equality, then this is okay, because we will know they are supposed to refer to the same person (and the same data source). We can then resolve whichLadyinstance is currently correct by abandoning both instances and reloading theLadywithID == 12from the database. By contrast, if we were using either reference equality or value equality, we would end up saving both objects, possibly overwriting the wrong data. Also, even if the data in both instances were identical and we only had two instances because we accidentally loadedJaneSmithfrom the database twice, the reference equality check would return false. It doesn’t seem quite right that,new Lady(12,"Jane").Equals(new Lady(12,"Jane"))should return false. It also makes caching virtually impossible, because you can never determine if a record has been cached or not.In general, using reference equality too compare persistent objects is a bad idea. However,
IDequality is not always appropriate either. Neither is value equality. There are cases for both, and you have to decide what best represents the data you are manipulating. For more on this, this article provides a very basic concept of the difference between “Entities” and “Value Objects” (concepts from the Domain Driven Design methodology).