To Lazy Load or not to Lazy load?
I just exchanged email with Martin Fowler about the term Lazy load. The interpretation that we on the Entity Framework team had about Lazy Loading was that on a given query we would not "eagerly" load an entire graph (i.e. load a customer, their orders, order lines and products...) but instead would, by default, retrieve a shallow version of the queried instances.
I believe this definition holds true but is incomplete, according to feedback from Martin. Per our exchange, Martin indicated that the notion of lazy load expects that one does not have to do anything beyond dereference operations to retrieve the related instances. As a result this coding pattern should work:
using (MSPetShop4Entities context = new MSPetShop4Entities())
{
var prod1 = (from p in context.Product select p).FirstOrDefault();
Console.WriteLine(prod1.Category.Name);
}
In the Entity Framework we were concerned that people using our framework would not be aware that the call prod1.Category.Name above would result in a query to the store. The result was that we require that the person be explicit about making the call to the store before doing the dereference:
In order for someone to retrieve the related instances one would call the ".Load" method on the reference or collection to indicate that one was indeed willing to execute a subsequent query to the store.
using (MSPetShop4Entities context = new MSPetShop4Entities())
{
var prod1 = (from p in context.Product select p).FirstOrDefault();
prod1.CategoryReference.Load();
Console.WriteLine(prod1.Category.Name);
}
I already mentioned in an earlier post how this can cause a leaky abstraction for people trying to abstract EF. We are looking at this pattern in V2. The interesting thing is whether the second example is lazy loading or not. We had been calling this "explicit lazy loading" but, to Martin's point it is an overload of a clear term and we should not do that. I think we will attempt to refer to this as Explicit Loading without introducing the "lazy" overload to try and be clear moving forward.