Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

July 10, 2009

Populating a Business Object from a DataTable

Filed under: C#,Data,OOP,VB.NET @ 3:43 pm

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!

1 Comment

  1.   MarkP — April 9, 2010 @ 5:50 am    Reply

    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

RSS feed for comments on this post. TrackBack URI

Leave a comment

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