虽然自动生成的编码省时省力,但这样的编码往往是非常通用化的(generic),为满足一个应用程序特有的需 求需要做些定制。但扩展自动生成的编码的风险在于,如果生成这些编码的工具决定该是重新生成这些编码的 时候了,则会把你定制的编码冲掉。使用.NET 2.0中的一个新的部分(partial)类的概念,很容易将一个类的 定义分写在几个文件里。这允许我们给自动生成的类添加我们自己的方法,属性,和事件,而不用担心Visual Studio会冲掉我们的定制编码。
为示范如何定制DAL起见,让我们来给SuppliersRow 添加一个GetProducts()
方法。这 个SuppliersRow
类代表了Suppliers
表的个别记录,每个供应商(supplier)可以 提供0个到多个产品,所以GetProducts()
将返回指定的供应商的这些产品。做法如 下,在App_Code
文件夹里添加一个新的类文件,将其命名为SuppliersRow.cs
, 然后在其中添加下列编码:
C# |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| using System; using System.Data; using NorthwindTableAdapters;
public partial class
Northwind { public partial class
SuppliersRow { public Northwind.ProductsDataTable GetProducts() { ProductsTableAdapter productsAdapter = new ProductsTableAdapter(); return productsAdapter.GetProductsBySupplierID(this.SupplierID); } } }
|
这个部分(partial)类指示编译器在编译Northwind.SuppliersRow
类时,应该包含我们刚定义的这个GetProducts()
方法。如果你编译你的项目,然后返回类视图,你就会看到GetProducts()
已被列为Northwind.SuppliersRow
的一个方法。

图34: GetProducts()
方法成为Northwind.SuppliersRow
类的一部 分
GetProducts()
方法现在就能用来枚举一个指定供应商的产品列单,如下列编码所示:
C# |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| NorthwindTableAdapters.SuppliersTableAdapter
suppliersAdapter = new
NorthwindTableAdapters.SuppliersTableAdapter();
// Get all of the suppliers Northwind.SuppliersDataTable suppliers = suppliersAdapter.GetSuppliers();
// Enumerate the suppliers foreach (Northwind.SuppliersRow supplier in suppliers) { Response.Write("Supplier: " +
supplier.CompanyName); Response.Write("
-
");
// List the products for this supplier
Northwind.ProductsDataTable products = supplier.GetProducts();
foreach (Northwind.ProductsRow product
in products)
Response.Write("" +
product.ProductName + "");
Response.Write("
"); }
|
This data can also be displayed in any of ASP.NET's data Web controls. The following page uses a GridView control with two fields:数据也可以在任何一种ASP.NET的Web控件中显示。下面这个网页 使用了含有2个字段的GridView 控件:
- 一个BoundField用以显示每个供应商的名字,
- 另一个TemplateField,包含了一个BulletedList控件,用来绑定针对每个供应商调用 的
GetProducts()
方法返回的结果
我们将在以后的教程里讨论怎样来显示这样的主/从(master-detail)报表。在这里,这个例子的目的是用 来示范如何使用添加到Northwind.SuppliersRow
类中的自定义的方法的。
SuppliersAndProducts.aspx
ASP.NET |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <%@ Page Language="C#"
AutoEventWireup="true" CodeFile="SuppliersAndProducts.aspx.cs"
Inherits="SuppliersAndProducts" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page
title> <link href="Styles.css"
rel="stylesheet"
type="text/css"
/>
head> <body> <form id="form1" runat="server"> <div> <h1> Suppliers and Their Products
h1> <p> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="DataWebControlStyle"> <HeaderStyle CssClass="HeaderStyle" /> <AlternatingRowStyle CssClass="AlternatingRowStyle" /> <Columns> <asp:BoundField DataField="CompanyName" HeaderText="Supplier" /> <asp:TemplateField HeaderText="Products"> <ItemTemplate> <asp:BulletedList ID="BulletedList1" runat="server" DataSource="<%# ((Northwind.SuppliersRow)((System.Data.DataRowView) Container.DataItem).Row).GetProducts() %>" DataTextField="ProductName">
asp:BulletedList>
ItemTemplate>
asp:TemplateField>
Columns>
asp:GridView>
p>
div>
form>
body>
html>
|
SuppliersAndProducts.aspx.cs
C# |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using NorthwindTableAdapters;
public partial class
SuppliersAndProducts : System.Web.UI.Page { protected void
Page_Load(object sender, EventArgs e) { SuppliersTableAdapter suppliersAdapter = new SuppliersTableAdapter(); GridView1.DataSource = suppliersAdapter.GetSuppliers(); GridView1.DataBind(); } }
|

图 35: 供应商的公司名字列在左栏,他们的产品列在右栏
总结
构造web应用时,创建DAL应该是你最先做的步骤之一,应该在你开始创建表现层之前进行。使用Visual Studio的话,创建基于强类型DataSet的DAL是个可以不写一行编码,在10到15分钟内就可完成的任务。以后的 教程将建立在这个DAL基础之上。在下一个教程里,我们将定义一堆业务规则,然后看一下如何在一个分开的 业务逻辑层里实现这些规则。
祝编程快乐!