c# - Fluent NHibernate 映射 IList<Point> 作为单列的值

标签 c# .net nhibernate fluent-nhibernate nhibernate-mapping

我有这门课:

public class MyEntity
{
    public virtual int Id { get; set; }
    public virtual IList<Point> Vectors { get; set; }
}

如何映射 Vectors在 Fluent NHibernate 中到单个列(作为值)?我在想这个:

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类作为标准 ISerializable?

或者我还能如何映射 IList<Point>到单列?我想我将不得不自己编写序列化/反序列化例程,这不是问题,我只需要告诉 FNH 使用这些例程即可。

我想我应该使用 IUserTypeICompositeUserType ,但我不知道如何实现它们,以及如何告诉 FNH 合作。

最佳答案

找到答案。 :-)

标题 UserTypeConvention<T>在:
http://wiki.fluentnhibernate.org/Available_conventions
用于自定义类型转换。

这是为了实现自定义类型转换器:
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://geekswithblogs.net/opiesblog/archive/2006/08/13/87880.aspx
http://kozmic.pl/archive/2009/10/12/mapping-different-types-with-nhibernate-iusertype.aspx
http://blogs.msdn.com/howard_dierking/archive/2007/04/23/nhibernate-custom-mapping-types.aspx

UserTypeConvention<T>用法:
http://jagregory.com/writings/fluent-nhibernate-auto-mapping-type-conventions/

最后一个链接中最重要的代码是这样的:

public class ReplenishmentDayTypeConvention : ITypeConvention
{
  public bool CanHandle(Type type)
  {
    return type == typeof(ReplenishmentDay);
  }

  public void AlterMap(IProperty propertyMapping)
  {
    propertyMapping
      .CustomTypeIs<ReplenishmentDayUserType>()
      .TheColumnNameIs("RepOn");
  }
}

在哪里ReplenishmentDayUserTypeIUserType派生类和 ReplenishmentDay是类,应使用您的用户类型转换器进行转换。

还有这个:

autoMappings
  .WithConvention(convention =>
  {
    convention.AddTypeConvention(new ReplenishmentDayTypeConvention());
    // other conventions
  });

关于c# - Fluent NHibernate 映射 IList<Point> 作为单列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2754138/

相关文章:

c# - 使用反射从 C# 对象中去除所有值

c# - NHibernate ISession 生命周期;什么时候可以在每个存储库方法中创建/处置?

c# - 为什么通用类型定义实现的接口(interface)会丢失类型信息?

c# - "An invalid or incomplete configuration was used while creating a SessionFactory"Web 服务中的 NHibernate

nHibernate 查询继承

c# - 为什么要编译? —> int foo = foo = 5;

c# - Dapper 多选 : Nested class primary key doesn't map unless using "AS"

c# - C#中的动态方法

带有 C# DLL 的 Python CLR 模块 - AddReference 有效,导入无效

c# - 使用 CaSTLe Active Record 与 Straight NHibernate 的优缺点是什么?