Fluent NHibernate - map a list of Enums using a custom mapping

400 Views Asked by At

In our solution, when mapping Enums, instead of simply saving the integer or text description, a custom IUserType is used to resolve the text description against a database table and retrieve the appropriate ID. (These values are cached at application start-up to speed things along)

This has been working really well for single Enum properties. The mapping code (using mapping overrides) looks something like this....

POCO:

  public class Connection
  {
      // Other properties omitted
      public Protocol Protocol { get; set; }
  }

Mapping Override:

  mapping.Map(map => map.Protocol, "ProtocolID")
         .CustomType<GenericEnumUserType<Protocol>>();

My problem occurs when I have a list of Enums to map. For example...

POCO:

  public class Credential
  {
      // Other properties omitted
      public ICollection<Protocol> Protocols { get; set; }
  }

My research (here and here) suggests the mapping should be similar to the following:

  mapping.HasMany(x => x.Protocols)
         .Table("TABLE")
         .KeyColumn("COLUMN")
         .Component(component =>
                   {
                           //MAP YOUR CUSTOM TYPE HERE
                   })
         .Cascade.None()
         .KeyNullable()
         .Not.LazyLoad();

... but I can't figure out how to wire up the CustomType in the MAP YOUR CUSTOM TYPE HERE section.

On the other hand, this SO thread suggests CustomType is not available for collections - however that particular comment is dated 2009 so I'm hoping something has changed since then.

Should this type of mapping be possible (and if so what am I missing) or do I need to start looking for the most appropriate work-around?

Cheers.

0

There are 0 best solutions below