From dc362dfbd9652aadd3bac1022015effd3f571ec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 6 Sep 2019 19:04:21 +0300 Subject: [PATCH] Resolved #1728: Remove overriding equals for entities and value types. --- .../Volo/Abp/Domain/Entities/Entity.cs | 31 ++----------------- .../Volo/Abp/Domain/Values/ValueObject.cs | 27 +++------------- 2 files changed, 6 insertions(+), 52 deletions(-) diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Entity.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Entity.cs index c96e305d45..07e1648ab4 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Entity.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Entity.cs @@ -35,8 +35,7 @@ namespace Volo.Abp.Domain.Entities Id = id; } - /// - public override bool Equals(object obj) + public bool EntityEquals(object obj) { if (obj == null || !(obj is Entity)) { @@ -73,33 +72,7 @@ namespace Volo.Abp.Domain.Entities return Id.Equals(other.Id); } - - /// - public override int GetHashCode() - { - if (Id == null) - { - return 0; - } - - return Id.GetHashCode(); - } - - public static bool operator ==(Entity left, Entity right) - { - if (Equals(left, null)) - { - return Equals(right, null); - } - - return left.Equals(right); - } - - public static bool operator !=(Entity left, Entity right) - { - return !(left == right); - } - + public override object[] GetKeys() { return new object[] {Id}; diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Values/ValueObject.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Values/ValueObject.cs index ea84b41792..00a4c7ddd3 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Values/ValueObject.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Values/ValueObject.cs @@ -7,23 +7,9 @@ namespace Volo.Abp.Domain.Values public abstract class ValueObject { - protected static bool EqualOperator(ValueObject left, ValueObject right) - { - if (ReferenceEquals(left, null) ^ ReferenceEquals(right, null)) - { - return false; - } - return ReferenceEquals(left, null) || left.Equals(right); - } - - protected static bool NotEqualOperator(ValueObject left, ValueObject right) - { - return !(EqualOperator(left, right)); - } - protected abstract IEnumerable GetAtomicValues(); - public override bool Equals(object obj) + public bool ValueEquals(object obj) { if (obj == null || obj.GetType() != GetType()) { @@ -31,8 +17,10 @@ namespace Volo.Abp.Domain.Values } ValueObject other = (ValueObject)obj; + IEnumerator thisValues = GetAtomicValues().GetEnumerator(); IEnumerator otherValues = other.GetAtomicValues().GetEnumerator(); + while (thisValues.MoveNext() && otherValues.MoveNext()) { if (ReferenceEquals(thisValues.Current, null) ^ @@ -47,15 +35,8 @@ namespace Volo.Abp.Domain.Values return false; } } - return !thisValues.MoveNext() && !otherValues.MoveNext(); - } - public override int GetHashCode() - { - return GetAtomicValues() - .Select(x => x != null ? x.GetHashCode() : 0) - .Aggregate((x, y) => x ^ y); + return !thisValues.MoveNext() && !otherValues.MoveNext(); } - // Other utilility methods } }