我有这门课:
public class MyEntity
{
public virtual int Id { get; set; }
public virtual IList<Point> Vectors { get; set; }
}
如何将 Fluent NHibernate 中的
Vectors
映射到单个列(作为值)?我在想这个:
public class Vectors : ISerializable
{
public IList<Point> Vectors { get; set; }
/* Here goes ISerializable implementation */
}
public class MyEntity
{
public virtual int Id { get; set; }
public virtual Vectors Vectors { get; set; }
}
是否可以像这样映射
Vectors
,希望Fluent NHibernate将Vectors
类初始化为标准ISerialized?
或者我还能如何将
IList<Point>
映射到单个列?我想我必须自己编写序列化/反序列化例程,这不是问题,我只需要告诉 FNH 使用这些例程即可。
我想我应该使用
IUserType
或ICompositeUserType
,但我不知道如何实施它们,以及如何告诉FNH合作。
找到答案。 :-)
标题
UserTypeConvention<T>
位于:这是为了实现自定义类型转换器:
http://intellect.dk/post/Implementing-custom-types-in-nHibernate.aspx
我发现的其他相关链接:
http://www.lostechies.com/blogs/rhouston/archive/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype.aspx
http://www.martinwilley.com/net/code/nhibernate/usertype.html
链接
http://kozmic.pl/archive/2009/10/12/mapping- Different-types-with-nhibernate-iusertype.aspx
链接
UserTypeConvention<T>
用法:最后一个链接中最重要的代码是这样的:
public class ReplenishmentDayTypeConvention : ITypeConvention
{
public bool CanHandle(Type type)
{
return type == typeof(ReplenishmentDay);
}
public void AlterMap(IProperty propertyMapping)
{
propertyMapping
.CustomTypeIs<ReplenishmentDayUserType>()
.TheColumnNameIs("RepOn");
}
}
其中
ReplenishmentDayUserType
是 IUserType
派生类,ReplenishmentDay
是应该使用用户类型转换器进行转换的类。
还有这个:
autoMappings
.WithConvention(convention =>
{
convention.AddTypeConvention(new ReplenishmentDayTypeConvention());
// other conventions
});