Scott on Writing

Musings on technical writing...

Tuesday, June 27, 2006 #

Customizing the TableAdapter

In my Working with Data in ASP.NET 2.0 tutorial series, Tutorial #1 begins the series by creating a Data Access Layer (DAL) using a Typed DataSet. A Typed DataSet is composed of a collection of strongly-typed DataTables and TableAdapters. The DataTables serve as business objects for holding the underlying database data, whereas the TableAdapters function as the DAL, synchronizing the contents of the business objects with the underlying data store.

The code for the TableAdapters is automatically generated by Visual Studio based on their properties and how you configured it in the Create TableAdapter Wizard. However, TableAdapters are implemented as partial classes, meaning it's possible to extend the functionality of the auto-generated TableAdapter by adding your own class file. The benefit of the partial class approach is that you can add methods and properties to the auto-generated TableAdapter without fear of having your modifications eaten the next time Visual Studio wants to regenerate the code. Let's look at how to utilize the fact that the TableAdapter is a partial class.

The TableAdapter's data store-specific details - the connection information, the command-level settings, and so on - are buried in its code as internal and private properties and methods. This makes sense, as the point of the DAL (and OOP in general) is to abstract away implementation details. However, there may be times where you need to update the connection string from the Business Logic Layer (BLL), or change the CommandTimeout for particularly long-running queries. By simply creating a new class file in your project, you can add public properties or methods to the TableAdapter than can access and modify those private and internal properties. For example, the following code adds a public ConnectionString property and a public SetCommandTimeout(timeout) method to the ProductsTableAdapter class (one of the TableAdapters created in the Working with Data in ASP.NET 2.0 tutorial series):

    1 namespace NorthwindTableAdapters

    2 {

    3     public partial class ProductsTableAdapter

    4     {

    5         public string ConnectionString

    6         {

    7             get

    8             {

    9                 return this.Connection.ConnectionString;

   10             }

   11             set

   12             {

   13                 Connection.ConnectionString = value;

   14             }

   15         }

   16 

   17         public void SetCommandTimeout(int timeout)

   18         {

   19             foreach (IDbCommand command in CommandCollection)

   20                 command.CommandTimeout = timeout;

   21         }

   22     }

   23 }

This property and method can then be used in the BLL after creating an instance of the TableAdapter and before issuing the call to access data (such as GetProducts()).

One word of warning - exposing implementation details in the DAL may be necessary for certain scenarios, but don't make a habit of it. Ideally, the DAL and BLL and presentation tier should each be self-contained and not need to “leak” implementation details or settings to other tiers...

posted @ 11:55 AM | Feedback (10)

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<June 2006>
SMTWTFS
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

Comment Stats

DayTotal% of Total
Sunday 2046.9%
Monday 42314.3%
Tuesday 50116.9%
Wednesday 54518.4%
Thursday 57219.3%
Friday 53618.1%
Saturday 1856.2%
Total 2966100.0%

Hour1Total% of Total
12:00 AM 752.5%
1:00 AM 802.7%
2:00 AM 672.3%
3:00 AM 812.7%
4:00 AM 642.2%
5:00 AM 1234.1%
6:00 AM 1153.9%
7:00 AM 1755.9%
8:00 AM 1876.3%
9:00 AM 1565.3%
10:00 AM 1866.3%
11:00 AM 1926.5%
12:00 PM 1996.7%
1:00 PM 1846.2%
2:00 PM 1675.6%
3:00 PM 1344.5%
4:00 PM 1153.9%
5:00 PM 1063.6%
6:00 PM 993.3%
7:00 PM 1063.6%
8:00 PM 903.0%
9:00 PM 842.8%
10:00 PM 893.0%
11:00 PM 923.1%
Total 2966100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.91157
Monday 4.92379
Tuesday 4.21471
Wednesday 7.42668
Thursday 6.53666
Friday 5.17450
Saturday 4.73175
Total 5.522966

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 4.0048
8:00 AM 4.29133
9:00 AM 6.04290
10:00 AM 5.83274
11:00 AM 4.36192
12:00 PM 6.44348
1:00 PM 3.14132
2:00 PM 5.04227
3:00 PM 7.97303
4:00 PM 3.8199
5:00 PM 6.00168
6:00 PM 4.56114
7:00 PM 8.95188
8:00 PM 8.58163
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.522966

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles