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.
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);
}
}
@MisinformedDNA
Copy link

This is nice alternative I found: https://enterprisecraftsmanship.com/posts/value-object-better-implementation/. Slightly more work, but more control and no reflection.

@marisks
Copy link
Author

marisks commented May 5, 2022

On latest .NET and C#, I would just use record types now.

@MisinformedDNA
Copy link

Here's some reasons why records don't always make good Value Objects. There's also a good discussion here.

@marisks
Copy link
Author

marisks commented Jun 1, 2022

Thanks for the info!
I am not working on domain-heavy apps anymore. So records are enough for my needs. The only thing - collection comparison is missing for me. For me equality is the biggest advantage - it makes unit testing much simpler. But if records with collection properties can't be compared that's bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment