Filtering and Loading, (continued)

Posts   
 
    
craigmain
User
Posts: 22
Joined: 17-Apr-2005
# Posted on: 18-Apr-2005 12:09:00   

Hi Guys,

I am attempting here to consolidate my last two questions. I have now build a mechanism to lazy load the collections - I have two choices here.

I am using a many to many relationship m:n with group -> vehiclegroupvehicle <- vehicle. I am using the adapter pattern. I wish to load the vehicles for a particular group. This is accomplished by loading the groups into a list. I do not load all the vehicles at this time (it is expensive getting them all).

The user clicks to view the vehicles in a particular group. I either use a separate list of vehicles (I think the best way of doing this), or I load into group.Vehicle (The associated collection).

The trick I need to achieve here, is to now keep the relationship working on the client. I need to cache the vehicles. Can I just copy all the entities into a separate list, and then retreive them into the list if the client re-visits a group. I dont' want to fetch the data from the server each time the user views the vehicles in a group. I basically want a cache.

I am happy to use separate entity collections for the groups and vehicles and manually copy the correct ones to the bound list as the user requests them. I am also happy to use the group.Vehicle 'built in list' which is also acceptable. I just need to load it from the cache rather than from the db when a group is re-visited.

What suggestions do you guys have. If I just add all the vehicles into a cache list, and when the user re-visits a group I copy the correct ones from the cache list. Will this work properly? Will the dirty state be maintained?

Regards Craig

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Apr-2005 17:41:56   

craigmain wrote:

I am attempting here to consolidate my last two questions. I have now build a mechanism to lazy load the collections - I have two choices here.

I am using a many to many relationship m:n with group -> vehiclegroupvehicle <- vehicle. I am using the adapter pattern. I wish to load the vehicles for a particular group. This is accomplished by loading the groups into a list. I do not load all the vehicles at this time (it is expensive getting them all).

The user clicks to view the vehicles in a particular group. I either use a separate list of vehicles (I think the best way of doing this), or I load into group.Vehicle (The associated collection).

Keep in mind that an m:n relation is readonly.

The trick I need to achieve here, is to now keep the relationship working on the client. I need to cache the vehicles. Can I just copy all the entities into a separate list, and then retreive them into the list if the client re-visits a group. I dont' want to fetch the data from the server each time the user views the vehicles in a group. I basically want a cache.

Please use the Vehicle - VehicleGroup - Group chain.

For a group to show, you check if you've that group in your cache already (for example a hashtable, with the PK for group as the key and the group as the value). If so, simply pick that group and show it. If not, load the group from the server, with vehicle group and vehicle entities.

Or is this too much? I.e.: all vehicles in a group, is that too much?

I am happy to use separate entity collections for the groups and vehicles and manually copy the correct ones to the bound list as the user requests them. I am also happy to use the group.Vehicle 'built in list' which is also acceptable. I just need to load it from the cache rather than from the db when a group is re-visited.

What suggestions do you guys have. If I just add all the vehicles into a cache list, and when the user re-visits a group I copy the correct ones from the cache list. Will this work properly? Will the dirty state be maintained?

You can work with 2 hashtables as well: one for groups and one for vehicles. When you load a group, you load the group entity together with the vehiclegroup entities. At the client, you them manually assign the vehicle entities to the vehiclegroup entities loaded.

I see you want to change entities as well. Could you give general overview of that scenario? I ask this because if you create a large graph with entities (groups and vehicles) and you then send that to the server for persistence, it might be very slow.

Frans Bouma | Lead developer LLBLGen Pro
craigmain
User
Posts: 22
Joined: 17-Apr-2005
# Posted on: 18-Apr-2005 18:09:08   

Hi,

I have made enormous progress, and am now rather close to purchasing. I have got the collections to lazy load as required.

I have a collection of groups. When the user wants the vehicles for a group, I load all the matching vehicles (m:n) and all the vgv (VehicleGroupVehicle intermediate class) into their matching collections in the current group.

I only load these as the user clicks, so that i am only obtaining the m:n collection as required (lazy loading).

I now have a simple problem. The vehicles collection (m:n) is read only. What do I do about this? If I add or remove vehicles from a group, how can I modify this collection appropriately. It does not seem that I am able to do this, even in code.

How do I manage editing this m:n collection, is there a strategy for this. One other question I had, is can I serialize the dirtyentities accross to the db layer, and have that commit just the dirty ones. I assume I can add them to an entity list, and commit them as long as I do it in the right order, is that correct?

Regards Craig

craigmain
User
Posts: 22
Joined: 17-Apr-2005
# Posted on: 18-Apr-2005 18:40:43   

If I want to filter the m:n collection on the client side, is there any more efficient way than iterating through all the records of the middle table, checking for matches, and then searching the target collection for the associated entity.

Is there any filtering functionality on the client side at all?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 19-Apr-2005 10:48:08   

craigmain wrote:

I have a collection of groups. When the user wants the vehicles for a group, I load all the matching vehicles (m:n) and all the vgv (VehicleGroupVehicle intermediate class) into their matching collections in the current group.

I only load these as the user clicks, so that i am only obtaining the m:n collection as required (lazy loading).

I now have a simple problem. The vehicles collection (m:n) is read only. What do I do about this?

I tried to explain that to you in my previous post. I hope that one was clear enough, if not please let me know. Basicly, it comes down to: the m:n relation is a 'view' on a 3-entity relation, and should only be used for viewing purposes. If you want to manipulate that relation, you have to fetch the intermediate entities (vgv) and related to them the vehicle objects.

If I add or remove vehicles from a group, how can I modify this collection appropriately. It does not seem that I am able to do this, even in code.

m:n relations are controlled in the database, as 2 entities are related in an m:n way indirectly via another entity. So manipulating that entity (via another thread/user) can destroy the relation between two entities, the m:n relation is readonly.

How do I manage editing this m:n collection, is there a strategy for this.

If you want to add a vehicle to a group, do:

VehicleGroupVehicleEntity vgv = new VehicleGroupVehicleEntity(); vgv.Vehicle = myVehicle; vgv.Group = myGroup;

then, the vehicle is added to the group, via the intermediate entity. Saving the vehicle entity will then also save the intermediate entity.

One other question I had, is can I serialize the dirtyentities accross to the db layer, and have that commit just the dirty ones. I assume I can add them to an entity list, and commit them as long as I do it in the right order, is that correct?

Saving entities is done in the right order by the framework. You just change values, assign entities to other entities and save an entity recursively, which will save the complete graph of dirty entities in teh right order. It doesn't matter where you start, top or bottom, the code will figure that out simple_smile

Frans Bouma | Lead developer LLBLGen Pro