Step 9 : Enhancing the OrderDetail Business Object

In this step you will enhance the OrderDetail business controller object.

Specifying Business Object Settings

  1. In the Solution Explorer, select the OrderDetail.vb file to open it in the code editing window.

  2. At the bottom of the class definition is a HookSetDefaultValues() method. This method was created by the Business Layer Generator in response to the default values that were set for the Order Details table. We will examine this method in more detail when we test the OrderDetail business object later on.

  3. In the Solution Explorer, select the OrderDetail.Partial.vb file to open it in the code editor.

  4. Add the following code in the HookConstructor() method of the OrderDetail class:

    ''' <summary>
    ''' Hook method automatically executed from the mmBusinessObject constructor
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub HookConstructor()
    	'' Place code here to be executed when the business object instantiates
    	Me.AutoSaveOnParentSaved = True
    	Me.AutoCancelOnParentCancel = True
    	Me.AutoDeleteOnParentDeleted = True
    	Me.AutoEmptyOnParentAdded = True
    	Me.ForeignParentKeyField = "OrderID"
    End Sub

    The property settings for AutoSaveOnParentSaved , AutoCancelOnParentCancel, AutoDeleteOnParentDeleted, AutoEmptyOnParentAdded, and ForeignParentKeyField go hand in hand. These settings specify behavior when establishing relationships between business controller objects.

    For example, you can create a parent/child relationship between the Order and OrderDetail controller objects as you will see later in this Jump Start. These settings indicate that the child business object should automatically save, cancel, and delete when its associated parent business object does. In addition, since a value is specified for ForeignParentKeyField, MM .NET automatically populates the child object's OrderID field with the value of the parent business object's foreign key field when saving the child business entity.

Creating OrderDetail Entity Retrieval Methods

  1. If it's not already open, go to the Solution Explorer, select the OrderDetail.Partial.vb file to open it in the code editor.

  2. According to the analysis and design we did earlier, the OrderDetail class needs one method: GetOrderDetail(). Add the following code to the bottom of the class definition after the HookConstructor() method:

    ''' <summary>
    ''' Returns all Order Detail for the specified Order ID
    ''' </summary>
    ''' <param name="orderID">Order ID</param>
    ''' <returns>Strongly typed list of OrderDetail entity objects</returns>
    Public Function GetOrderDetail(ByVal orderID As Nullable(Of Integer)) As mmBindingList(Of OrderDetailEntity)
     
    Dim orderDetailList As mmBindingList(Of OrderDetailEntity)
     
        If orderID.HasValue And orderID > 0 Then
     
    	Dim query As IQueryable(Of OrderDetailEntity) =
    	        From o In Me.ObjectContext.OrderDetailEntities
    	        Where o.OrderID = orderID
    	        Select o
     
    	orderDetailList = Me.GetEntityList(query)
     
        Else
     
    	orderDetailList = Me.GetEmptyEntityList()
     
        End If
     
        Return orderDetailList
     
    End Function

    This method accepts a single nullable integer orderID parameter which it uses in the call to the GetEntityList() method. The method returns a strongly typed list of OrderDetail entity objects for the specified order.

  3. Finally, add the following methods to the OrderDetail class:

    ''' <summary>
    ''' Parent Retrieved hook method
    ''' </summary>
    ''' <param name="bizObj">Parent business object</param>
    ''' <param name="e">EventArgs</param>
    Protected Overrides Sub HookParentRetrieved(ByVal bizObj As mmBusinessObjectByVal e As mmBusinessStateChangeEventArgs)
    	Me.GetOrderDetail(CType(e.PrimaryKeyValue, Nullable(Of Integer)))
    End Sub
     
     
    ''' <summary>
    ''' Parent Navigated hook method
    ''' </summary>
    ''' <param name="bizObj">Parent business object</param>
    ''' <param name="e">EventArgs</param>
    Protected Overrides Sub HookParentNavigated(ByVal bizObj As mmBusinessObjectByVal e As mmBusinessStateChangeEventArgs)
    	Me.GetOrderDetail(CType(e.PrimaryKeyValue, Nullable(Of Integer)))
    End Sub

    This code will be executed when you work through the Jump Start instructions for building Windows and Web applications and you register the OrderDetail business object as a child of the Orders business object.

See Also:
Step 11 : Testing the OrderDetail Business Object


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 02/11/26
Comment or report problem with topic