Step 8: Registering Business Objects with the Window

In this step you will register the Order, OrderDetail, Customer, Shipper, and Employee business objects with the Customer Orders window. These objects will be used to retrieve, manipulate, and save entities.

If the application is currently running, close it down before performing the following steps.

  1. In the Solution Explorer, right-click the CustomerOrdersWindow.xaml file and select View Code from the shortcut menu.

  2. If you are using the regular Entity Framework Business Object project, at the top of the window, add a reference to the following namespace:

    using Acme.OrderSystem.BusinessEF;
    

    If you are using the Code First Entity Framework Business Object project, add a reference to the following namespace:

    using Acme.OrderSystem.BusinessCF;
    

    This is needed to refer to the application's custom business classes.

  3. At the top of the class definition, add the following private fields that will be used to store references to business object controllers used on the window:

    public partial class CustomerOrdersWindow : mmBusinessWindow
    {
        private Order Order;
        private OrderDetail OrderDetail;
        private Customer Customer;
        private Employee Employee;
        private Shipper Shipper;

  4. In the window's constructor method, before the call to InitializeComponent, add the following code that instantiates the business objects and registers them with the window:

    public CustomerOrdersWindow()
    {
        // Instantiate and register business objects
        this.Order = (Order)this.RegisterPrimaryBizObj(new Order());
        this.Customer = (Customer)this.RegisterBizObj(new Customer());
        this.OrderDetail = (OrderDetail)this.RegisterBizObj(new OrderDetail());
        this.Order.RegisterChildBizObj(this.OrderDetail);
        this.Shipper = (Shipper)this.RegisterBizObj(new Shipper());
        this.Employee = (Employee)this.RegisterBizObj(new Employee());
     
        this.InitializeComponent();

    Notice the code that registers the OrderDetail business object as a child of the Order business object. We establish this relationship so that whenever the Order object retrieves an order, the OrderDetail object automatically retrieves the associated order detail.

    In OrderDetail.Partial.cs, there are two methods that retrieve data based on events in the parent Order business object. The HookParentRetrieved() method retrieves new order detail when the Order business object retrieves an order. The HookParentNavigated() method retrieves new order detail when the Order business object navigates to a new order. We will take advantage of this relationship in the Customer Orders window.

    The other reason for establishing a relationship between the Order and OrderDetail business objects is so the OrderDetail business object automatically saves its entities when the Order business object saves an order. Registering the OrderDetail business object as a child of the Order business object causes the save of the order detail to be wrapped in the same transaction as the save of the order header.

  5. At run time, when the user enters a character in the Auto-Complete TextBox control, we need to display a list of Customers that match the search string. To do this, double-click the Auto-Complete TextBox in design mode which creates a handler for the SearchStringChanged event. Add the following code to this event handler:

    private void actCustomerID_SearchStringChanged(object sender, SearchStringChangedEventArgs e)
    {
        // Call a method on the Customer controller object and 
        // store it in the AutoComplete TextBox's BindingList
        ((mmAutoCompleteTextBox)sender).BindingList = 
            this.Customer.GetCustomersLikeCustomerID(e.SearchString);
    }

  6. When the user selects a Customer ID at run time using the Auto-Complete TextBox control we need to retrieve all orders for the specified customer by means of the Orders business object. To add this code to the window, select the Auto-Complete TextBox in design mode, then go to the Properties Window and select the Events button (lightning bolt). Double-click the SearchStringSelected event to add a new event handler method. Add the following code to this method:

    private void actCustomerID_SearchStringSelected(object sender, SearchStringSelectedEventArgs e)
    {
        // Store the customer orders in the window's data context
        this.DataContext = this.Order.GetOrdersByCustomerID(e.SearchString);
     
        // Hide the new item row place holder in the Order Detail DataGrid
        ((IEditableCollectionView)CollectionViewSource.GetDefaultView(grdOrders.ItemsSource)).NewItemPlaceholderPosition =
            NewItemPlaceholderPosition.None;
     
        this.btnOrderDetailNew.IsEnabled = true;
    }

    This code stores the list of OrderEntity objects to the window's DataContext property. You will learn more about this property later on. It also hides the DataGrid's default new item placeholder row (this should be much easier). It also enables the Order Detail New button which you will also learn more about later.

  7. We also need to add code to the window that retrieves all Shippers and Employees for display on the Properties page. Since this code only needs to run once when the window is first instantiated, we can place it in the window's constructor after the call to InitializeComponent. The placement of this code is important, because we want it to execute after all business objects have been instantiated and registered, and after all user interface elements have been instantiated (which occurs in the InitializeComponent method).

    Add the following code to the window constructor:

    this.InitializeComponent();
     
    this.cboEmployees.ItemsSource = this.Employee.GetAllEntities();
    this.cboShippers.ItemsSource = this.Shipper.GetAllEntities();

  8. Now you need to add code to the form that stores Order Detail items for the selected Order in the Order Detail DataGrid. Before doing this, go to the top of CustomerOrdersWindow.xaml.cs and add the following using statement:

    using OakLeaf.MM.Main.Business;

    Next, go to the bottom of the CustomerOrdersWindow constructor, and enter the following code:

    After typing +=, press TAB (as mentioned in the tool tip) to create registration code for the OrderDetail business object's StateChange event. Then, press TAB a second time to create the actual handler method:

    	this.OrderDetail.StateChange += OrderDetail_StateChange;
    }

    Change the code in the new handler method to the following:

    void OrderDetail_StateChange(mmBaseBusinessObject bizObj, mmBusinessStateChangeEventArgs e)
    {
        if (e.State == mmBusinessState.Retrieved)
        {
            // Store the Order Detail in the DataGrid's data context
            this.grdOrderDetail.DataContext = this.OrderDetail.EntityList;
     
            // Hide the new item row place holder in the Orders DataGrid
            ((IEditableCollectionView)CollectionViewSource.GetDefaultView(grdOrderDetail.ItemsSource)).NewItemPlaceholderPosition =
                NewItemPlaceholderPosition.None;
        }
    }

    This code stores the OrderDetail business object's entity list to the Order Detail DataGrid's data context. It also hides the DataGrid's default new item row placeholder.

  9. Finally, you can add code to the window that stores the current customer ID into the current Order entity's CustomerID property when creating a new order. To do this, open the Customer Orders window in design mode. Go to the List tab, and double-click the btnNew button. This creates a handler method for the button's click event. Add the following code to the handler method:

    private void btnNew_Click(object sender, RoutedEventArgs e)
    {
        this.Order.Entity.CustomerID = this.actCustomerID.TextBoxContent;
    }


Now that you have registered all business objects with the window, and added the first bit of code that calls business object services, it's time to bind user interface controls to business objects.


See Also:
Step 9: Binding User Interface Elements to Business Objects


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 04/19/18
Comment or report problem with topic