locked
How about automatic relation loading? RRS feed

  • Question

  • Hello,

     

    In Entity Framework, loading the related data of a navigation property is manual, and this is by design. They can be loaded using two schemes:

    • if (!master.Details.IsLoaded) master.Details.Load();
    • Include the navigation properties to preload in the query.

    I think i understnand the motive of this strategy and i agree: Make the developer aware of the costs and protect from performance penalties hiding in automatic loads.

     

    But there is an argument. Since Entity Framework provides a model, it is natural to build methods or controllers around the generated classes. So, it is expected to have methods like:

     

    void Controller::ProcessOrder(Order order); // Order is an EF object having relations like Lines, etc. 

     

     

     

    Three related problems arise in this very common scenario:

    1. Either the method must issue if (!master.Details.IsLoaded) master.Details.Load() before touching anything it needs. This adds cross-cutting noise to the code. And, forgetting to do so, might not fail right away, but install a time bomb instead. See point 3.
    2. Either the method caller is in charge of loading and must not only provide a valid Order object, but must have also precisely "loaded" or "included" relations for use with the ProcessOrder method. This is a clear break of encapsulation and separation of concerns.
    3. In both above cases, reusability of method ProcessOrder is extremely fragile: A valid, healthy Order argument is not sufficient. Calling the method from a new code path can easily brake, because previous successful code paths eventually "loaded" or "included" navigation properties that were needed yet not loaded by the method. This omission happened in case 1 probably by error, in case 2 probably by design, in all cases not failing previously. So a succeeding method turned into a silently failing method (seeing collections of zero count) very easily.

     

    So, how about an option in ObjectContext permitting automatic loads? This would permit lean and safe controller methods that work with all scenarios while keeping the loading optimization concerns to the object factory code performing the queries.

     

    Thank you for reading these thoughts,

     

    -emmanuel.

    Tuesday, January 29, 2008 5:03 PM

Answers

  • Thanks for the feedback!

     

    Creating a way to easily opt-in to implicit lazy loading is something we've definitely talked about for a future release, but it won't happen in v1.  What you can do, though, if you need this capability is write your own classes with IPOCO or customize the code gen process to add the if !IsLoaded, Load statement to the navigation property getter.

     

    - Danny

    Wednesday, January 30, 2008 5:33 AM
  • Emmanuel,

    Thanks a lot for your insightful post.

    This is a new angle on Lazyloading that I had not heard before.

    Regards
    Alex
    Wednesday, January 30, 2008 5:34 AM
  • Wednesday, January 30, 2008 2:53 PM

All replies

  • Thanks for the feedback!

     

    Creating a way to easily opt-in to implicit lazy loading is something we've definitely talked about for a future release, but it won't happen in v1.  What you can do, though, if you need this capability is write your own classes with IPOCO or customize the code gen process to add the if !IsLoaded, Load statement to the navigation property getter.

     

    - Danny

    Wednesday, January 30, 2008 5:33 AM
  • Emmanuel,

    Thanks a lot for your insightful post.

    This is a new angle on Lazyloading that I had not heard before.

    Regards
    Alex
    Wednesday, January 30, 2008 5:34 AM
  • Danny, Alex,

     

    Thank you both for your answers. Concerning what Danny mentions, is there a standard way to customize the code gen process of the existing tools?

     

    Thank you.

     

    Wednesday, January 30, 2008 11:54 AM
  • Wednesday, January 30, 2008 2:53 PM