NHibernate:映射IList <datetime> </datetime>类型的属性

时间:2010-07-28 08:54:20

标签: nhibernate

使用NHibernate,可以快速映射以下类:

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

...到桌子:

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

1 个答案:

答案 0 :(得分:2)

快速?我认同。创建OfficeHoliday类并将其映射为Office中的一对多,将该集合作为Office中的私有成员进行处理。然后公开Holidays属性和方法来维护它。

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);
        }
    }
}