PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Framework/SeguridadCiudadana.Fwk.Domain.Core/ValueObject.cs

https://gitlab.com/dariuchizz/Seguridad
C# | 134 lines | 87 code | 25 blank | 22 comment | 17 complexity | 280f123c07c35a4443e4368add9f2932 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. namespace SeguridadCiudadana.Fwk.Domain.Core
  7. {
  8. /// <summary>
  9. /// Base class for value objects in domain.
  10. /// Value
  11. /// </summary>
  12. /// <typeparam name="TValueObject">The type of this value object</typeparam>
  13. public class ValueObject<TValueObject> : IEquatable<TValueObject>
  14. where TValueObject : ValueObject<TValueObject>
  15. {
  16. #region IEquatable and Override Equals operators
  17. /// <summary>
  18. /// <see cref="M:System.Object.IEquatable{TValueObject}"/>
  19. /// </summary>
  20. /// <param name="other"><see cref="M:System.Object.IEquatable{TValueObject}"/></param>
  21. /// <returns><see cref="M:System.Object.IEquatable{TValueObject}"/></returns>
  22. public bool Equals(TValueObject other)
  23. {
  24. if ((object)other == null)
  25. return false;
  26. if (Object.ReferenceEquals(this, other))
  27. return true;
  28. //compare all public properties
  29. PropertyInfo[] publicProperties = this.GetType().GetProperties();
  30. if ((object)publicProperties != null
  31. &&
  32. publicProperties.Any())
  33. {
  34. return publicProperties.All(p =>
  35. {
  36. var left = p.GetValue(this, null);
  37. var right = p.GetValue(other, null);
  38. if (typeof(TValueObject).IsAssignableFrom(left.GetType()))
  39. {
  40. //check not self-references...
  41. return Object.ReferenceEquals(left, right);
  42. }
  43. else
  44. return left.Equals(right);
  45. });
  46. }
  47. else
  48. return true;
  49. }
  50. /// <summary>
  51. /// <see cref="M:System.Object.Equals"/>
  52. /// </summary>
  53. /// <param name="obj"><see cref="M:System.Object.Equals"/></param>
  54. /// <returns><see cref="M:System.Object.Equals"/></returns>
  55. public override bool Equals(object obj)
  56. {
  57. if ((object)obj == null)
  58. return false;
  59. if (Object.ReferenceEquals(this, obj))
  60. return true;
  61. ValueObject<TValueObject> item = obj as ValueObject<TValueObject>;
  62. if ((object)item != null)
  63. return Equals((TValueObject)item);
  64. else
  65. return false;
  66. }
  67. /// <summary>
  68. /// <see cref="M:System.Object.GetHashCode"/>
  69. /// </summary>
  70. /// <returns><see cref="M:System.Object.GetHashCode"/></returns>
  71. public override int GetHashCode()
  72. {
  73. int hashCode = 31;
  74. bool changeMultiplier = false;
  75. int index = 1;
  76. //compare all public properties
  77. PropertyInfo[] publicProperties = this.GetType().GetProperties();
  78. if ((object)publicProperties != null
  79. &&
  80. publicProperties.Any())
  81. {
  82. foreach (var item in publicProperties)
  83. {
  84. object value = item.GetValue(this, null);
  85. if ((object)value != null)
  86. {
  87. hashCode = hashCode * ((changeMultiplier) ? 59 : 114) + value.GetHashCode();
  88. changeMultiplier = !changeMultiplier;
  89. }
  90. else
  91. hashCode = hashCode ^ (index * 13);//only for support {"a",null,null,"a"} <> {null,"a","a",null}
  92. }
  93. }
  94. return hashCode;
  95. }
  96. public static bool operator ==(ValueObject<TValueObject> left, ValueObject<TValueObject> right)
  97. {
  98. if (Object.Equals(left, null))
  99. return (Object.Equals(right, null)) ? true : false;
  100. else
  101. return left.Equals(right);
  102. }
  103. public static bool operator !=(ValueObject<TValueObject> left, ValueObject<TValueObject> right)
  104. {
  105. return !(left == right);
  106. }
  107. #endregion
  108. }
  109. }