Populating Your Business Objects
If you run your application at this point, you will see that it does not do anything. Even though the controls are bound to the properties, the properties have no values. So the next big step in object binding is populating the business objects.
|Object binding to a class that inherits from BindingList(Of T) provides a very nice way to display and manage lists of things.|
You can retrieve data and populate your business objects in many different ways. You could use a DataSet or DataTable; you could set up a DataReader; you could call a Web service to retrieve the needed data; or you could use TableAdapters to further bind your properties to database fields.
To keep this example simple, a private Retrieve
method on the business object pretends to retrieve the needed data. Actually, it uses ADO.NET to create a DataTable in code. That way you don't need to create a database, stored procedures, and/or Web service in order to test out object binding. Listing 1
shows the VB code for the Retrieve
method; Listing 2
shows the Retrieve
method in C#.
Best practices dictate using the factory pattern to create the business objects and populate them from the retrieved data. You could choose a number of ways to implement the factory pattern. In this example, the factory pattern implementation makes the constructor private (so no other code can create the object) and defines a static (shared) Create
method that creates and populates the instance as shown.
Private Sub New()
' Don't allow creation of this object
Public Shared Function Create(ByVal CustomerID _
As Int32) As Customer
' Create the customer
Dim oCustomer As New Customer
' Retrieve the data for this customer
Dim dr As DataRow
dr = oCustomer.Retrieve(CustomerID)
' Populate the business object
// Don't allow creation of this object
public static Customer Create(Int32 CustomerID)
// Create the customer
Customer oCustomer = new Customer();
// Retrieve the data for this customer
DataRow dr =oCustomer.Retrieve(CustomerID);
// Populate the business object
The method that populates the business objects is as follows.
Private Sub Populate(ByVal CustomerDataRow As _
Me.LastName = .Item("LastName").ToString
Me.FirstName = .Item("FirstName").ToString
Me.CustomerID = .Item("CustomerID").ToString
private void Populate(DataRow CustomerDataRow)
You would need to enhance the code in the Populate
method each time you add a property to the business object. If you use a TableAdapter or typed DataSet to retrieve the data instead of using an untyped DataSet, DataReader, or some other technique, you would reference the data columns by their automatically generated strongly typed names instead of their quoted string column names.
To complete the object binding, add code in the Load
event for the form to create and bind the object. Call the Create
method to create the instance and then assign the CustomerBindingSource to that instance to bind it as follows:
Private Sub CustomerWin_Load(ByVal sender As _
Object, ByVal e As System.EventArgs) _
Dim CustomerInstance As Customer
CustomerInstance = Customer.Create(1)
private void CustomerWin_Load(object sender,
Customer CustomerInstance =
At this point you can run the application. If all is well, the form should appear with the property values assigned as the text of the controls.