"post-prefetching"

Posts   
 
    
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 22-Apr-2006 19:25:22   

Hi,

I have the following situation. In my application, my screens are using tabcontrols, and so that databinding on a tabpage only actually happens when the tab is made active/visible. This is to postpone the DB queries as much as possible. But what if i need prefecting on a postponed query ? To use the -customer-order analogy : Suppose i have a customer screen, on which i have a tab for its orders. Somewhere i have code in my databinding masterroutine:


if (Tabcontrol.SelectedPage == OrdersTabPage)
       DataGrid.DataSource = MyCustomerEntity.Orders

At this moment, lazy loading will do it's thing and load the Orders for MyCustomerEntity. So far so good, that's the functionality i want. But in this grid i also want to display some data releated to the Order, eg. what if every order is related to a SupplierEntity, and i want to show the SupplierName in the grid. Then for each order-record in the grid the Supplierentity will be lazy loaded = not good. Is there any way to make sure the SupplierEntities are fetched along with the Orders when the Orders are lazy loaded ? I do not want to construct a seperate query and bind it's results tot he datagrids, because i need to use the MyCustomer.Orders collection. The reason for that is that later there is a routine to check (among others) if MyCustomer.Orders.ContainsDirtyItems==true and may ask to save MyCustomer if so.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 24-Apr-2006 15:30:03   

This is the typical case to drop the lazy loading and use PrefetchPaths, as explaiend in the LLBLGen Pro manual "using the generated code -> SelfServicing -> Prefetch Paths".

If you still want to use Lazy Loading then you might create a database view of all the needed fields, map it as entity, define its relation with the customer entity. But you will have to handle the inserts updates yourself, (the view is used for a Read-Only purposes).

HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 24-Apr-2006 20:02:50   

Yes, i know about the prefetching (and use that allready a lot), but in this situation i don't want it. I want the "prefetching" to happen when my subobject collection is lazy-loaded (so after the main entity is allready loaded, hence the title "post-prefetching")

I'll take a look at the view-thingy, haven't used that yet and it's allways nice to learn some new stuff ! Thnx for the hint.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 25-Apr-2006 10:31:20   

You can start a prefetch when somethng happens in the GUI, and prefetch the data into the collection. You can do that by adding the collection to a context, then fetch the collection again with the prefetch path. You'll get the data merged into the existing entities.

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 02-May-2006 12:00:56   

hmm ..it sounds like this is exactly what i need, except i don't understand it :-)

Could you clarify

You can do that by adding the collection to a context, then fetch the collection again with the prefetch path

Actually what i want to achieve is something like this :


//m_customer is a member variable of my form
m_customer  = new CustomerEntity(intId);
//..now some stuff happens

//somewhere later in the code when the user actives the "Oders" tab in the tabcontrol
//**-->>> here is the spot where i would want to prefecth the Supplier subobject for every order
gridOrders.DataSource = m_customer.Orders
//this is ok, lazy loaded ...but the gridcolums are set up in a way  so that they display the Order.SupplierName property in each row. This property is implemented in the CustomerEntity.cs class

//LLBLGen custom code block
public string SupplierName
{
   get
   {
      return this.Supplier.SupName;
   }
}

How can i prevent that when filling the grid, every Supplier will get lazy loaded to retrieve it's name. Is there a prefetch possible at the indicated spot ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 02-May-2006 21:28:18   

yes, when you fetch the orders (in a prefetch path) add to the subpath of that prefetch path the supplier fetch.

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 02-May-2006 21:52:48   

sorry for re-opening, but it's still not quite clear ...

I think you are hinting to use the prefetch in a way like this


//don' t use : gridOrders.DataSource = m_customer.Orders
//but use
OrderCollection ordercoll = new OrderCollection()
//now set the filter to only retreive those for m_customer.CustID
//then set the prefecth path to also include the Suppliers
ordercoll.GetMulti(filter,prefetchpath);
gridOrders.DataSource = ordercoll;

right ?

But with this approach i lose 1 very important aspect..when closing my form, i check to see if my m_customer is dirty, using m_customer.IsDirty and m_customer.Orders.ContainsDirtyItems ...this will no longer work i suppose, since changes by the user done in gridOrders no longer affected the m_customer.Orders objects ...

Or am i totally clueless here ?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-May-2006 07:29:06   

Don't use this line: OrderCollection ordercoll = new OrderCollection()

Instead use m_customer.Orders in the GetMulti() method, with the PrefetchPath

HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 05-May-2006 16:01:45   

omg i feel sooooooo stupid now flushed