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 //会创建一个叫App_Code的文件夹, click Yes.
| C# | VB | |
|
|
||
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//SQL语句的步骤, 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 //N列architectural practice.
3. In the Solution Explorer right-click App_Code //注意而不是App_Data文件夹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.
| C# | VB | |
public AuthorsDataSet GetAuthors(string state)
{
AuthorsDataSetTableAdapters.authorsTableAdapter authorsTableAdapter =
new AuthorsDataSetTableAdapters.authorsTableAdapter();
AuthorsDataSet authorsData = new AuthorsDataSet();
authorsTableAdapter.Fill(authorsData.authors, state);
return authorsData;
}
|
||
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.
注:此篇文章生词和学述词语较多,不易读懂,应多读几遍。
浙公网安备 33010602011771号