Eager loading for Records

Topics: Writing modules
Feb 9, 2012 at 10:35 AM

I'm developing the Poll module for Orchard. There is a PollRecord which actually store the question.

PollAnswer <- list of answers and PollLog <- list of voted users/ips.

In most of the cases I need an answers eager loaded with poll, but not logs. Answers are lazy loaded, which is simple from development view, but awful from performance point.

How do I enable eager loading? Is it ok if I cast IQuryable back to INHibernateQueryable?

Please help, related discussion with no answer: http://orchard.codeplex.com/discussions/258230

Feb 9, 2012 at 12:43 PM

Orchard has two methods of eager loading, at least in latest 1.x code. How is your data modelled, are you using content items or plain records / IRepository?

Feb 10, 2012 at 8:29 AM

Thank you for response.

PollQuestion, PollAnswer and PollLog are plain records. I use IRepository<>.Table to load them.

Feb 10, 2012 at 8:36 AM

Ok so you can use the [Aggregate] attribute on the model property where you want them eager loaded.

Feb 10, 2012 at 11:03 AM

Thank. That's pretty much waht I wanted. But is there a way to do that during the query creation?

Oct 9, 2013 at 4:40 PM
I solved this using ing the Fetch extension method defined inside the class EagerFetchingExtensionMethods under the NHibernate.Linq namespace.
parentRepository.Table.Where([Condition]).Fetch(c => c.ChildRecord).ToList();
Full example on my blog: http://codeutil.wordpress.com/2013/10/09/orchard-eager-loading-when-using-irepository/