New Posts New Posts RSS Feed: Lazy loading, how does it work?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Lazy loading, how does it work?

 Post Reply Post Reply
Author
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 146
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post Topic: Lazy loading, how does it work?
    Posted: 17-Oct-2012 at 1:01pm
Some times I see it working as expected(it is in other places) and some times when I run profiler I see data being fetched when there is no need for it.
 
I have table Document and table File. File contains binary data and I don't want it to load unless I need this data.
 
SYSDocument.PropertyMetadata.SYSFile.ReferenceStrategy = new EntityReferenceStrategy(EntityReferenceLoadStrategy.DoNotLoad, MergeStrategy.OverwriteChanges);
After placing code above it works (entity not loaded) and no errors which proves me that my code doesn't need "File" just yet. No bindings, nothing needs it.
 
Question is, what triggers lazy loading and how I can see which line of code caused lazy loading to happen? I like the feature but also want to be able to contol it. Or at least understand why DevForce decides to load it.
 
 
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 17-Oct-2012 at 5:53pm
Lazy loading is triggered when a navigation property is accessed and it hasn't already been loaded, or the load strategy indicates it should always be loaded from the data source. 
 
You might be seeing lazy loading occur for data loaded into a grid, particularly when columns are auto-generated.  For example, if displaying Order information and using auto-generated columns, the OrderDetails for each order will be lazily loaded.
 
As you've noted, you can use the ReferenceStrategy to change the loading behavior for navigation properties.  Here's more information - http://drc.ideablade.com/xwiki/bin/view/Documentation/navigation-properties-data-retrieval.
Back to Top
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 146
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post Posted: 17-Oct-2012 at 6:15pm
I don't have anything like that (no data grid) and nothing breaks if I specify reference strategy.
 
Is there any way for me to trace who is accessing this entity to see why it's doing it? I like lazy loading behavior when I need entity but in this case it doesn't seem to be needed..
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 17-Oct-2012 at 6:27pm
You can set a breakpoint on the navigation property's getter, and when the break point is hit check the stack.
Back to Top
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 146
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post Posted: 18-Oct-2012 at 4:38pm
Simple :) I found it. It was my "Is Dirty" check on parent entity where I was checking ParentEntity.EntityInQuestions.EntityAspect...
Back to Top
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 146
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post Posted: 19-Oct-2012 at 4:02pm
Follow up question on dealing with Lazy loaded entities. I figured that DevForce knows how to fetch it when data bound.
 
How do I programmaticaly fetch it? In my case I have SYSDocument which describes document properties. It's child entity is SYSFile - this is table where I store blobs.
 
User wants to download file and I need to bring this SYSFile to the client. If I just access SYSFile.Data property in code than I get empty byte array (byte[] Data), it's not lazy loaded.
 
I tried to run code like this but it didn't work either:
var query = documentPlacement.SYSDocument.SYSFile.ToQuery();
            query.ExecuteAsync(
                op =>
                    {
                        if (op.HasError)
                        {
                            var aa = op.Error;
                        }
                        var a = op.Results;
                        var b = a;
                    });
 
op.Results is empty. I understand that I can write query to fetch it myself. But how do I know that I need to fetch it? SYSDocument.SYSFile is always present (not NULL). How to programmaticaly determine if this entity loaded?
 
 
 
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 19-Oct-2012 at 5:08pm
By definition, a "lazily loaded" navigation property is one which will be retrieved upon the first call to the property getter.  This happens regardless of any data binding in place; for example, the first call to anOrder.OrderDetails will retrieve the OrderDetails for the order.
 
If you haven't changed the reference strategy, the call to SysDocument.SysFile will lazily load the SysFile.  If you've set the reference load strategy to DoNotLoad, which you did in an earlier post, then the SysFile won't be retrieved.   DevForce scalar navigation properties don't return null, they return a null entity if no entity exists.   More here - http://drc.ideablade.com/xwiki/bin/view/Documentation/null-entity.
 
Also remember that if you're working in Silverlight all lazy loads are asynchronous.  When called, these properties return a pending entity or pending entity list.
 
 
Back to Top
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 146
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post Posted: 19-Oct-2012 at 5:24pm
Yes, null-entity is what I needed to know. Otherwise - I don't see a way to "wait" programmaticaly (I'm sure this way exists) for entities loaded when I access them.
 
In my scenario - I ended up disabling lazy-loading (as in first post) and now I load entity on demand controlling async behavior.. 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down