(just to make clear: my app isn\'t really about employees and departments. I just use these terms for example\'s sake).
Each department has an employees collection, whic
I would use a StatelessSession and batch optimization.
The session will keep track of all the loaded objects, and if we load a lot of data, it will eventually blow out with an out of memory exception. Luckily, NHibernate has a ready made solution for this, the stateless session. The code now looks like this:
using (IStatelessSession s = sessionFactory.OpenStatelessSession())
{
var books = new ActionableList(book => Console.WriteLine(book.Name));
s.CreateQuery("from Book")
.List(books);
}
The stateless session, unlike the normal NHibernate session, doesn’t keep track of loaded objects, so the code here and the data reader code are essentially the same thing.
For batch optimization and more: NHibernate performance tricks.