Instantly share code, notes, and snippets.

# vkhorikov/1.cs Last active Jun 15, 2017

Value Objects: when to create one?
 public struct DecimalGreaterThanZero : IEquatable { private readonly decimal _value; public DecimalGreaterThanZero(decimal value) { if (CanCreate(value) == false) throw new ArgumentException("Value must be greater than zero.", nameof(value)); _value = value; } public static bool CanCreate(decimal d) { return d > decimal.Zero; } private decimal ValueGreaterThanZero => _value == decimal.Zero ? 1 : _value; public static implicit operator DecimalGreaterThanZero(decimal d) { return new DecimalGreaterThanZero(d); } public static implicit operator decimal(DecimalGreaterThanZero decimalGreaterThanZero) { return decimalGreaterThanZero.ValueGreaterThanZero; } public override bool Equals(object obj) { if (!(obj is DecimalGreaterThanZero)) return false; return Equals((DecimalGreaterThanZero)obj); } public bool Equals(DecimalGreaterThanZero other) { return ValueGreaterThanZero == other.ValueGreaterThanZero; } public static bool operator ==(DecimalGreaterThanZero a, DecimalGreaterThanZero b) { return a.Equals(b); } public static bool operator !=(DecimalGreaterThanZero a, DecimalGreaterThanZero b) { return !a.Equals(b); } public override int GetHashCode() { return ValueGreaterThanZero.GetHashCode(); } }
 DecimalGreaterThanZero value = 0;
 decimal value2 = value1; // value1 is a DecimalGreaterThanZero
 DecimalGreaterThanZero value = (DecimalGreaterThanZero)0;
 public class DecimalGreaterThanZero : ValueObject { private readonly decimal _value; public DecimalGreaterThanZero(decimal value) { if (CanCreate(value) == false) throw new ArgumentException("Value must be greater than zero.", nameof(value)); _value = value; } public static bool CanCreate(decimal d) { return d > decimal.Zero; } public static explicit operator DecimalGreaterThanZero(decimal d) { return new DecimalGreaterThanZero(d); } public static implicit operator decimal(DecimalGreaterThanZero decimalGreaterThanZero) { return decimalGreaterThanZero._value; } protected override bool EqualsCore(DecimalGreaterThanZero other) { return _value == other._value; } protected override int GetHashCodeCore() { return _value.GetHashCode(); } }
 public struct Money : IEquatable { public int Amount { get; } public CurrencySymbol Currency { get; } public bool Equals(Money other) { return Amount == other.Amount && Currency == other.Currency; } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; return obj is Money && Equals((Money)obj); } public override int GetHashCode() { unchecked { return (Amount * 397) ^ (int)Currency; } } }
 public struct Money(int Amount, CurrencySymbol Currency);
 public abstract class ValueObject where T : ValueObject { public override bool Equals(object obj) { var valueObject = obj as T; if (ReferenceEquals(valueObject, null)) 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 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 DecimalGreaterThanZero : ValueObject { private readonly decimal _value; public DecimalGreaterThanZero(decimal value) { if (CanCreate(value) == false) throw new ArgumentException("Value must be greater than zero.", nameof(value)); _value = value; } public static bool CanCreate(decimal d) { return d > decimal.Zero; } private decimal ValueGreaterThanZero => _value == decimal.Zero ? 1 : _value; public static implicit operator DecimalGreaterThanZero(decimal d) { return new DecimalGreaterThanZero(d); } public static implicit operator decimal(DecimalGreaterThanZero decimalGreaterThanZero) { return decimalGreaterThanZero.ValueGreaterThanZero; } protected override bool EqualsCore(DecimalGreaterThanZero other) { return ValueGreaterThanZero == other.ValueGreaterThanZero; } protected override int GetHashCodeCore() { return ValueGreaterThanZero.GetHashCode(); } }
 public DecimalGreaterThanZero(decimal value) { if (CanCreate(value) == false) throw new ArgumentException("Value must be greater than zero.", nameof(value)); _value = value; } public static bool CanCreate(decimal d) { return d > decimal.Zero; }
 private decimal ValueGreaterThanZero => _value == decimal.Zero ? 1 : _value;
 public class DecimalGreaterThanZero : ValueObject { private readonly decimal _value; public DecimalGreaterThanZero(decimal value) { if (CanCreate(value) == false) throw new ArgumentException("Value must be greater than zero.", nameof(value)); _value = value; } public static bool CanCreate(decimal d) { return d > decimal.Zero; } public static implicit operator DecimalGreaterThanZero(decimal d) { return new DecimalGreaterThanZero(d); } public static implicit operator decimal(DecimalGreaterThanZero decimalGreaterThanZero) { return decimalGreaterThanZero._value; } protected override bool EqualsCore(DecimalGreaterThanZero other) { return _value == other._value; } protected override int GetHashCodeCore() { return _value.GetHashCode(); } }
 public static implicit operator DecimalGreaterThanZero(decimal d) { return new DecimalGreaterThanZero(d); }