Skip to content

Instantly share code, notes, and snippets.

@JonathanLoscalzo
Forked from marisks/Sample.cs
Created March 31, 2018 01:39
Show Gist options
  • Save JonathanLoscalzo/6001c228c67bc344d07b58e8ba03b841 to your computer and use it in GitHub Desktop.
Save JonathanLoscalzo/6001c228c67bc344d07b58e8ba03b841 to your computer and use it in GitHub Desktop.
Improved Jimmy Bogard's ValueObject<T> which supports inheritance
// 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
}
}
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
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)
{
if (ReferenceEquals(x, y))
{
return true;
}
if (((object)x == null) || ((object)y == null))
{
return false;
}
return x.Equals(y);
}
public static bool operator !=(ValueObject<T> x, ValueObject<T> y)
{
return !(x == y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment