Create a gist now

Instantly share code, notes, and snippets.

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

What would you like to do?
Value Objects: when to create one?
public struct DecimalGreaterThanZero : IEquatable<DecimalGreaterThanZero>
{
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<DecimalGreaterThanZero>
{
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<Money>
{
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<T>
where T : ValueObject<T>
{
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<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 DecimalGreaterThanZero : ValueObject<DecimalGreaterThanZero>
{
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<DecimalGreaterThanZero>
{
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment