(Fluent) Nhibernate and Lazy Loading

If I have the following:

IList<Foo> foos = fooRepository.GetAll();

:has ‘foos’ actually been filled yet? Can I then do:

foos = foos.Where(x => x.SomeProp == someValue);
foreach (Foo f in foos) …

: and expect it not to be fully executed until the foreach loop?

it depends on how GetAll is implemented… but since its returning IList<Foo> I supose its already been filled.

If you use Linq To NHibernate you can return an IQueriable<Foo>.

That’s what I was afraid of, knowing the nature of List. The SharpArch repository<t> implementation has GetAll returning IList. I may have to write my own base implementation.

Their Implementaton uses: return session.CreateCriteria<T>().List(); // returns IList<T>
Mine would have: return session.CreateCriteria<T>().Future(); // returns a defered IQueryable<T>

The reason I like this is because I use pipline methods to slim a query as conditions present themselves:

public static void WhereEnabledIs(this IQueryable<Category> query, bool enabled)
return query.Where(x => x.Enabled == enabled);

I have a static extension class, with several such pipline filters, for each entity. This way, with a defered query return from the repository, the service class can fine tune it, based on logic, before it’s executed.

Really an nHibernate question–the fluent bits just apply to classmapping, not to the ORM’s behavior. Anyhow, I believe the default is lazy loading unless you override. Personally, I’d run some tests with a SQL Server trace running to see what is hitting the wire when.