Property fetch test

Posts   
 
    
jaschag
User
Posts: 79
Joined: 19-Apr-2006
# Posted on: 13-Dec-2006 14:43:52   

using version 2 / adapter:

Is there any way to test whether a relation property has been populated yet. E.g. if you have an OrderEntity, can you test whether the related CustomerEntity or OrderLineEntities has/have been loaded (presumably via prefetch)? The reason I ask is that it would be useful to be able to do something like:

if ( !Order.PropertyHasBeenFetched("OrderLines") ) // fetch OrderLines and possibly other related entities via prefetch path

I realise that, by modifying the templates, I could test whether _orderLines (1-m) is null but the same option is not available for m-1 properties such as Order.Customer since it will be null even if a prefetch has been performed if there happens to be no related Customer.

Any suggestions?

Jascha

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 13-Dec-2006 14:56:54   

For Entities I use the following (myEntity.Fields.State == EntityState.Fetched) You may also use one of the following: Check for the PK value (Id > 0) Check the IsNew property (IsNew == false)

For Collections, you can test for the Count (Count > 0) And if the check returns true, then you might pick the first entity and use one of the above checks.

jaschag
User
Posts: 79
Joined: 19-Apr-2006
# Posted on: 13-Dec-2006 15:58:39   

Walaa wrote:

For Entities I use the following (myEntity.Fields.State == EntityState.Fetched) You may also use one of the following: Check for the PK value (Id > 0) Check the IsNew property (IsNew == false)

For Collections, you can test for the Count (Count > 0) And if the check returns true, then you might pick the first entity and use one of the above checks.

Hi Walaa,

Thanks for that.

I can see that for a m-1 I can use a combination of PK and entity reference - I guess that would not catch the situation when a PK is defined but the referenced entity is no longer in the database but that would be an exceptional situation anyway.

For 1-m relationships, if (Count==0) there is still no way to tell if a prefetch has already been done. In the absence of a flag that is set by the prefetch mechanism I guess there is no way to detect this.

Maybe this might be a feature worth adding?

Jascha

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 14-Dec-2006 10:32:27   

Why would you want to know? There isn't any data... so that thus means: there's no data. Do you want to know this because you then can save a roundtrip to the db? If that's the case, isn't that solvable by scheduling the work differently? I mean by that: - fetch data, call method, process data available, return.

Frans Bouma | Lead developer LLBLGen Pro
jaschag
User
Posts: 79
Joined: 19-Apr-2006
# Posted on: 14-Dec-2006 11:55:30   

Otis wrote:

Why would you want to know? There isn't any data... so that thus means: there's no data. Do you want to know this because you then can save a roundtrip to the db? If that's the case, isn't that solvable by scheduling the work differently? I mean by that: - fetch data, call method, process data available, return.

I would want to know because the entity graph that is initially loaded by a particular use case may not satisfy the requirements of subsequent use cases that may occur.

For example, you retrieve a list of customers to present to the user - there is no point is retrieving more than what you need to present the customer list as you do not know what the user will subsequently do. The user may then select a customer and choose to view its orders. The code that will present the orders is passed a customer entity and, ideally, would like to be able to prefetch the required order graph for that customer if it is not already populated - being modular and decoupled, it does not know the internals of the code that performed the original customer entity fetch and so does not know whether the orders were fetched or not. It would be helpful to have a test for whether that is necessary (i.e. has an attempt been made to fetch Customer.Orders?).

Thinking about it, what would be really great would be to have a method that, given an entity graph and a prefetch path, would ensure that the graph is populated to meet the prefetch path, i.e. load all the bits that are not already loaded...

EnsureGraph(RootEntity, DesiredPrefetchPath)

That way, a use case could present the specification of its data requirements and let LL satisfy them. Just an idea...

Jascha

Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 14-Dec-2006 12:30:22   

jaschag wrote:

Thinking about it, what would be really great would be to have a method that, given an entity graph and a prefetch path, would ensure that the graph is populated to meet the prefetch path, i.e. load all the bits that are not already loaded...

EnsureGraph(RootEntity, DesiredPrefetchPath)

That way, a use case could present the specification of its data requirements and let LL satisfy them. Just an idea...

Jascha

I believe that a better approach is to compare 2 PrefetchPath, and find if one is "globally" a subpath, or equal, to the other. But you must take in consideration not only the fetched entity, but event the subPath's filter.

Maybe that a particolar entity is fetched, but not in the right way for the needed elaboration.

Say, for example, that the the second elaboration need an entity graph with Order and all Order details; and the first elaboration (that loaded the original entity graph) loaded only Order and OrderDatails with total cost greater than XXX $...

It's not so easy to chek this type of thing...