| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using Abp.Reflection;
- using System;
- using System.Linq;
- using System.Reflection;
- namespace Abp.Domain.Values
- {
- //Inspired from https://blogs.msdn.microsoft.com/cesardelatorre/2011/06/06/implementing-a-value-object-base-class-supertype-patternddd-patterns-related/
- /// <summary>
- /// Base class for value objects.
- /// </summary>
- /// <typeparam name="TValueObject">The type of the value object.</typeparam>
- public abstract class ValueObject<TValueObject> : IEquatable<TValueObject>
- where TValueObject : ValueObject<TValueObject>
- {
- public bool Equals(TValueObject other)
- {
- if ((object)other == null)
- {
- return false;
- }
- var publicProperties = GetPropertiesForCompare();
- if (!publicProperties.Any())
- {
- return true;
- }
- return publicProperties.All(property => Equals(property.GetValue(this, null), property.GetValue(other, null)));
- }
- public override bool Equals(object obj)
- {
- if (obj == null)
- {
- return false;
- }
- var item = obj as ValueObject<TValueObject>;
- return (object)item != null && Equals((TValueObject)item);
- }
- public override int GetHashCode()
- {
- const int index = 1;
- const int initialHasCode = 31;
- var publicProperties = GetPropertiesForCompare();
- if (!publicProperties.Any())
- {
- return initialHasCode;
- }
- var hashCode = initialHasCode;
- var changeMultiplier = false;
- foreach (var property in publicProperties)
- {
- var value = property.GetValue(this, null);
- if (value == null)
- {
- //support {"a",null,null,"a"} != {null,"a","a",null}
- hashCode = hashCode ^ (index * 13);
- continue;
- }
- hashCode = hashCode * (changeMultiplier ? 59 : 114) + value.GetHashCode();
- changeMultiplier = !changeMultiplier;
- }
- return hashCode;
- }
- public static bool operator ==(ValueObject<TValueObject> x, ValueObject<TValueObject> y)
- {
- if (ReferenceEquals(x, y))
- {
- return true;
- }
- if (((object)x == null) || ((object)y == null))
- {
- return false;
- }
- return x.Equals(y);
- }
- public static bool operator !=(ValueObject<TValueObject> x, ValueObject<TValueObject> y)
- {
- return !(x == y);
- }
- private PropertyInfo[] GetPropertiesForCompare()
- {
- return GetType().GetTypeInfo().GetProperties().Where(t => ReflectionHelper.GetSingleAttributeOrDefault<IgnoreOnCompareAttribute>(t) == null).ToArray();
- }
- }
- }
|