tags:

views:

20

answers:

2

Using NHibernate, is there a quick way to map the following class:

public class Office
{
    public virtual Guid Id { get; set; }
    public virtual IList<DateTime> Holidays { get; set; }
}

...to the tables:

table Office { Guid Id }
table OfficeHolidays { Guid OfficeId, DateTime Holiday }
A: 

Unfortunately, not directly. See this question for a full discussion.

DanP
+2  A: 

Quick? I think so. Create an OfficeHoliday class and map it as one-to-many from Office, maping the collection as a private member in Office. Then expose the Holidays property and methods to maintain it.

public class Office
{
    private IList<OfficeHoliday> _officeHolidays;

    public virtual Guid Id { get; set; }

    public IEnumerable<DateTime> Holidays
    {
        get { return _officeHolidays.Select(x => x.Holiday); }
    }

    public void AddHoliday(DateTime holiday)
    {
        // should check if it already exists first...
        var newHoliday = new OfficeHoliday(this, holiday.Date);
        _officeHolidays.Add(newHoliday);
    }

    public void RemoveHoliday(DateTime holiday)
    {
        var removeHoliday = _officeHolidays.FirstOrDefault(x => x.Holiday == holiday.Date);
        if (removeHoliday != null)
        {
            _officeHolidays.Remove(removeHoliday);
        }
    }
}
Jamie Ide