locked
Lazy loading MVVM question RRS feed

  • Question

  • Lets say I have the following Entity objects:

    Customer

        List<Application> Applications

    Application

        List<ApplicationDetail> ApplicationDetails

    I have Lazy loading set to false, and in my query (this is using a DomainService) I do:

    return this.ObjectContext.Customers.Include("Applications").Include("Applications.ApplicationDetails");

    I then have the Customer entity as a notify property in my viewmodel.

    So, this works.  But not with the functionality that I am looking for really.  This eager load goes and grabs all the data right then and there.  If I turn lazy loading on, then I get the customer, but it does not get the child data, which is okay to start- but then if I bind to the child items in my view, and select the parent, I see no update.

    Is there a way to load only the information being requested from the view?  And then as you drill deeper in your view (i.e. I select a customer, I want to see a list of Applications) the data is automatically loaded?  I thought this was kind of what lazy loading was for, but the request for the Applications does not enumerate and thus query the database.  Should it? 

    What should I be doing here?

    Wednesday, October 13, 2010 6:44 PM

Answers

  • So since nobody replied to this- and I worked my way through this, I will post what I had to do as a solution.

    Bottom line?

    Entity + DomainService is smart, but not smart enough to rewrite queries on the go to pull up data as needed.  In a perfect world, there would maybe be a group of queries associated with each object built in.  If you ask for a customer and applications the query would do the join for you.  If you just want a customer, it just retrieves that customer.  And if you then wanted to get the applications for that customer it would pull those and attach them to your customer.

    But it doesn't work that way, and you need to create separate queries yourself, and determine when to call them.

    • Marked as answer by BryanCC Monday, October 18, 2010 4:15 AM
    Monday, October 18, 2010 4:15 AM

All replies

  • So since nobody replied to this- and I worked my way through this, I will post what I had to do as a solution.

    Bottom line?

    Entity + DomainService is smart, but not smart enough to rewrite queries on the go to pull up data as needed.  In a perfect world, there would maybe be a group of queries associated with each object built in.  If you ask for a customer and applications the query would do the join for you.  If you just want a customer, it just retrieves that customer.  And if you then wanted to get the applications for that customer it would pull those and attach them to your customer.

    But it doesn't work that way, and you need to create separate queries yourself, and determine when to call them.

    • Marked as answer by BryanCC Monday, October 18, 2010 4:15 AM
    Monday, October 18, 2010 4:15 AM
  • This helped me as well, thanks
    Saturday, October 29, 2011 6:29 PM