Can I disable lazy loading per query ?

Posts   
 
    
Laurent
User
Posts: 3
Joined: 03-Oct-2013
# Posted on: 03-Oct-2013 15:49:36   

Hi,

I would like to know if i can disable lazy loading for a particular query ( using Self Servicing and Linq to LLBLGen Pro ).

The reason why i would like to do that is that I have a LLBLGen Template that create DTOs for each entity. When I construct my DTO from an entity, it will automatically populate the DTO and all embedded DTO ( triggering the lazy loading mechanism ). But if a lot of relationships exist, then i can fetch a lot of data rage

So in those cases, i would like to be able to create a query with prefetch path and disable lazy loading.

  • Can i do that ?
  • Another way to do that would be with Include/Exclude methods if those support entities. But if i am not mistaken, this is also not possible.

What i do today is the following : - I create a linq query with a prefetch Path. - Then for each embedded entites at the end of the prefetch path, i set the AlreadyFetched[...] property to true

But that is far to be optimal.

Regards, Laurent

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Oct-2013 17:33:21   

Entities fetched or pre-fetched won't be lazy-loaded. Lazy loading is for un-fetched entities.

Laurent
User
Posts: 3
Joined: 03-Oct-2013
# Posted on: 03-Oct-2013 17:54:50   

Thanks for your reply, but that does not answer my question. Let me try to give a better explanation of my issue :

Let's say we have the relationships as follow : A --> B --> C -->D |--> E --> F

That is, A have relations with B and E B have a relation with C C have relation with D E have relation with F

Now imagine for a particular query i only need A, B, C, and E I will make a query for A, with prefetch path to B, C, and E.

But then, when i will pass my entity A to the constructor of my DTO A, it will use already fetched ( without lazy loading ) B, C and E. => That is OK. It will then lazy load D and F. => I don't want that.

So my question is : Can i make a query for A, with prefetch for B, C and E, and disable all lazy loading for all others entities ?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Oct-2013 20:04:50   

No direct way for that.

If D & F are entities, then you can set entity.Fields.State = EntityState.Fetched If they are collections try to instantiate them with new empty collections.

Laurent
User
Posts: 3
Joined: 03-Oct-2013
# Posted on: 04-Oct-2013 09:25:52   

In my case, D & F are collections. That is why I set the properties AlreadyFetchedDCollection and AlreadyFetchedFCollection to true. If i have to do it for entities, i will do as you said with .Fields.State = EntityState.Fetched

But i do not like this way to do, because if my model change and new relationships are added, i will have to change many queries in my code.

That is why the possibility to disable lazyloading per query ( i.e. automatically set all alreadyFetched to true and all .Fields.State to Fetched ) or the possibility to use Include/Exclude methods on Entities and EntitiesCollection in addition to fields would be great for me.

Do you think this is something worthy to be included in a future release of LLBLGen Pro ?

Thanks, Laurent.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Oct-2013 09:45:47   

We will keep it in our list to investigate. In the mean time, and in new projects, I encourage you to use the adapter model, where you have more control, needless to say, there is no LazyLoading there.