Populating a Business Object from a DataTable

Posted Fri, Jul 10 2009 13:43 by Deborah Kurata

Most business applications have business objects such as customer, order, or invoice. Often, the data access layer (DAL) provides the data and your code needs to use that data to manually populate a business object.

This post describes how to manually populate a business object from a DataTable. It uses the Customer class defined here.

The code is first shown in both VB and C#. It is then described in detail below.

In C#:

public static List<Customer> Retrieve()
{
   DataTable dt = Dac.ExecuteDataTable("CustomerRetrieveAll", null);

    List<Customer> customerList = new List<Customer>();

    foreach (DataRow dr in dt.Rows)
    {
        customerList.Add(new Customer()
                    {
                        CustomerId = (int)dr["CustomerId"],
                        LastName = (string)dr["LastName"],
                        FirstName = (string)dr["FirstName"]
                    });
    }

    return customerList;
}

In VB:

Public Shared Function Retrieve() As List(Of Customer)

    Dim dt As DataTable = Dac.ExecuteDataTable( _
                   
"CustomerRetrieveAll", nothing)

    Dim customerList As New List(Of Customer)

    For Each dr As DataRow In dt.Rows
       customerList.Add(New Customer With _
                    {.CustomerId = CType(dr("CustomerID"), Integer), _
                     .LastName = dr("LastName").ToString, _
                     .FirstName = dr("FirstName").ToString}) 
    Next

    Return customerList
End Function

The Retrieve method is public and static (shared in VB). It is public so it can be called from the user interface code. It is static/shared because it does not use or retain any state. This allows the function to be called without creating an instance of the class containing the function.

This function calls the ExecuteDataTable method from here, which returns a DataTable. It then creates a new list and adds a new customer to the list for each row in the DataTable.

The resulting list of customers is returned. The user interface code can then use this list to bind to a control such as a grid or combo box. You can also search, sort, filter, or work with this list using LINQ or Lambda expressions.

NOTE: If the fields in the DataTable match the properties of the business object, you could use reflection to map the fields to the properties. Though reflection does have a performance hit, it provides a more general solution that could be used by any business object.

Enjoy!

Filed under: , , , , , ,

Comments

# Daily tech links for .net and related technologies - July 9-13, 2009

Monday, July 13, 2009 3:15 AM by Sanjeev Agarwal

Daily tech links for .net and related technologies - July 9-13, 2009 Web Development Using Custom T4

# re: Populating a Business Object from a DataTable

Friday, April 09, 2010 5:50 AM by MarkP

Hi Deborah,

Another very useful tip, thank you!

Do you have an example of "using reflection to map the fields to the properties"?

I've got many different objects (where the properties match the fields of the dataset) and it would make life much easier if I didn't have to manually match up each field and property.

Also, when you say that this would lead to a performance hit, would it be easy to evaluate in advance how much of a hit?

Many thanks,

MarkP

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: