2010-05-02 74 views
3

我有这个类:功能NHibernate映射的IList <Point>作为价值单列

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

我怎样才能在功能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这样,希望功能NHibernate将初始化Vectors类为标准ISerializable的?

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

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

回答

4

找到了答案。 :-)

在标题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 
    }); 
+0

+1 - 而且,可以为您的解决方案的代码编辑你的答案?我一直在寻找一个很好的,使用UserTypeConvention的简单例子... – 2010-05-04 18:20:01

+0

嗯,我没有我的代码,因为我最近没有时间,但我会更新一些额外的信息找到。 – Paya 2010-05-04 18:48:53

+0

已修改。如果您要尝试这些代码,请告诉我是否有任何问题,因为如果它有效,我很感兴趣。 – Paya 2010-05-04 19:03:28