views:

215

answers:

1

I have this class:

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

How can I map the Vectors in Fluent NHibernate to a single column (as value)? I was thinking of this:

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

Is it possible to map the Vectors like this, hoping that Fluent NHibernate will initialize Vectors class as standard ISerializable?

Or how else could I map IList<Point> to a single column? I guess I will have to write the serialization/deserialization routines myself, which is not a problem, I just need to tell FNH to use those routines.

I guess I should use IUserType or ICompositeUserType, but I have no idea how to implement them, and how to tell FNH to cooperate.

+2  A: 

Found an answer. :-)

Heading UserTypeConvention<T> at:
http://wiki.fluentnhibernate.org/Available_conventions
for custom type conversions.

This is for implementing custom type convertors:
http://intellect.dk/post/Implementing-custom-types-in-nHibernate.aspx

Additional related links I've found:
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> usage:
http://jagregory.com/writings/fluent-nhibernate-auto-mapping-type-conventions/

The most important code in last link is this:

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

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

Where ReplenishmentDayUserType is IUserType-derived class and ReplenishmentDay is the class, which should be converted using your user type converter.

And this:

autoMappings
  .WithConvention(convention =>
  {
    convention.AddTypeConvention(new ReplenishmentDayTypeConvention());
    // other conventions
  });
Paja
+1 - Also, could you edit your answer with the code for your solution? I've been looking for a nice, simple example using UserTypeConvention...
Tom Bushell
Well I don't have my code, since I didn't had time for this lately, but I will update the answer with some additional info I've found.
Paja
Edited. If you are going to try the code, please let me know if there are any problems, as I'm interested if it works.
Paja