2
votes

I am fairly new to Fluent NHibernate. I was wondering if someone would please guide me into the right direction.

I have a class WorkDay, which represents every day of the week.

 Table("WorkDays");
 Id(x => x.ID).GeneratedBy.Identity();
 Map(x => x.Date);

I am adding a new table Holidays, which stores all the holidays for the entire year.

CREATE TABLE [dbo].[Holidays]( [ID] [smalldatetime] NOT NULL, [Description] varchar NULL)

I have created a class Holidays:

public class Holiday: Entity<DateTime>
    {
        public virtual string Description { get; set; }
    }

and mapped it:

public HolidayMap()
   {
      Table("Holidays");
      Id(x => x.ID).GeneratedBy.Assigned();
      Map(x => x.Description);
   }

What is the best way to properly Reference and map the Holidays in the WorkDay in order to check if the specific day is a Holiday?

Thank you for your suggestions.

2

2 Answers

4
votes

Option 1: if you only need to know if a day is a holyday

public virtual bool IsHolyDay { get; protected set; }

// in WorkDayMap
Map(x => x.IsHolyDay).Formula("(SELECT 1 FROM Holidays h WHERE h.Id = Date)");

Option 2: if Holydays are static don't map it at all but keep all HolyDays in memory and use

IDictionary<DateTime, HolyDay> holyDays;

HolyDay holyDay;
if (holyDays.TryGetValue(workDay.Date, out holyDay))
{
    Print(workDay.Date + " is " + holyDay.Description);
}
else
{
    Print(workDay.Date + " is no holyDay");
}

Option 3: you can if you really need to add an additional reference

// in WorkDayMap
Reference(x => x.HolyDay, "Date")
    .NotFound.Ignore()  // because not every date is a holyday
    .ReadOnly();        // you already have a property which is responsible for insertion

but that would always have to load the HolyDay because NH has to know if the HolyDay exists to null the property or create a Proxy

1
votes

Why not just make your holiday class store the date that it applies to?

public class Holiday
{
    public DateTime Date { get; set; }
    public string Description { get; set; }
    public int Id { get; set; }
}

It doesn't seem worthwhile having a separate table for 'work days'. How are you using this data?