[英]Create an IUserType for a generic option class with nhibernate
I'm looking for a way to create an IUserType for an option type class.我正在寻找一种为选项类型 class 创建 IUserType 的方法。 Here is the option type class code:这是选项类型 class 代码:
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.它基本上只是用户想要的任何类型的 T 的包装器。 It should end up mapping a nullable version of T. I have been unable to find any documentation on doing something like this.它最终应该映射一个可以为空的 T 版本。我一直无法找到任何有关执行此类操作的文档。
Any help is appreciated.任何帮助表示赞赏。
Here are some articles I have used for the basis of my IUserType classes:以下是我用于 IUserType 类基础的一些文章:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.