/// <summary> /// To help ensure hashcode uniqueness, a carefully selected random number multiplier /// is used within the calculation. Goodrich and Tamassia's Data Structures and /// Algorithms in Java asserts that 31, 33, 37, 39 and 41 will produce the fewest number /// of collissions. See http://computinglife.wordpress.com/2008/11/20/why-do-hash-functions-use-prime-numbers/ /// for more information. /// </summary> public override int GetHashCode() { if (cachedHashcode.HasValue) { return(cachedHashcode.Value); } if (IsTransient()) { //this.cachedHashcode = base.GetHashCode(); return(TransientId.GetHashCode()); //don't cache because this won't stay transient forever } else { unchecked { // It's possible for two objects to return the same hash code based on // identically valued properties, even if they're of two different types, // so we include the object's type in the hash calculation var hashCode = GetType().GetHashCode(); cachedHashcode = (hashCode * HashMultiplier) ^ Id.GetHashCode(); } } return(cachedHashcode.Value); }
public override bool Equals(object obj) { var compareTo = obj as AuditEntityConfiguration; if (ReferenceEquals(this, compareTo)) { return(true); } if (compareTo == null || !GetType().Equals(compareTo.GetTypeUnproxied())) { return(false); } if (HasSameNonDefaultIdAs(compareTo)) { return(true); } // Since the Ids aren't the same, both of them must be transient to // compare domain signatures; because if one is transient and the // other is a persisted entity, then they cannot be the same object. return(IsTransient() && compareTo.IsTransient() && (base.Equals(compareTo) || TransientId.Equals(compareTo.TransientId))); }