Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

September 8, 2009

Understanding Object Binding

Filed under: C#,Data Binding,OOP,VB.NET,Visual Studio @ 2:26 pm

Before going through the details of how to use object binding, it is important to understand exactly what it is—and what it is not. Object binding is binding your business object properties to user interface elements. Object binding is not database binding in the strict sense of the term. It does not directly collect or bind any data from your database.

When you are using business object classes without object binding, the flow of data from the database to your user interface and back again requires these steps:

1. The business object calls the data access component to get the data from the database and sets the business object properties using that data.

For example, the Product class calls the data access component, which uses a query or stored procedure to fill a DataTable from the Product table. The data access component returns the DataTable to the Product class, which assigns each field from the table to a property of the object. To illustrate, the line of code required to get the ProductName field from the DataTable and set the
ProductName property is as follows:

In C#:

myProduct.ProductName = dt.Rows[0]["ProductName"];

In VB:

myProduct.ProductName = dt.Rows(0).Item("ProductName")

2. The user interface component accesses the business object properties to fill the values of the controls on the form.

For example, each control on the ProductWin form is assigned to the value of the appropriate Product business object property. To illustrate, the line of code required to set the Text property of the Name TextBox control to the ProductName property of the Product business object is as follows:

In C#:

NameTextBox.Text = myProduct.ProductName;

In VB:

NameTextBox.Text = myProduct.ProductName

3. After the user makes any changes, the user interface component assigns the current values in the controls back to the business object properties.

For example, the value in each control on the ProductWin form is assigned back to its associated Product business object property. To illustrate, the line of code required to set the ProductName property to the current value in the Name TextBox control is as follows:

In C#:

myProduct.ProductName = NameTextBox.Text;

In VB:

myProduct.ProductName = NameTextBox.Text

4. The business object component updates the DataTable using the property values and passes it back to the data access component, which updates the database with the changed data.

For example, the value of each Product business object property is assigned to the associated field in the DataTable, and the result is passed to the data access component, which updates the Product table. To illustrate, the line of code required to set the ProductName field in the DataTable to the value of the ProductName business object property is as follows:

In C#:

dt.Rows[0]["ProductName"] = myProduct.ProductName;

In VB:

dt.Rows(0).item("ProductName") = myProduct.ProductName

Using object binding allows you to skip steps 2 and 3. Object binding automatically populates the controls on the user interface from the business object properties. As the users change the contents of the controls, object binding updates the associated business object properties, keeping them in synchronization.

That still leaves steps 1 and 4 for you. This link provides information on building a data access component to handle steps 1 and 4.

In summary, object binding is the process of binding control properties directly to properties of your business objects. For example, you could bind the Text property of a TextBox control to the ProductName property of a Product business object. When the form is displayed, the runtime automatically displays the value of the ProductName property in the TextBox. And if the user changes the text in the TextBox control, the runtime modifies the ProductName property accordingly. This saves you from writing the code required to transfer data back and forth between the controls on the user interface and the business object properties.

Visual Studio provides design-time tools for working with your business objects as data sources for your user interface, making it easy to bind each control to its associated business object property. The only requirement for your business objects to work with these tools is that the business object class needs at least one public property. No specific constructors, interfaces, or attributes are needed.

Object Binding Versus Data Binding

Don’t confuse the term object binding with the more generalized term data binding. Data binding is the broad term for binding control properties to data from any data source. Object binding is just one type of data binding. Some common types of data binding are as follows:

  • Binding to tables in a database (Visual Studio generates code to
    define a typed DataSet and TableAdapters)
  • Binding to stored procedures in a database (Visual Studio generates
    code to define a typed DataSet and TableAdapters)
  • Binding to a business object (object binding does not generate code;
    it just sets control properties)
  • Binding to an array or collection of data
  • Binding to a Web service

When binding to a database, Visual Studio generates a significant amount of code and then binds the user interface to that generated code. Object binding binds to your code. That gives you much more control and greatly simplifies the maintenance of your application.

Using Object Binding

You use object binding by following these steps:

1. Build the business objects for your application.

2. Define a business object data source in the Windows Application
project containing your user interface.

3. Bind properties of the controls on the form to business object
properties.

Although it is much easier to think about object binding as a direct binding of a control’s property to a specific business object’s property, object binding frequently uses a BindingSource component as an intermediary. A BindingSource is a component on a form that binds the controls on the form to the business object. Each control is bound to the BindingSource component, which in turn is bound to the business object. This makes it much easier to change the binding for all controls by changing the BindingSource without having to separately rebind each control.

You set the BindingSource to an individual business object instance in your code. The runtime then binds all the properties associated with that instance to the controls, thereby displaying the business object property values in the controls. And as the user changes the content of any controls, the business object property values are changed accordingly.

A form can contain multiple BindingSource components. For example, a ProductWin form can contain product data and display a drop-down list of product types. You can define a BindingSource component for the product data and a second BindingSource component for the product type data.

(Based on an except from "Doing Objects in Visual Basic 2005".)

For more information on object binding, see these links:

Enjoy!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2018 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs