0

When must we use eager loading in NHibernate? What is it's usage?

masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
  • 2
    The Domain-Driven Design (DDD) Aggregate Root concept can be used to help you decide when to apply eager vs. lazy loading. – JulianM Jun 07 '10 at 14:26

2 Answers2

1

One usage is when you will cache or store an object graph (in ASP.NET Cache for instance). If you don't store the whole graph, you would be missing information on a detached object. You can reattach objects of course, but that would probably be a new roundtrip to the database anyway.

If you don't eager load your collections, you would need to touch every one of the to invoke the lazy fetch. In those cases, an eager fetch is much more useful.

jishi
  • 24,126
  • 6
  • 49
  • 75
  • Thank you jishi, How can I implement eager loading in my application? – masoud ramezani Jun 07 '10 at 12:11
  • Eager loading is either defined globally on your collection (meaning, lazy="false") or at query time with ICriteria (FetchMode.Eager) – jishi Jun 07 '10 at 12:16
  • please see this : http://stackoverflow.com/questions/2987697/is-there-any-utility-or-built-in-class-in-nhibernate-that-force-a-class-to-load-i is your answer my solution? – masoud ramezani Jun 07 '10 at 12:23
  • The answer you got there is the same as I gave you. You can also use HQL to specify fetching. It is however not possible to utilize Batch-mode fetching without specifying it in your mapping, then you must depend on the first level cache and fetch data subsequently, please look up MultiCriteria/MultiQuery for that. – jishi Jun 07 '10 at 15:30
1

Maybe this presentation by Udi can help you decide.

http://www.infoq.com/presentations/Making-Roles-Explicit-Udi-Dahan

epitka
  • 17,275
  • 20
  • 88
  • 141