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...