nhibernateilist

NHibernate IList or List?


I've looked around Stackoverflow and seen a couple questions related to NHibernate IList vs List. I have a question of my own...

Since the purpose of IList is to have a lazyloaded list, what purpose does it serve returning from a repository. After all if you ToList() your repository call, you are creating a definitive object? So therefore regardless if your method is returning an IList or a List, there is no more lazy loading involved, correct?


Solution

  • If your repository returns an IQueryable, the results will be retrieved first time you enumerate it. If you call ToList() on it, it will retrieve the results at this moment, because ToList will enumerate the IQueryable.

    Now, lazy loading is not necessarily impacted by this behavior : suppose you have Customers and Orders. Each customer has a property named CustomerOrders, which is marked as lazy loaded. That means that when you load your list of customers, the orders are retrieved only if you try to enumerate the CustomerOrders property for each Customer. So if you just do a ToList() on your GetCustomers() method's return value, it has no effect on the lazy loading of CustomerOrders