Skip to content

Instantly share code, notes, and snippets.

@marisks
Last active December 9, 2023 13:35
Show Gist options
  • Save marisks/f9938777ba590b1645376e783f5980ff to your computer and use it in GitHub Desktop.
Save marisks/f9938777ba590b1645376e783f5980ff to your computer and use it in GitHub Desktop.

Revisions

  1. marisks revised this gist Mar 10, 2017. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion ValueObject.cs
    Original 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)
    {
    // ReSharper disable once PossibleNullReferenceException
    if (ReferenceEquals(x, y))
    {
    return true;
    }

    if (((object)x == null) || ((object)y == null))
    {
    return false;
    }

    return x.Equals(y);
    }

  2. marisks revised this gist Mar 10, 2017. 1 changed file with 44 additions and 44 deletions.
    88 changes: 44 additions & 44 deletions ValueObject.cs
    Original 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());
    }
    (current, value) => current * multiplier + value.GetHashCode());
    }

    public virtual bool Equals(T other)
    {
    if (other == null)
    return false;
    public virtual bool Equals(T other)
    {
    if (other == null)
    return false;

    var t = GetType();
    var otherType = other.GetType();
    var t = GetType();
    var otherType = other.GetType();

    if (t != otherType)
    return false;
    if (t != otherType)
    return false;

    var fields = GetFields(this);
    var fields = GetFields(this);

    foreach (var field in fields)
    {
    var value1 = field.GetValue(other);
    var value2 = field.GetValue(this);

    foreach (var field in fields)
    if (value1 == null)
    {
    var value1 = field.GetValue(other);
    var value2 = field.GetValue(this);

    if (value1 == null)
    {
    if (value2 != null)
    return false;
    }
    else if (!value1.Equals(value2))
    if (value2 != null)
    return false;
    }

    return true;
    else if (!value1.Equals(value2))
    return false;
    }

    private static IEnumerable<FieldInfo> GetFields(object obj)
    {
    var t = obj.GetType();
    return true;
    }

    var fields = new List<FieldInfo>();
    private static IEnumerable<FieldInfo> GetFields(object obj)
    {
    var t = obj.GetType();

    while (t != typeof(object))
    {
    if (t == null) continue;
    fields.AddRange(t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));
    var fields = new List<FieldInfo>();

    t = t.BaseType;
    }
    while (t != typeof(object))
    {
    if (t == null) continue;
    fields.AddRange(t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));

    return fields;
    t = t.BaseType;
    }

    public static bool operator ==(ValueObject<T> x, ValueObject<T> y)
    {
    // ReSharper disable once PossibleNullReferenceException
    return x.Equals(y);
    }
    return fields;
    }

    public static bool operator !=(ValueObject<T> x, ValueObject<T> y)
    {
    return !(x == y);
    }
    }
    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);
    }
    }
  3. marisks revised this gist Mar 10, 2017. 1 changed file with 19 additions and 22 deletions.
    41 changes: 19 additions & 22 deletions ValueObject.cs
    Original 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

    namespace EpiEvents.Core.Common
    public abstract class ValueObject<T> : IEquatable<T>
    where T : ValueObject<T>
    {
    public abstract class ValueObject<T> : IEquatable<T>
    where T : ValueObject<T>
    public override bool Equals(object obj)
    {
    public override bool Equals(object obj)
    {
    if (obj == null)
    return false;
    if (obj == null)
    return false;

    var other = obj as T;
    var other = obj as T;

    return Equals(other);
    }
    return Equals(other);
    }

    public override int GetHashCode()
    {
    var fields = GetFields(this);
    public override int GetHashCode()
    {
    var fields = GetFields(this);

    var startValue = 17;
    var multiplier = 59;
    var startValue = 17;
    var multiplier = 59;

    return fields
    .Select(field => field.GetValue(this))
    .Where(value => value != null)
    .Aggregate(
    startValue,
    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);
    }
    }
    }
    }
  4. marisks created this gist Mar 10, 2017.
    36 changes: 36 additions & 0 deletions Sample.cs
    Original 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
    }
    }
    97 changes: 97 additions & 0 deletions ValueObject.cs
    Original 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);
    }
    }
    }