Adding a Data Layer Class
In most cases it's best to architect your applications to use a data access layer
that encapsulates
all the database interactions. This protects the database, simplifies
calls from the presentation layer, and allows you to wrap the data access logic in
business logic (or use a separate business tier altogether).
VWD supports this commonly used architecture by allowing you to bind to an object
as a data source.
In this sample you will build a simple data access layer and consume it via an ObjectDataSource
control, binding it to a GridView control. This class will encapsulate methods exposed
by a TableAdapter class that services a typed DataSet. This implementation will
become much clearer as you proceed through the next few lessons.
You begin by adding a typed DataSet to your project. Typed DataSets are better to
use than generic DataSets because they strongly type the objects unique to your
data source based on the data source's schema (metadata). You thus have full IntelliSense
support for your data objects such as tables, rows and columns, reducing guesswork
and coding errors. You also get a performance boost because late binding is not
used to access the data objects (e.g., field name look-up in a collection).
1. In the Solution Explorer right-click the project and select
Add New Item. In the Add New Item dialog select
DataSet and call it "AuthorsDataSet.xsd". Click Add.
When prompted to create an App_Code folder, click Yes.
ASP.NET 2.0 includes a suite of special folders. Among them is the App_Code folder. VWD monitors source code files in this folder, automatically compiling them and
making the resulting binary accessible to code in the rest of your project. In this
way it acts like the traditional bin folder, but you don't have to explicitly compile
or add a reference to the resulting assembly.
When a typed DataSet is added to your project, VWD also adds a TableAdapter class
to work with the DataSet. You will now configure the TableAdapter. This is similar
to what you saw when configuring the SqlDataSource object.
2. In the TableAdapter Configuration Wizard click Next
twice. In the Enter a SQL Statement step, enter the SELECT statement you
used in the previous lessons: "SELECT [au_id], [au_lname], [au_fname], [phone],
[address], [city], [state], [zip], [contract] FROM [authors] WHERE ([state] = @state)". Click Next. You will notice that all options are selected. Click Next. Click Finish.
Press CTRL+S to save the changes.
(The AuthorsDataSetTableAdapters namespace is not available until
you do so.) You should see the following in the Design
window.
You have configured your typed DataSet and TableAdapter. Although you could work directly with these classes from the presentation layer, you will now add a custom
data access class to simulate real-world n-tier architectural practice.
3. In the Solution Explorer right-click App_Code
and select Add New Item. In the Add New Item dialog
select Class and call it "DataAccess.vb" (or, if working in C#,
"DataAccess.cs"). Click Add.
4. To the DataAccess class add the following method for filling
the typed DataSet, removing the line breaks required for Web
presentation.
Public Function GetAuthors(ByVal state As String) As AuthorsDataSet
Dim authorsTableAdapter As New AuthorsDataSetTableAdapters.authorsTableAdapter
Dim authorsData As New AuthorsDataSet()
authorsTableAdapter.Fill(authorsData.authors, state)
Return authorsData
End Function
VB
Notice that this code provides no additional functionality over the TableAdapter class. In a real world
scenario you would likely have additional custom logic wrapping this code, such as validation or business logic.