Business Logic Framework - Tutorial 7:
Our seventh tutorial shows you how to run searches on related tables. The objective is to allow the user to specify an amount and the business object will find all the orders that contain a item that is priced higher than the specified amount. Essentially, if the user specifies 69.99 then any order that contains an item priced higher than that will be included in the result set. We however do not want to include duplicate records for an order in case an order has more than one item priced higher than the specified amount.
Here is the C# code that is used to achieve the desired results:
| if (this.TextBox_MinimumItemAmount.Text.Trim().Length > 0) | |
| { | |
| using (Orders orders = new Orders()) | |
| { | |
| // Setting the ObjectMode on the customer object to ObjectModes.Search | |
| // tells the customer object to change its field's UseInSearch property to true | |
| // when a value is assigned to the field's value property. | |
| orders.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Search; | |
| orders.Join_OrderID_OrderItems_Child(); | |
| // since we don't want to see any fields from OrderItems object, we set the Visible property | |
| // of the OrderID_OrderItems_Child object to false. | |
| orders.OrderID_OrderItems_Child.Visible = false; | |
| // By using the Parse method we are assigning the value specified by the user in TextBox_MinimumItemAmount | |
| // to the ItemAmount property of the OrderID_OrderItems_Child object. | |
| // Since the ObjectMode of the Orders object was set to ObjectModes.Search and the OrderID_OrderItems_Child was joined afterwards | |
| // the OrderID_OrderItems_Child's ObjectMode was also set to ObjectModes.Search at the time of Join creation between the two objects. | |
| orders.OrderID_OrderItems_Child.ItemAmount.Parse(this.TextBox_MinimumItemAmount.Text); | |
| // We have great amount of control over our search criteria. One very good example is the ability to | |
| // modify the Operator of any field so that the business object uses this operator instead of using the default equality operator. | |
| orders.OrderID_OrderItems_Child.ItemAmount.Operator = " > "; | |
| // OnlyDistinct property is very useful if you want to only return rows that are unique. | |
| // This is equivalent to specifying a DISTINCT clause in the SQL statement. | |
| orders.OnlyDistinct = true; | |
| orders.Find(); | |
| if (orders.AffectedRecords > 0) | |
| { | |
| this.GridView.DataSource = orders.ResultSet; | |
| this.GridView.DataMember = orders.GetResultSetName(); | |
| } | |
| else | |
| { | |
| this.GridView.DataSource = ""; | |
| } | |
| } | |
| } | |
| If Me.TextBox_MinimumItemAmount.Text.Trim().Length > 0 Then | |
| Using orders As Orders = New Orders() | |
| orders.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Search | |
| orders.Join_OrderID_OrderItems_Child() | |
| ' since we don't want to see any fields from OrderItems object, we set the Visible property | |
| ' of the OrderID_OrderItems_Child object to false. | |
| orders.OrderID_OrderItems_Child.Visible = False | |
| ' By using the Parse method we are assigning the value specified by the user in TextBox_MinimumItemAmount | |
| ' to the ItemAmount property of the OrderID_OrderItems_Child object. | |
| ' Since the ObjectMode of the Orders object was set to ObjectModes.Search and the OrderID_OrderItems_Child was joined afterwards | |
| ' the OrderID_OrderItems_Child's ObjectMode was also set to ObjectModes.Search at the time of Join creation between the two objects. | |
| orders.OrderID_OrderItems_Child.ItemAmount.Parse(Me.TextBox_MinimumItemAmount.Text) | |
| ' We have great amount of control over our search criteria. One very good example is the ability to | |
| ' modify the Operator of any field so that the business object uses this operator instead of using the default equality operator. | |
| orders.OrderID_OrderItems_Child.ItemAmount.Operator = " > " | |
| ' OnlyDistinct property is very useful if you want to only return rows that are unique. | |
| ' This is equivalent to specifying a DISTINCT clause in the SQL statement. | |
| orders.OnlyDistinct = True | |
| orders.Find() | |
| If orders.AffectedRecords > 0 Then | |
| Me.GridView.DataSource = orders.ResultSet | |
| Me.GridView.DataMember = orders.GetResultSetName() | |
| Else | |
| Me.GridView.DataSource = "" | |
| End If | |
| End Using | |
| End If | |

Click to enlarge