Disable lazy loading or lazy loading with filter?

Posts   
 
    
pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 27-Mar-2006 13:37:00   

Hi,

I am using the self servicing template and would like to loop through a collection to get each "row" as well as each related value IF there is any (while using a filter). For example I would like all fields + the value of the metadata if it has RecordID = x. Unfortunately lazy loading gets all rows in the metadata collection without considering the filter. Is there any way to get around this? E.g. to get a collection based on a filter and prevent lazy loading from getting rows which don't fit into this filter?

Thanks very much Patrick


Sample


Relationship
------------
Field.FieldId - 1:m - MetaData.FieldId / MetaData.RecordId - m:1 - Record.RecordId

TABLES
--------
Field
------
FieldId PK
FieldName 
etc....

MetaData
--------
FieldId FK
RecordId FK
Value
....

Record
-------
RecordId PK
RecordName 
etc....


Dim filter As New PredicateExpression(MetaDataFields.RecordId = RecordId Or MetaDataFields.RecordId = DBNull.Value)

Dim prefetch As IPrefetchPath = New PrefetchPath(EntityType.FieldEntity)
prefetch.Add(FieldEntity.PrefetchPathMetaData)

Dim relation As New RelationCollection()
relation.Add(FieldEntity.Relations.MetaDataEntityUsingFieldId, JoinHint.Left).CustomFilter = filter

Dim fieldCol As New CollectionClasses.FieldCollection
fieldCol.GetMulti(filter, 0, Nothing, relation, prefetch)

For Each fieldEnt As EntityClasses.FieldEntity In fieldCol
    Dim label As String = fieldEnt.ExternalFieldLabelId
    Dim value As String = String.Empty
    If fieldEnt.MetaData.Count > 0 Then value = fieldEnt.MetaData(0).Value
    ' Add values to custom collection
    dic.Add(label, value)
Next

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 27-Mar-2006 15:55:03   

The following is copied from "Using the generated code -> SelfServicing -> Prefetch Paths" section found in the LLBLGen Pro documentation manual:

"Once data is fetched through a Prefetch Path, load-on-demand for that related entity or entity collection is switched off, unless you set the always fetch flag for that particular related entity or set of related entities to true."

pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 28-Mar-2006 08:21:08   

Thanks your hint pointed in the right direction. I changed the code and it is working now:


            Dim filter As New PredicateExpression(MetaDataFields.RecordId = RecordId Or MetaDataFields.RecordId = DBNull.Value)

            Dim prefetch As IPrefetchPath = New PrefetchPath(EntityType.FieldEntity)
            prefetch.Add(FieldEntity.PrefetchPathMetaData, 0, filter)

            Dim fieldCol As New CollectionClasses.FieldCollection
            fieldCol.GetMulti(Nothing, prefetch)


The filter had to be part of the prefetch path and no relationship was needed: prefetch.Add(FieldEntity.PrefetchPathMetaData, 0, filter)

I am very glad it is working now!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 28-Mar-2006 08:37:02   

I am very glad it is working now!

That's always a nice thing to hear simple_smile

Good Luck

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 28-Aug-2006 13:16:17   

Using a prefetch path is not lazy loading, is it? If I want to use a filter AND lazy loading, what do I have to do? Is it possible to set a filter for lazy loading?

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 28-Aug-2006 17:59:55   

Hi,

lazy loading results in "GetMulti" queries being automatically generated and run with filters for the corresponding foreign keys. Unfortunately, there is no way to customize those filter beforehand, but I think this is also because lazy loading assumes you want to access the db records transparently, thus that if you do myEntity.SomeCollection you want the complete collection relating to your base entity.

Now if you want to change this, most properties are overridable, so you may get the expected behaviour in some inherited entity class.

The related entities properties call the corresponding getmultixxx... methods, which are involved in building the filter. So you may have an override of such a method augment your filter with some additional predicate.

Hope that helps

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 29-Aug-2006 12:04:50   

Thanks for your answer, it was very helpful. I had to assure myself that i understood the concept correctly.

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 01-Sep-2006 13:15:59   

Hm, I tried to implement that functionality. Seems i have to inherit every Entity class, collection class, dao class... much to do.

I'm sure there's a reason why this isn't implemented by the LLBL designers. What is this? (I don't want to spend time for the enhancement and in the end, it's not possible simple_smile )

And yes you are right, most of the time one wants to get the whole collection of sub entities. But in binding scenarios a filter feature would be really great!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Sep-2006 15:09:01   

instead of having your modification implemented in classes, you may have them applied in the templates that generate the code.

You should just alter a template, namely the entityInclude.template. Create a copy of the original, create a new templatebindings file, bring it to the top of the list at tab 2 so it overrules the original binding.