Last active
December 9, 2023 13:35
-
-
Save marisks/f9938777ba590b1645376e783f5980ff to your computer and use it in GitHub Desktop.
Revisions
-
marisks revised this gist
Mar 10, 2017 . 1 changed file with 10 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -83,7 +83,16 @@ private static IEnumerable<FieldInfo> GetFields(object obj) public static bool operator ==(ValueObject<T> x, ValueObject<T> y) { if (ReferenceEquals(x, y)) { return true; } if (((object)x == null) || ((object)y == null)) { return false; } return x.Equals(y); } -
marisks revised this gist
Mar 10, 2017 . 1 changed file with 44 additions and 44 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,64 +31,64 @@ public override int GetHashCode() .Where(value => value != null) .Aggregate( startValue, (current, value) => current * multiplier + value.GetHashCode()); } public virtual bool Equals(T other) { if (other == null) return false; var t = GetType(); var otherType = other.GetType(); if (t != otherType) return false; var fields = GetFields(this); foreach (var field in fields) { var value1 = field.GetValue(other); var value2 = field.GetValue(this); if (value1 == null) { if (value2 != null) return false; } else if (!value1.Equals(value2)) return false; } return true; } private static IEnumerable<FieldInfo> GetFields(object obj) { var t = obj.GetType(); var fields = new List<FieldInfo>(); while (t != typeof(object)) { if (t == null) continue; fields.AddRange(t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); t = t.BaseType; } return fields; } public static bool operator ==(ValueObject<T> x, ValueObject<T> y) { // ReSharper disable once PossibleNullReferenceException return x.Equals(y); } public static bool operator !=(ValueObject<T> x, ValueObject<T> y) { return !(x == y); } } -
marisks revised this gist
Mar 10, 2017 . 1 changed file with 19 additions and 22 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,33 +6,31 @@ // This base class comes from Jimmy Bogard, but with support of inheritance // http://grabbagoft.blogspot.com/2007/06/generic-value-object-equality.html public abstract class ValueObject<T> : IEquatable<T> where T : ValueObject<T> { public override bool Equals(object obj) { if (obj == null) return false; var other = obj as T; return Equals(other); } public override int GetHashCode() { var fields = GetFields(this); var startValue = 17; var multiplier = 59; return fields .Select(field => field.GetValue(this)) .Where(value => value != null) .Aggregate( startValue, (current, value) => current*multiplier + value.GetHashCode()); } @@ -93,5 +91,4 @@ private static IEnumerable<FieldInfo> GetFields(object obj) { return !(x == y); } } -
marisks created this gist
Mar 10, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ // Ordinary public class SimpleDto : ValueObject<SimpleDto> { public string Value { get; set; } } // With base class public abstract class BaseDto<T> : ValueObject<T> { public string BaseValue { get; set; } } public class DtoWithBase : BaseDto<DtoWithBase> { public string Value { get; set; } } public class Program { static void Main() { var s1 = new SimpleDto { Value = "Sample" }; var s2 = new SimpleDto { Value = "Sample" }; var s3 = new SimpleDto { Value = "Sample2" }; var e1 = s1 == s2; // true var e2 = s1 == s3; // false var d1 = new DtoWithBase { Value = "Sample", BaseValue = "Sample" }; var d2 = new DtoWithBase { Value = "Sample", BaseValue = "Sample" }; var d3 = new DtoWithBase { Value = "Sample", BaseValue = "Sample2" }; var e3 = d1 == d2; // true var e4 = d1 == d3; // false } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,97 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; // This base class comes from Jimmy Bogard, but with support of inheritance // http://grabbagoft.blogspot.com/2007/06/generic-value-object-equality.html namespace EpiEvents.Core.Common { public abstract class ValueObject<T> : IEquatable<T> where T : ValueObject<T> { public override bool Equals(object obj) { if (obj == null) return false; var other = obj as T; return Equals(other); } public override int GetHashCode() { var fields = GetFields(this); var startValue = 17; var multiplier = 59; return fields .Select(field => field.GetValue(this)) .Where(value => value != null) .Aggregate( startValue, (current, value) => current*multiplier + value.GetHashCode()); } public virtual bool Equals(T other) { if (other == null) return false; var t = GetType(); var otherType = other.GetType(); if (t != otherType) return false; var fields = GetFields(this); foreach (var field in fields) { var value1 = field.GetValue(other); var value2 = field.GetValue(this); if (value1 == null) { if (value2 != null) return false; } else if (!value1.Equals(value2)) return false; } return true; } private static IEnumerable<FieldInfo> GetFields(object obj) { var t = obj.GetType(); var fields = new List<FieldInfo>(); while (t != typeof(object)) { if (t == null) continue; fields.AddRange(t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); t = t.BaseType; } return fields; } public static bool operator ==(ValueObject<T> x, ValueObject<T> y) { // ReSharper disable once PossibleNullReferenceException return x.Equals(y); } public static bool operator !=(ValueObject<T> x, ValueObject<T> y) { return !(x == y); } } }