Thursday, June 19, 2008

Eagerly load two or more collections

I'll be verbose on it:

this link has exactly what i needed for.

As a proof of concept I wrote this unit test to check that collections are loaded eagerly. Domain model is a client has a collection of prders. An Order has a collection of bills


Client client = null;
using (new NHibernateSession())
{
ISession session = NHibernateSession.GetISession(typeof(Client));

client = session.CreateCriteria(typeof(Client))
.SetFetchMode("Orders", FetchMode.Join)
.UniqueResult();

session.CreateCriteria(typeof(Client))
.CreateCriteria("Orders")
.SetFetchMode("Bills", FetchMode.Join)
.List();
}

// Assert that orders and order.Bills are eagerly laoded
foreach (RisingStar.BLL.Model.Order order in client.Orders)
{
order.Bills.Count.ToString()
}