Given your request, you could simply not map from Department to Employees, nor have an Employees property on your department. This would mean you always have to make a database hit to find the employees of a database.
Aplogies if these code examples don't work out of the box, I'm not near a compiler at the moment
So, your department class might look like:
public class Department
{
public int Id { get; protected set; }
public string Name { get; set; }
/* Equality and GetHashCode here */
}
and your Employee would look like:
public class Employee
{
public int Id { get; protected set; }
public Name Name { get; set; }
public Department Department { get; set; }
/* Equality and GetHashCode here */
}
Any time you wanted to find Employees for a department, you've have to call:
/*...*/
session.CreateCriteria(typeof(Employee))
.Add(Restrictions.Eq("Department", department)
.List<Employee>();
Simply because your spec says "Departments have many Employees", doesn't mean you have to map it as a bi-directional association. If you can keep your associated uni-directional, you can really get your data-access to fly too.
Google "Domain Driven Design" Aggregate, or see Page 125 of Eric Evan's book on Domain Driven Design for more information
IMO the easiest way would be having a httpmodule that manages the session (session-per-request pattern) and then using dependency injection to supply the ISession to the controllers.
This is pretty much the approach used by S#arp and others.
LINQ for NHibernate builds on top of the "standard" NHibernate, so all session management patterns still apply.
Don't have the book though so I don't know how much would have to be changed...
Best Answer
Ok from what I remember with the ISession.CreateQuery you can return a list or an enumerable. The List will load all of them into memory (I believe). The IEnumerable will issue a statement to the server to retrieve all of the Ids from the server. Every time you iterate through the ienumerable, it'll check the cache and if it's not in the cache, it'll issue a database call to retrieve the information.
Neither of them is what you want. You either use a ton of memory or hammer the DB.
I really sounds like you're trying to do some kind of ETL operation. Your best bet is to keep a couple of specialized pieces of code around to handle this situation.
If you insist on using NHibernate I would just page through the results in manageable chunks and use stateless sessions here:
http://davybrion.com/blog/2008/10/bulk-data-operations-with-nhibernates-stateless-sessions/