Typed DataSets vs Late Bound DataSets

Chapter 11 - Performance
byJohn Kauffman, Fabio Claudio Ferracchiatiet al.?
Wrox Press ?2002

In the examples we've looked at so far, the datasets have been late bound. In other words, the Dataset instance determines which table it's going to be associated with at runtime. An alternative option is to use a typed dataset, in which case the association takes place when the application is compiled. From a programmatic standpoint, a typed DataSet is simpler to use, and your developer's instinct might lead you to expect it to be faster than its un-typed counterpart. In fact, as we'll see, a typed data set is really just a programmer-friendly wrapper around a late bound DataSet, and the performance change is negligible.

Try It Out - Creating a Typed DataSet

start example

The steps required to develop a typed DataSet are as follows:

  1. In code, create a DataSet and associate it with a particular SQL query (that is, use late binding, as normal).

  2. Call the DataSet's GetXmlSchema() method in order to retrieve the schema. Save this schema in a file called WXEmployee.xsd.

  3. Using the schema file, run the xsd command-line utility to create a file containing the definition of a typed DataSet. An example of this is as follows, where the /d option specifies that a typed DataSet is being generated, the /l:VB option specifies that a VB.NET source file will be generated, and /n specifies the namespace used within the generated VB.NET file.

     > xsd /d /1:VB WXEmployee.xsd /n:WXDBPerfDemo 

  4. The previous command creates a file called WXEmployee.vb that can be added to our project using the Project | Add Existing Item menu item in Visual Studio .NET. The class contained in WXEmployee.vb is derived from DataSet:

     Public Class WXEmployeeDataSet Inherits DataSet End Class 

  5. For a given DataSet, create an SqlDataAdapter and a SqlCommandBuilder object that can be used to update information in that DataSet. The WXSetupDataSet() method below demonstrates how to create a SqlCommandBuilder associated with a SqlDataAdapter, and how to use the SqlDataAdapter to fill a DataSet:

     Const _selectAll As String = _     "SELECT CustomerID, CompanyName, ContactName, ContactTitle, " & _     "Address, City, Region, PostalCode, Country, Phone, Fax " & _     "FROM WXCustomers" Private Sub WXSetupDataSet(ByVal command As  SqlCommand, _                            ByVal ds As DataSet, _                            ByRef da As SqlDataAdapter, _                            ByRef cb As SqlCommandBuilder)     command.CommandText = _selectAll     command.CommandType = CommandType.Text     da = New SqlDataAdapter(command)     cb = New SqlCommandBuilder(da)     da.Fill(ds, "WXCustomers") End Sub 

  6. To test code that demonstrates a late bound DataSet, simply create a DataSet and pass it to WXSetupDataSet():

     Dim da As SqlDataAdapter Dim cb As SqlCommandBuilder Dim ds As new DataSet() WXSetupDataSet(command, ds, da, cb) 

  7. To test code that demonstrates a typed DataSet, create a typed DataSet instance (such as an object of the WXEmployeeDataSet class created earlier), and pass that to WXSetupDataSet():

     Dim da As Sql DataAdapter Dim cb As SqlCommandBuilder Dim ds As New WXDBPerfDemo.WXEmployeeDataSet() WXSetupDataSet(command, ds, da, cb) 

The code we actually used to evaluate typed versus late bound DataSet instances can be found in the WXDataSetModify project in the download files - it's not dissimilar from the code we used for our earlier trial involving different data access objects. What we're interested in here is the result of the evaluation, which was that a typed Dataset is actually couple of percentage points slower than a late bound DataSet.

You probably find that surprising at first blush, but a typed DataSet is really just a DataSet with some wrapper code to simplify programming and perform some error handling, the combined result of which is a slight performance degradation. Frankly, had the late bound DataSet code used in WXDataSetModify.aspx been as thorough with its error handling, performance would likely be identical.

The conclusion here is that while there's no raw performance issue surrounding the use of typed versus late bound DataSet instances, there are still good reasons to prefer the former. The extra facilities they provide make writing code faster, and should result in fewer bugs. This gives developers more time to address performance issues elsewhere in their applications.

end example



Beginning ASP. NET 2.0 and Databases
Beginning ASP.NET 2.0 and Databases (Wrox Beginning Guides)
ISBN: 0471781347
EAN: 2147483647
Year: 2004
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net