本文标签:定制编码 DAL
第六步:给DAL添加定制编码
添加到强类型DataSet中的TableAdapter和DataTable是在一个XML Schema定义文 件(Northwind.xsd)中定义的 。你可以在解决方案资源管理器里在Northwind.xsd 文件上按右鼠标,选择“查看编码(View Code)”,打开这个Schema文件来查看其中内容 。
图32:Northwinds强类型DataSet的XML Schema定义文件
这个schema信息在设计时编译之后会被翻译成C#或Visual Basic 编码,或者如果有必要的话,会在运行时翻译,然后你就能在调试器里单步遍历执行 。想查看这些自动生成的编码的话,在类视图里,展开TableAdapter 类或者强类型的DataSet 类 。如果在屏幕上看不到类视图的话,在“查看”(View)菜单里选择“ 类视图”,或者按键组合Ctrl+Shift+C 。在类视图里,你能看到强类型的DataSet类和TableAdapter类的属性,方法和事件 。想看某个特定的方法的编码话,在类视图双击对应方法的名字或者在方法上按右鼠标,选择“移至定义区(Go To Definition)” 。
图33:在类视图里选择“移至定义区(Go To Definition)”,查看自动生成的编码
虽然自动生成的编码省时省力,但这样的编码往往是非常通用化的(generic),为满足一个应用程序特有的需求需要做些定制,就是所谓的定制编码 。但扩展自动生成的编码的风险在于,如果生成这些编码的工具决定该是重新生成这些编码的时候了,则会把你定制的编码冲掉 。使用.NET 2.0中的一个新的部分(partial)类的概念,很容易将一个类的定义分写在几个文件里 。这允许我们给自动生成的类添加我们自己的方法,属性,和事件,而不用担心Visual Studio会冲掉我们的定制编码 。
为示范如何定制DAL起见,让我们来给SuppliersRow 添加一个GetProducts()方法 。这个SuppliersRow类代表了Suppliers表的个别记录,每个供应商(supplier)可以 提供0个到多个产品,所以GetProducts()将返回指定的供应商的这些产品 。做法如下,在App_Code文件夹里添加一个新的类文件,将其命名为SuppliersRow.cs,然后在其中添加下列编码:
C#
- 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#
- NorthwindTableAdapters.SuppliersTableAdapter
-
- suppliersAdapter = new
-
- NorthwindTableAdapters.SuppliersTableAdapter();
-
-
- Northwind.SuppliersDataTable suppliers =
- suppliersAdapter.GetSuppliers();
-
-
- foreach (Northwind.SuppliersRow supplier in suppliers)
- {
- Response.Write("Supplier: " +
-
- supplier.CompanyName);
- Response.Write("");
-
-
- 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.nets 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
- < %@ Page Language="C#"
-
- AutoEventWireup="true" CodeFile="SuppliersAndProducts.aspx.cs"
-
- Inherits="SuppliersAndProducts" %>
-
- < !DOCTYPE html PUBLIC "-
-
- Transitional
-
- < html xmlns="http://www.w3.org/1999/xhtml" >
- < head runat="server">
- < title>Untitled Pagetitle>
- < link href="Styles.css"
-
- rel="stylesheet"
-
- type="text/css"
-
- />
- head>
- < body>
- < form id="form1" runat="server">
- < div>
- < h1>
- Suppliers and Their Productsh1>
- < 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#
- 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
|