Skip to content

Instantly share code, notes, and snippets.

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 muratbaseren/0fd0d9309d3582fddea9ff5f01cd240b to your computer and use it in GitHub Desktop.
Save muratbaseren/0fd0d9309d3582fddea9ff5f01cd240b to your computer and use it in GitHub Desktop.
Vaughn Vernon Value Objects Console Sample
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
}
}
class Location : ValueObject
{
public double Lattitude { get; private set; }
public double Longitude { get; private set; }
public Location(double lattitude, double longitude)
{
Lattitude = lattitude;
Longitude = longitude;
}
}
class MapRoute : ValueObject
{
public Location Start { get; private set; }
public Location End { get; private set; }
public MapRoute(Location start, Location end)
{
Start = start;
End = end;
}
//protected override IEnumerable<object> GetEqualityComponents()
//{
// return new[] { Start, End };
//}
}
class MaliyetHesaplayici
{
public decimal Hesapla(MapRoute route, int aracTipi)
{
return 0;
}
}
public abstract class ValueObject
{
/// <summary>
/// When overriden in a derived class, returns all components of a value objects which constitute its identity.
/// </summary>
/// <returns>An ordered list of equality components.</returns>
protected virtual IEnumerable<object> GetEqualityComponents()
{
return GetType().GetProperties().Select(x => x.GetValue(this));
}
public override bool Equals(object obj)
{
if (object.ReferenceEquals(this, obj)) return true;
if (object.ReferenceEquals(null, obj)) return false;
if (this.GetType() != obj.GetType()) return false;
var vo = obj as ValueObject;
return GetEqualityComponents().SequenceEqual(vo.GetEqualityComponents());
}
public override int GetHashCode()
{
return HashCodeHelper.CombineHashCodes(GetEqualityComponents());
}
}
public abstract class ComparableValueObject : ValueObject, IComparable
{
protected abstract IEnumerable<IComparable> GetComparableComponents();
protected IComparable AsNonGenericComparable<T>(IComparable<T> comparable)
{
return new NonGenericComparable<T>(comparable);
}
class NonGenericComparable<T> : IComparable
{
public NonGenericComparable(IComparable<T> comparable)
{
this.comparable = comparable;
}
readonly IComparable<T> comparable;
public int CompareTo(object obj)
{
if (object.ReferenceEquals(this.comparable, obj)) return 0;
if (object.ReferenceEquals(null, obj))
throw new ArgumentNullException();
return this.comparable.CompareTo((T)obj);
}
}
protected int CompareTo(ComparableValueObject other)
{
using (var thisComponents = GetComparableComponents().GetEnumerator())
using (var otherComponents = other.GetComparableComponents().GetEnumerator())
{
while (true)
{
var x = thisComponents.MoveNext();
var y = otherComponents.MoveNext();
if (x != y)
throw new InvalidOperationException();
if (x)
{
var c = thisComponents.Current.CompareTo(otherComponents.Current);
if (c != 0)
return c;
}
else
{
break;
}
}
return 0;
}
}
public int CompareTo(object obj)
{
if (object.ReferenceEquals(this, obj)) return 0;
if (object.ReferenceEquals(null, obj)) return 1;
if (GetType() != obj.GetType())
throw new InvalidOperationException();
return CompareTo(obj as ComparableValueObject);
}
}
public abstract class ComparableValueObject<T> : ComparableValueObject, IComparable<T>
where T : ComparableValueObject<T>
{
public int CompareTo(T other)
{
return base.CompareTo(other);
}
}
internal static class HashCodeHelper
{
public static int CombineHashCodes(IEnumerable<object> objs)
{
unchecked
{
var hash = 17;
foreach (var obj in objs)
hash = hash * 23 + (obj != null ? obj.GetHashCode() : 0);
return hash;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment