Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced Factory with new TThis(); #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 86 additions & 98 deletions ValueOf/ValueOf.cs
Original file line number Diff line number Diff line change
@@ -1,109 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace ValueOf
{
public class ValueOf<TValue, TThis> where TThis : ValueOf<TValue, TThis>, new()
{
private static readonly Func<TThis> Factory;

/// <summary>
/// WARNING - THIS FEATURE IS EXPERIMENTAL. I may change it to do
/// validation in a different way.
/// Right now, override this method, and throw any exceptions you need to.
/// Access this.Value to check the value
/// </summary>
protected virtual void Validate()
{
}

using System.Collections.Generic;

namespace ValueOf
{
public class ValueOf<TValue, TThis> where TThis : ValueOf<TValue, TThis>, new()
{
/// <summary>
/// WARNING - THIS FEATURE IS EXPERIMENTAL. I may change it to do
/// validation in a different way.
/// Right now, override this method, and throw any exceptions you need to.
/// Access this.Value to check the value
/// </summary>
protected virtual void Validate()
{
}

protected virtual bool TryValidate()
{
return true;
}

static ValueOf()
{
ConstructorInfo ctor = typeof(TThis)
.GetTypeInfo()
.DeclaredConstructors
.First();

var argsExp = new Expression[0];
NewExpression newExp = Expression.New(ctor, argsExp);
LambdaExpression lambda = Expression.Lambda(typeof(Func<TThis>), newExp);

Factory = (Func<TThis>)lambda.Compile();
}

public TValue Value { get; protected set; }

public static TThis From(TValue item)
{
TThis x = Factory();
x.Value = item;
x.Validate();

return x;
}

}

static ValueOf()
{
}

public TValue Value { get; protected set; }

public static TThis From(TValue item)
{
TThis x = new TThis()
{
Value = item
};
x.Validate();

return x;
}

public static bool TryFrom(TValue item, out TThis thisValue)
{
TThis x = Factory();
x.Value = item;
TThis x = new TThis()
{
Value = item
};

thisValue = x.TryValidate()
? x
: null;

return thisValue != null;
}

protected virtual bool Equals(ValueOf<TValue, TThis> other)
{
return EqualityComparer<TValue>.Default.Equals(Value, other.Value);
}

public override bool Equals(object obj)
{
if (obj is null)
return false;

if (ReferenceEquals(this, obj))
return true;

return obj.GetType() == GetType() && Equals((ValueOf<TValue, TThis>)obj);
}

public override int GetHashCode()
{
return EqualityComparer<TValue>.Default.GetHashCode(Value);
}

public static bool operator ==(ValueOf<TValue, TThis> a, ValueOf<TValue, TThis> b)
{
if (a is null && b is null)
return true;

if (a is null || b is null)
return false;

return a.Equals(b);
}

public static bool operator !=(ValueOf<TValue, TThis> a, ValueOf<TValue, TThis> b)
{
return !(a == b);
}

// Implicit operator removed. See issue #14.

public override string ToString()
{
return Value.ToString();
}
}
}
}
protected virtual bool Equals(ValueOf<TValue, TThis> other)
{
return EqualityComparer<TValue>.Default.Equals(Value, other.Value);
}
public override bool Equals(object obj)
{
if (obj is null)
return false;
if (ReferenceEquals(this, obj))
return true;
return obj.GetType() == GetType() && Equals((ValueOf<TValue, TThis>)obj);
}
public override int GetHashCode()
{
return EqualityComparer<TValue>.Default.GetHashCode(Value);
}
public static bool operator ==(ValueOf<TValue, TThis> a, ValueOf<TValue, TThis> b)
{
if (a is null && b is null)
return true;
if (a is null || b is null)
return false;
return a.Equals(b);
}
public static bool operator !=(ValueOf<TValue, TThis> a, ValueOf<TValue, TThis> b)
{
return !(a == b);
}
// Implicit operator removed. See issue #14.
public override string ToString()
{
return Value.ToString();
}
}
}