New Posts New Posts RSS Feed: Lazy Loading of Navigation Properties
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Lazy Loading of Navigation Properties

 Post Reply Post Reply
Author
LowOrbit View Drop Down
Newbie
Newbie


Joined: 17-Aug-2007
Location: United States
Posts: 20
Post Options Post Options   Quote LowOrbit Quote  Post ReplyReply Direct Link To This Post Topic: Lazy Loading of Navigation Properties
    Posted: 25-Sep-2012 at 7:09am
Hah , I couldn't see the forest through the trees. That was exactly what I was looking for. I had though about just creating a whole new EntityManager and didn't even think about looking for a simple Clear() command on the EM itself.
 
Thanks!!
 
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 25-Sep-2012 at 1:37am

The only easy way to do a wholesale refresh is to call EntityManager.Clear and then load the data again. Everything else involves complicated sifting through the cache and figuring out what needs to be refreshed along the lines of what your describing. E.g. setting IsLoaded to false etc.

The design pattern that helps you manage this kind of thing is the unit of work pattern. With the unit of work pattern you use multiple EntityManagers each dedicated to a particular task or workflow that the user performs. In your case, the grid would be backed by a unit of work that uses it's own EntityManager. At any point, you can call Clear on the grid's unit of work to refresh the grid without affecting other tasks or workflows in your system.
 
You can learn more about the unit of work pattern at the following link. If you don't use Cocktail it's a good idea to implement the pattern yourself as part of your framework.
 


Edited by mgood - 25-Sep-2012 at 1:37am
Back to Top
LowOrbit View Drop Down
Newbie
Newbie


Joined: 17-Aug-2007
Location: United States
Posts: 20
Post Options Post Options   Quote LowOrbit Quote  Post ReplyReply Direct Link To This Post Posted: 24-Sep-2012 at 1:53pm
Hello
 
I am looking for a way to clear the loaded state of all Navigation Properties in an EntityManager so that lazy loading works as normal when a query is being re-executed.
 
Here is an example of what I'm doing. I execute a query to get a list of OrderDetail which is bound into a GridView. The gridview has a column that displays the OrderDetail.Order.Id of each OrderDetail and also the Customer Name (OrderDetail.Order.Customer.Name) for the Order. With lazy loading turned on, the binding to OrderDetail.Order retrieves the Order Entity. And binding to Order.Customer retrieves the Customer Entity.
 
Everything so far is good. But after some period of time, I want to be able to refresh the whole grid. With lazy loading turned on, DevForce will only refetch the OrderDetail list. It never refetches the related OrderDetail or Customer Entities. I have even tried clearing the QueryCache and it still won't refetch the related entities.
 
I know there is a EntityReferenceStrategy that can be set to "Load" instead of "Lazy" but this completely negates the use of the cache (every reference to a navigation property will always go to the database even in it has already been fetched and is in the query cache). I also know you can set the IsLoaded property in the metadata for an entity's NavigationProperty but this requires me to loop through all the entities in my list to set this (and any other nested properties (i.e. Order to Customer, etc.)). Depending upon how many nested navigations you use, this is not practical.
 
So, is there an easy way to tell the EntityManager to basically forget (or reset) all navigation properties so that when referenced, they will be lazy loaded again (only once)?
 
Thanks!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down