Quick Objects Documentation Akal Tech Logo
Loading Multiple Parent Objects In Single Call To Database

Glossary Item Box

Business Logic Framework - How To:

Loading multiple related objects using an ORM framework/code generation tool etc typically involves making multiple calls to database. In addition, you end up loading the entire set of object(s).  This not only results in wastage of prestigious resources but ends up slowing down your application.  The approach taken by the Business Logic Framework provides you the flexibility to decide what you need to load, and it can load one or more (parent) levels of related objects within a single call.

In this example, we will load the following objects and their fields with one call to the database:

The process is actually simple.

  1. Create an instance of the OrderItems class.
  2. Then call the Join method that creates a join with Products object.
  3. Then call the Join method that creates a join with the Orders object.
  4. Then call the Join method on the Orders object (available as a property on the OrderItems instance) that will create a join with the Customers object.
1 Using oi As New OrderItems() 
2     ' Since we want to include the Orders object we create a join between the OrderItems and Orders object. 
3     ' This must be done before the Load method is call in order to load the Orders object within the same call to the database. 
4     oi.Join_OrderID_Orders_Parent() 
5     ' In addition to Orders since we want to Load information about the Product, we can create join with the Products object. 
6     ' Essentially the Join method is creating a new instance of the Products class, and then adding a Join instance in the Joins collection 
7     ' of the OrderItems instance. 
8     oi.Join_ProductID_Products_Parent() 
9     ' You are not limited to one level, you can now create a join between the Orders and Customers object and hence indicate that the  
10     ' OrderItems load not only include the Orders object but also the Customers object. 
11     oi.OrderID_Orders_Parent.Join_CustomerID_Customers_Parent() 
12      
13     ' The following line allows you to customize the visibility of the fields. This gives you extreme control on what should be selected and loaded. 
14     ' Instead of loading every value of all the four objects we will load only the fields that we require. 
15     ' SetVisibleFields is a new method introduced in v3.3 and allows you to show certain fields while hiding the others. 
16     ' Fields from the current and related objects can all be included by separating them with a comma. 
17     oi.SetVisibleFields(True, oi.ItemAmount, oi.Quantity, oi.OrderID_Orders_Parent.OrderDate, oi.OrderID_Orders_Parent.OrderProcessed, oi.ProductID_Products_Parent.ProductName, _ 
18         oi.OrderID_Orders_Parent.CustomerID_Customers_Parent.FirstName, oi.OrderID_Orders_Parent.CustomerID_Customers_Parent.LastName) 
19     ' The above call to SetVisibleFields ensures that we only load the fields that we have specified. 
20      
21     ' Now lets specify a value for the primary key of the Order Items. 
22     oi.OrderItemID.Value = 2 
23      
24     ' We can check the return of the Load method as it indicates if the object was successfully loaded or not. 
25     If oi.Load() Then 
26         ' FYI: We can also check the IsLoaded property of the OrderItems or its parent objects after calling the Load method. 
27         MessageBox.Show("Is Product Loaded: " + oi.ProductID_Products_Parent.IsLoaded.ToString()) 
28         If Not oi.OrderID_Orders_Parent.CustomerID_Customers_Parent.FirstName.IsNull Then 
29             MessageBox.Show(oi.OrderID_Orders_Parent.CustomerID_Customers_Parent.FirstName.Value + " bought " + oi.Quantity.Value.ToString() + " licenses of " + oi.ProductID_Products_Parent.ProductName.Value) 
30         End If 
31     End If 
32 End Using 
33  
1 using  (OrderItems oi =  new OrderItems()) 
2
3     // Since we want to include the Orders object we create a join between the OrderItems and Orders object.  
4     // This must be done before the Load method is call in order to load the Orders object within the same call to the database.  
5     oi.Join_OrderID_Orders_Parent(); 
6     // In addition to Orders since we want to Load information about the Product, we can create join with the Products object.  
7     // Essentially the Join method is creating a new instance of the Products class, and then adding a Join instance in the Joins collection  
8     // of the OrderItems instance.  
9     oi.Join_ProductID_Products_Parent(); 
10     // You are not limited to one level, you can now create a join between the Orders and Customers object and hence indicate that the   
11     // OrderItems load not only include the Orders object but also the Customers object.  
12     oi.OrderID_Orders_Parent.Join_CustomerID_Customers_Parent(); 
13  
14     // The following line allows you to customize the visibility of the fields. This gives you extreme control on what should be selected and loaded.  
15     // Instead of loading every value of all the four objects we will load only the fields that we require.  
16     // SetVisibleFields is a new method introduced in v3.3 and allows you to show certain fields while hiding the others.  
17     // Fields from the current and related objects can all be included by separating them with a comma.  
18     oi.SetVisibleFields( true
19         oi.ItemAmount, oi.Quantity, 
20         oi.OrderID_Orders_Parent.OrderDate, oi.OrderID_Orders_Parent.OrderProcessed, 
21         oi.ProductID_Products_Parent.ProductName, 
22         oi.OrderID_Orders_Parent.CustomerID_Customers_Parent.FirstName, 
23         oi.OrderID_Orders_Parent.CustomerID_Customers_Parent.LastName 
24         ); 
25     // The above call to SetVisibleFields ensures that we only load the fields that we have specified.  
26  
27     // Now lets specify a value for the primary key of the Order Items.  
28     oi.OrderItemID.Value = 2; 
29  
30     // We can check the return of the Load method as it indicates if the object was successfully loaded or not.  
31     if (oi.Load()) 
32     { 
33          // FYI: We can also check the IsLoaded property of the OrderItems or its parent objects after calling the Load method.  
34         MessageBox.Show( "Is Product Loaded: "  + oi.ProductID_Products_Parent.IsLoaded.ToString()); 
35          if (!oi.OrderID_Orders_Parent.CustomerID_Customers_Parent.FirstName.IsNull) 
36         { 
37             MessageBox.Show(oi.OrderID_Orders_Parent.CustomerID_Customers_Parent.FirstName.Value  
38                 +  " bought "  
39                 + oi.Quantity.Value.ToString()  
40                 +  " licenses of " 
41                 + oi.ProductID_Products_Parent.ProductName.Value); 
42         } 
43     } 
44
45