Mapping a complex component as a dictionary key

Original Post diver's Avatar

diver

25 Feb, 2010 09:45 AM via web

Is there a way to map this IDictionary<RangeOf<Tons>, double>?
Giving...

public class Tons
{
  public int Amount{get; private set;}
}

And...

public class RangeOf<T> 
{
 public T Start { get; private set; }
 public T End { get; private set; }
}

RangeOf<Tons> should be mapped as a component that is at the same time the key of the dictionary.

Thank you

  1. Support Staff 2 Posted by Hudson Akridge on 02 Mar, 2010 05:26 PM

    Hudson Akridge's Avatar

    I don't believe we support entity-key or composite-map-key in a dictionary. At least I tried this a month or so ago and couldn't get it working.

    To be honest, in NHibernate in general, this is going to cause you headaches. It's best to map the "RangeOf" class including the double field as a class and map that.

    Also, when using generics, unless something has changed, you have to explicitly map the type so you'd have to create a mapping for every implementation of the generic type.

  2. 3 Posted by diver on 02 Mar, 2010 06:17 PM

    diver's Avatar

    I might have misunderstood you, but the following is working like a charm:

    public class Berth : Entity
    {
      private IDictionary<Voyage, RangeOf<DateTime>> _occupancyList = new Dictionary<Voyage, RangeOf<DateTime>>();
      public virtual IDictionary<Voyage, RangeOf<DateTime>> OccupancyList
      {
       get { return new ReadOnlyDictionary<Voyage, RangeOf<DateTime>>(_occupancyList); }
      }
    }
    

    Voyage is an entity. Here is the mapping:

    public class BerthMap : EntityMap<Berth>
    {
    public BerthMap()
    {
      Table("Berths");
      HasMany(x => x.OccupancyList)
        .Table("Berth_occupancy_list")
        .AsEntityMap()
        .Component(cp =>
        {
          cp.Map(dr => dr.Start, "range_start");
          cp.Map(dr => dr.End, "range_end");
        })
        .Access.CamelCaseField(Prefix.Underscore)
        .Cascade.All()
        .LazyLoad();
     }
    

    As you can see, an Entity as a key and RangeOf<> as the value.
    Here is the hbm file I'm using to map a IDictionary<RangeOf, double>:

    <map name="_totalAmountFactors" table="TotalAmount_factors" access="field">
      <key column="Configuration_Id"></key>
      <composite-index class="RangeOf`1[System.Int32]">
        <key-property name="Start" column="Start_Amount"></key-property>
        <key-property name="End" column="End_Amount"></key-property>
      </composite-index>
      <element column="Factor"></element>
    </map>
    

    This "composite-index / element" is what I'm looking for. After a quick review of the source codes, I realized that it is not (yet) supported. I'll take a closer look as soon as I can.

    Thank you very much for your respond. FNH is awesome!

  3. Support Staff 4 Posted by Hudson Akridge on 02 Mar, 2010 06:24 PM

    Hudson Akridge's Avatar

    Yup, that's basically what I mean. The entity being mapped as an index/key is not supported in FNH afaik.

  4. Hudson Akridge resolved this discussion on 02 Mar, 2010 06:24 PM.

Comments are currently closed for this discussion. You can start a new one.

Recent Discussions

05 Jul, 2010 10:29 PM
05 Jul, 2010 12:45 PM
05 Jul, 2010 12:42 PM
05 Jul, 2010 12:17 PM
05 Jul, 2010 12:12 PM

 

03 Jul, 2010 12:26 AM
02 Jul, 2010 02:17 PM
02 Jul, 2010 08:18 AM
02 Jul, 2010 12:20 AM
01 Jul, 2010 10:14 PM