tags:

views:

83

answers:

1

I have a class which contains a collection of Points (PointF's rather).

I want to be able to persist instances of that class using NHibernate.

My class looks somewhat like this (simplified):

public class MyClass
{
    public IDictionary<string, PointF> Points = new Dictionary<string, PointF>();

    public void AddPoint( location, PointF position )
    {
        Points.Add(location, position);
    }
}

The mapping of this collection looks like this (simplified):

<map name="Points" table="Locations">
   <key column="MyClassId" />
   <index column="LocationName" />
   <composite-element class="System.Drawing.PointF, System.Drawing">
      <property name="X" column="X" />
      <property name="Y" column="Y" />
   </composite-element>
</map>

The problem now is, that NHibernate throws an error while processing the mapping file, since PointF is not a known (mapped) entity.

How can I solve this in the most simple way ? How can I make sure that NHibernate is able to persist my collection of locations (with their coordinates (point) ?

+1  A: 

The problem is not that you didn't map the type PointF - because you map it as composite-element, which is correct.

When mapping such types you need to make sure

  • that properties are writable (which is luckily the case here)
  • that it has a default constructor, which is not the case here.

So how should NH create new instances when there is not default constructor? It can't.

Your options are:

  • implement an interceptor or NH event. I think it is possible to inject code there which creates instances of certain types, but I don't know how.
  • implement a NH user type (derived from ICompositeUserType), which is not too hard to do
  • map another type (eg. a wrapper to PointF)
Stefan Steinegger
Actually, a PointF is a structure and thus, it should have a default constructor ...In the meantime, i've solved it by creating another type which kinda wraps PointF. Thx for your input anyway. :)
Frederik Gheysels