开发者

NHibernate: Map a property of type IList<DateTime>

Using NHibernate, is there a quick way to map the foll开发者_运维技巧owing 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 }


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜