views:

66

answers:

1

I have the following entity

public class Employee
{
   public virtual int Id {get;set;}
   public virtual ISet<Hour> XboxBreakHours{get;set}
   public virtual ISet<Hour> CoffeeBreakHours {get;set}
}

public class Hour
{
   public DateTime Time {get;set;}
}

(What I want to do here is store information that employee A plays Xbox everyday let's say at 9:00 13:30 and has a coffee break everyday at 7:00 12:30 18:00) - I am not sure if my approach is valid at all here.

The question is how should my (ideally fluent) mappings look like here? It is not necessary (from my point of view) for Hour class to have Id or be accessible from some kind of repository.

+1  A: 

Depending on how you want to do it, you either need to map your collection as an element mapping or as a component collection (that's <element> and <composite-element> in NHibernate terms). The former will need an IUserType defining, while the latter is for if you're going to have your Hour class have more than one property.

If you're sticking with a single property, you'll need to define an IUserType so NHibernate knows how to translate to and from your type to your database. Once you've done that, you can map it with Fluent NHibernate like so:

HasMany(x => x.XboxBreakHours)
  .Element("value", x => x.CustomType<YourUserType));

That specifies that your collection is stored in a table with a column called value containing the actual values. The CustomType call is what tells NHibernate to use the IUserType for this collection.

If you're going to have multiple properties in your Hour class, then you need to do the following (note: this is actually very similar to doing a Component mapping).

HasMany(x => x.XboxBreakHours)
  .Component(comp =>
  {
    comp.Map(x => x.Time);
    comp.Map(x => x.Another);
  });
James Gregory