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.cs 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.cs 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>
    protected override void HookConstructor()
    {
    	this.AutoSaveOnParentSaved = true;
    	this.AutoCancelOnParentCancel = true;
    	this.AutoDeleteOnParentDeleted = true;
    	this.AutoEmptyOnParentAdded = true;
    	this.ForeignParentKeyField = "OrderID";
    }

    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.cs 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 mmBindingList<OrderDetailEntity> GetOrderDetail(int? orderID)
    {
    	mmBindingList<OrderDetailEntity> orderDetailList;
     
    	if (orderID != null && orderID != 0)
    	{
    		IQueryable<OrderDetailEntity> query =
    			from o in this.ObjectContext.OrderDetailEntities
    			where o.OrderID == orderID
    			select o;
    		orderDetailList = this.GetEntityList(query);
    	}
    	else
    	{
    		orderDetailList = this.GetEmptyEntityList();
    	}
     
    	return orderDetailList;
    }

    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 below the method you added in the previous step:

    /// <summary>
    /// Parent Retrieved hook method
    /// </summary>
    /// <param name="bizObj">Parent business object</param>
    /// <param name="e">EventArgs</param>
    protected override void HookParentRetrieved(mmBusinessObject bizObj, mmBusinessStateChangeEventArgs e)
    {
    	this.GetOrderDetail((int?)e.PrimaryKeyValue);
    }
     
    /// <summary>
    /// Parent Navigated hook method
    /// </summary>
    /// <param name="bizObj">Parent business object</param>
    /// <param name="e">EventArgs</param>
    protected override void HookParentNavigated(mmBusinessObject bizObj, mmBusinessStateChangeEventArgs e)
    {
    	this.GetOrderDetail((int?)e.PrimaryKeyValue);
    }

    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 10 - Testing the OrderDetail Business Object


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