Search code examples
c#object-identityobject-equality

Please explain the technique used in this code to test Object Equality and Identity


Please explain the technique used in this code to test Object Equality and Identity.

Better, if you can supply me any web-link/book-reference for detailed discussion.

[Serializable]
    public abstract class BusinessObject<T> : IBusinessObject where T : BusinessObject<T>
    {
        private int? requestedHashCode;

        public virtual int ID { get; set; }

        public virtual bool Equals(IBusinessObject other)
        {
            if (null == other || !GetType().IsInstanceOfType(other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }

            bool otherIsTransient = Equals(other.ID, default(T));
            bool thisIsTransient = IsTransient();
            if (otherIsTransient && thisIsTransient)
            {
                return ReferenceEquals(other, this);
            }

            return other.ID.Equals(ID);
        }

        protected bool IsTransient()
        {
            return Equals(ID, default(T));
        }

        public override bool Equals(object obj)
        {
            var that = obj as IBusinessObject;
            return Equals(that);
        }

        public override int GetHashCode()
        {
            if (!requestedHashCode.HasValue)
            {
                requestedHashCode = IsTransient() ? base.GetHashCode() : ID.GetHashCode();
            }
            return requestedHashCode.Value;
        }
}

What is a transient object?


Solution

    • it first checks if other is an instance of the same type as the current object. If not, they're not equal
    • it then performs a reference equality to check if other and the current object are the same instance. If they are, obviously they are equal
    • If both other and the current object are transient (i.e. not yet persisted), they don't have an ID, so they can't be compared by ID. Instead, they are compared by reference. (as noted by Marc Gravell in the comments, the test to check if the object is transient is broken; it doesn't make sense to compare an int to default(T))
    • Eventually, their IDs are compared; the objects are considered equal if they have the same ID