Ejemplo n.º 1
0
        /// <summary>
        ///   Should ONLY contain the "business value signature" of the object and not the Id, 
        ///   which is handled by <see cref = "Entity" />.  This method should return a unique 
        ///   int representing a unique signature of the domain object.  For 
        ///   example, no two different orders should have the same ShipToName, OrderDate and OrderedBy;
        ///   therefore, the returned "signature" should be expressed as demonstrated below.
        /// 
        ///   Alternatively, we could decorate properties with the [DomainSignature] attribute, as shown in
        ///   <see cref = "Customer" />, but here's an example of overriding it nonetheless.
        /// </summary>
        public override bool HasSameObjectSignatureAs(BaseObject compareTo)
        {
            var orderCompareTo = compareTo as Order;

            return orderCompareTo != null && this.ShipToName.Equals(orderCompareTo.ShipToName) &&
                   (this.OrderDate ?? DateTime.MinValue).Equals(orderCompareTo.OrderDate ?? DateTime.MinValue) &&
                   this.OrderedBy.Equals(orderCompareTo.OrderedBy);
        }
        /// <summary>
        ///     Determines whether the current object has the same object signature as the specified object.
        /// </summary>
        /// <param name="compareTo">The object to compare to.</param>
        /// <returns>
        ///     <c>true</c> if the current object has the same object signature as the specified object; otherwise,
        ///     <c>false</c>.
        /// </returns>
        /// <remarks>You may override this method to provide your own comparison routine.</remarks>
        public virtual bool HasSameObjectSignatureAs(BaseObject compareTo)
        {
            PropertyInfo[] signatureProperties = this.GetSignatureProperties();

            // if there were no signature properties, then simply return the default behavior of Equals
            if (signatureProperties.Length == 0)
            {
                // ReSharper disable once BaseObjectEqualsIsObjectEquals
                return base.Equals(compareTo);
            }

            // use for loop instead of foreach/LINQ for performance reasons.
            // ReSharper disable once ForCanBeConvertedToForeach
            // ReSharper disable once LoopCanBeConvertedToQuery
            for (var index = 0; index < signatureProperties.Length; index++)
            {
                PropertyInfo property = signatureProperties[index];
                object valueOfThisObject = property.GetValue(this, null);
                object valueToCompareTo = property.GetValue(compareTo, null);

                if (valueOfThisObject == null && valueToCompareTo == null)
                {
                    continue;
                }

                if ((valueOfThisObject == null ^ valueToCompareTo == null) ||
                    (!valueOfThisObject.Equals(valueToCompareTo)))
                {
                    return false;
                }
            }

            // If we've gotten this far and signature properties were found, then we can
            // assume that everything matched
            return true;
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     You may override this method to provide your own comparison routine.
        /// </summary>
        public virtual bool HasSameObjectSignatureAs(BaseObject compareTo)
        {
            var signatureProperties = this.GetSignatureProperties();

            if ((from property in signatureProperties
                 let valueOfThisObject = property.GetValue(this, null)
                 let valueToCompareTo = property.GetValue(compareTo, null)
                 where valueOfThisObject != null || valueToCompareTo != null
                 where (valueOfThisObject == null ^ valueToCompareTo == null) || (!valueOfThisObject.Equals(valueToCompareTo))
                 select valueOfThisObject).Any())
            {
                return false;
            }

            // If we've gotten this far and signature properties were found, then we can
            // assume that everything matched; otherwise, if there were no signature 
            // properties, then simply return the default bahavior of Equals
            return signatureProperties.Any() || base.Equals(compareTo);
        }