代码之家  ›  专栏  ›  技术社区  ›  Paya

Fluent NHibernate将IList<Point>作为值映射到单个列

  •  3
  • Paya  · 技术社区  · 16 年前

    我有一门课:

    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; }
    }
    

    有没有可能绘制地图 像这样,希望流利的NHibernate能初始化 向量

    不然我怎么能绘制地图呢 IList<Point> 到一列?我想我必须自己编写序列化/反序列化例程,这不是问题,我只需要告诉FNH使用这些例程。

    IUserType ICompositeUserType

    1 回复  |  直到 16 年前
        1
  •  4
  •   Paya    16 年前

    找到了答案。:-)

    航向 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

    用户类型约定<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");
      }
    }
    

    在哪里? ReplenishmentDayUserType IUserType -派生类和 ReplenishmentDay

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