Can you determine if collection was prefetched?

Posts   
 
    
DarinSh
User
Posts: 9
Joined: 25-Feb-2007
# Posted on: 19-Apr-2007 23:39:05   

I would like to be able to be able to tell if a collection was prefecthed. I have an object A, which has a relation to collection B. I want to be able to look at A.B to see if it is already prefetched. In this case, I dont want A.B.Count or any other access to that collection to cause lazy loading.

I was hoping I could find this out by looking at A.Fields["B"], but fields on relations dont seem to be in the Fields collection.

Any ideas on how to determine if B was already loading via a prefetch path?

Thanks!

DarinSh
User
Posts: 9
Joined: 25-Feb-2007
# Posted on: 19-Apr-2007 23:40:49   

Sorry, forgot to include some info..

Self Servicing, v1.0.2005.1, c# 2.0

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Apr-2007 10:54:05   
DarinSh
User
Posts: 9
Joined: 25-Feb-2007
# Posted on: 20-Apr-2007 19:16:15   

Although similar, that thread does not address my specific need. Let me explain it further. I am taking Entity objects and formatting them into lighter, trimer objects to be soap serialized for in the web services layer. This formatting layer is unaware of how the data was fetched. The objects could have had child collections prefetched or not, and those child collections may have been prefetched or not. I do not want the formatting layer to make additional calls to the database while formatting, rather just formatting the objects as they were passed to that layer, and not formatting any child collections that dont already have data in memory.

So, in summary, I just need a way to see the state of a child collection (has data or not), but if my formatting layer checks the count of a child collection, the data gets fetched.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Apr-2007 05:26:12   

You can use A.GetMultiB(false), where parameter false indicate not to fetch from persistent storage if the data is already fetched.

So, you can use A.GetMultiB(false).Count.

David Elizondo | LLBLGen Support Team
Posts: 254
Joined: 16-Nov-2006
# Posted on: 21-Apr-2007 22:28:30   

I don't actually think this will work as Darin doesn't want to only get the contents of the collection if it has been fetched, so in this case the GetMulti(false) call will still perform a fetch if it hasn't been fetched, and incur the overhead of going to the database.

However if you need to determine whether a related collecton has already been fetched, the LLBLGen generated code generates private members in the EntityBase classes which indicate this e.g.

private MyApp.CollectionClasses.UserCollection  _users;
private bool    _alwaysFetchUsers, _alreadyFetchedUsers;

I think you need access to the _alreadyFetched member to achieve your goal here? As the members are public you don't have the option to overide them or even access them in your derived entity class e.g. to allow clients access to the variables.

Frans, although I can't think of much use for this perhaps we could consider adding a feature in the framework for clients to access this information via a public interface e.g. which entities have already been fetched?

The only option I could suggest here is to modify the templates which generate the code to change the private modifier to protected / public.

DarinSh
User
Posts: 9
Joined: 25-Feb-2007
# Posted on: 22-Apr-2007 04:22:32   

Thanks Matt, that is the information I was looking for. I can see that this is a bit of a unique situation, but it would be nice if there was a public property denoting which entities and collections have already been fetched. I'll see what I can do with modifying templates (haven't tried that yet.)

Thanks for the help!