I’m new to NHibernate -also in EntityFramework, I’m learning them. I see the Lazy keyword in both NH and EF, but I can’t understand it. How you can explain Lazy for a five years old? It seems to be important. Can you explain it in simple words please?
UPDATE:
In NHibernate 3.2 mapping by code, we have:
ManyToOne(t => t.SomeProperty, t => t.Lazy(LazyRelation.Proxy));
that LazyRelation has this static values:
public abstract class LazyRelation {
public static LazyRelation Proxy;
public static LazyRelation NoProxy;
public static LazyRelation NoLazy;
}
What does means each of them? Thanks in advanced.
Lazy loading is a technique used by ORMs (such as EF, NH and Linq2SQL) whereby related entities in a hierarchy are not retrieved immediately. Instead, related entities are only fetched if they are needed, typically when they are ‘navigated to’ or dereferenced.
e.g. In a class model, you might have a class
Person, which has aCollection(e.g.IEnumerable) ofPetswhich he / she owns.If you use lazy loading when you fetch the Person, then the related Pets collection will not be fetched at the same time. This way, database and memory resources are conserved.
However, if the Pets collection IS accessed (and assuming the Context / Session of the ORM is still available), then the ORM will go and fetch the Pets on an ‘as needed’ basis.
Compare this to Eager loading (e.g.
LoadsWithL2SQL orIncludeEF) where the caller explicitly states which of the related entities will be fetched along with the Person.See also here : Lazy loading, Deferred Loading and Eager Loading in Entity framework
Re Update
Ayende explains why you would use no-proxy lazy loading (versus proxy lazy loading) here.
NoLazy disables lazy loading (i.e. eager loaded)
You can read up more on the techniques used to implement lazy loading on Wiki.
NH’s Proxy pattern implementation is discussed here