Create an IUserType for a generic option class with nhibernate
-
25-12-2020 - |
Question
I'm looking for a way to create an IUserType for an option type class. Here is the option type class code:
public static class Option
{
public static Option<T> Some<T>(T value)
{
return new Option<T>(value);
}
public static Option<T> None<T>()
{
return new Option<T>();
}
}
public class Option<T>
{
public Option(T value)
{
_value = value;
_isSome = true;
}
public Option()
{
_isSome = false;
}
T _value;
bool _isSome;
public bool IsSome
{
get { return _isSome; }
}
public bool IsNone
{
get { return !_isSome; }
}
public T Value
{
get { return _value; }
}
public T ValueOrDefault(T value)
{
if (IsSome)
return Value;
return value;
}
public override bool Equals(object obj)
{
var temp = obj as Option<T>;
if (temp == null)
return false;
if (this.IsNone && temp.IsNone)
return true;
if (this.IsSome && temp.IsSome)
{
var item1 = this.Value;
var item2 = temp.Value;
return object.Equals(item1, item2);
}
return false;
}
public override int GetHashCode()
{
if (this.IsNone)
return base.GetHashCode() + 23;
return base.GetHashCode() + this.Value.GetHashCode() + 23;
}
}
It is basically just a wrapper around whatever type of T the user wants. It should end up mapping a nullable version of T. I have been unable to find any documentation on doing something like this.
Any help is appreciated.
No correct solution
OTHER TIPS
Here are some articles I have used for the basis of my IUserType classes:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow