Skip to content

Instantly share code, notes, and snippets.

@alexsandro-xpt
Forked from vkhorikov/1.cs
Created March 10, 2019 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexsandro-xpt/14c9bbddacace30960b1689092fe5e46 to your computer and use it in GitHub Desktop.
Save alexsandro-xpt/14c9bbddacace30960b1689092fe5e46 to your computer and use it in GitHub Desktop.
Value Object
public abstract class ValueObject<T>
where T : ValueObject<T>
{
public override bool Equals(object obj)
{
var valueObject = obj as T;
if (ReferenceEquals(valueObject, null))
return false;
if (GetType() != obj.GetType())
return false;
return EqualsCore(valueObject);
}
protected abstract bool EqualsCore(T other);
public override int GetHashCode()
{
return GetHashCodeCore();
}
protected abstract int GetHashCodeCore();
public static bool operator ==(ValueObject<T> a, ValueObject<T> b)
{
if (ReferenceEquals(a, null) && ReferenceEquals(b, null))
return true;
if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
return false;
return a.Equals(b);
}
public static bool operator !=(ValueObject<T> a, ValueObject<T> b)
{
return !(a == b);
}
}
public class Address : ValueObject<Address>
{
public string Street { get; }
public string City { get; }
public string ZipCode { get; }
public Address(string street, string city, string zipCode)
{
Street = street;
City = city;
ZipCode = zipCode;
}
protected override bool EqualsCore(Address other)
{
return Street == other.Street
&& City == other.City
&& ZipCode == other.ZipCode;
}
protected override int GetHashCodeCore()
{
unchecked
{
int hashCode = Street.GetHashCode();
hashCode = (hashCode * 397) ^ City.GetHashCode();
hashCode = (hashCode * 397) ^ ZipCode.GetHashCode();
return hashCode;
}
}
}
public abstract class ValueObject
{
protected abstract IEnumerable<object> GetEqualityComponents();
public override bool Equals(object obj)
{
if (obj == null)
return false;
if (GetType() != obj.GetType())
return false;
var valueObject = (ValueObject)obj;
return GetEqualityComponents().SequenceEqual(valueObject.GetEqualityComponents());
}
public override int GetHashCode()
{
return GetEqualityComponents()
.Aggregate(1, (current, obj) =>
{
unchecked
{
return current * 23 + (obj?.GetHashCode() ?? 0);
}
});
}
public static bool operator ==(ValueObject a, ValueObject b)
{
if (ReferenceEquals(a, null) && ReferenceEquals(b, null))
return true;
if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
return false;
return a.Equals(b);
}
public static bool operator !=(ValueObject a, ValueObject b)
{
return !(a == b);
}
}
public class Address : ValueObject
{
public string Street { get; }
public string City { get; }
public string ZipCode { get; }
public Address(string street, string city, string zipCode)
{
Street = street;
City = city;
ZipCode = zipCode;
}
protected override IEnumerable<object> GetEqualityComponents()
{
yield return Street;
yield return City;
yield return ZipCode;
}
}
protected override IEnumerable<object> GetEqualityComponents()
{
yield return Street;
yield return City;
yield return ZipCode;
}
public class Address : ValueObject
{
public string Street { get; }
public string City { get; }
public string ZipCode { get; }
public List<Tenant> Tenants { get; }
public Address(string street, string city, string zipCode, List<Tenant> tenants)
{
Street = street;
City = city;
ZipCode = zipCode;
Tenants = tenants;
}
protected override IEnumerable<object> GetEqualityComponents()
{
yield return Street;
yield return City;
yield return ZipCode;
foreach (Tenant tenant in Tenants)
{
yield return tenant;
}
}
}
public class Money : ValueObject
{
public string Currency { get; }
public decimal Amount { get; }
public Money(string currency, decimal amount)
{
Currency = currency;
Amount = amount;
}
protected override IEnumerable<object> GetEqualityComponents()
{
yield return Currency.ToUpper();
yield return Math.Round(Amount, 2);
}
}
var money1 = new Money("usd", 2.2222m);
var money2 = new Money("USD", 2.22m);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment