ASP.NET 2.0数据教程:给DAL添加定制编码


  本文标签:定制编码 DAL

  第六步:给DAL添加定制编码

  添加到强类型DataSet中的TableAdapter和DataTable是在一个XML Schema定义文 件(Northwind.xsd)中定义的  。你可以在解决方案资源管理器里在Northwind.xsd 文件上按右鼠标,选择“查看编码(View Code)”,打开这个Schema文件来查看其中内容  。

  Northwinds强类型DataSet的XML Schema定义文件 

  图32:Northwinds强类型DataSet的XML Schema定义文件

  这个schema信息在设计时编译之后会被翻译成C#或Visual Basic 编码,或者如果有必要的话,会在运行时翻译,然后你就能在调试器里单步遍历执行  。想查看这些自动生成的编码的话,在类视图里,展开TableAdapter 类或者强类型的DataSet 类  。如果在屏幕上看不到类视图的话,在“查看”(View)菜单里选择“ 类视图”,或者按键组合Ctrl+Shift+C  。在类视图里,你能看到强类型的DataSet类和TableAdapter类的属性,方法和事件  。想看某个特定的方法的编码话,在类视图双击对应方法的名字或者在方法上按右鼠标,选择“移至定义区(Go To Definition)”  。

  在类视图里选择“移至定义区(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#

  

  1. using System;  
  2. using System.Data;  
  3. using NorthwindTableAdapters;  
  4.  
  5. public partial class   
  6.  
  7. Northwind  
  8. {  
  9.     public partial class   
  10.  
  11. SuppliersRow  
  12.     {  
  13.         public Northwind.ProductsDataTable GetProducts()  
  14.         {  
  15.             ProductsTableAdapter productsAdapter =  
  16.              new ProductsTableAdapter();  
  17.             return 
  18.               productsAdapter.GetProductsBySupplierID(this.SupplierID);  
  19.         }  
  20.     }  
  21. }  
  22.  

  这个部分(partial)类指示编译器在编译Northwind.SuppliersRow类时,应该包含我们刚定义的这个GetProducts()方法  。如果你编译你的项目,然后返回类视图,你就会看到GetProducts()已被列为Northwind.SuppliersRow的一个方法  。

  GetProducts()方法成为Northwind.SuppliersRow类的一部分 

  图34: 定制编码:GetProducts()方法成为Northwind.SuppliersRow类的一部分

  GetProducts()方法现在就能用来枚举一个指定供应商的产品列单,如下列编码所示:

  C#

  

  1. NorthwindTableAdapters.SuppliersTableAdapter   
  2.  
  3. suppliersAdapter = new   
  4.  
  5. NorthwindTableAdapters.SuppliersTableAdapter();  
  6.  
  7. // Get all of the suppliers  
  8. Northwind.SuppliersDataTable suppliers =  
  9.   suppliersAdapter.GetSuppliers();  
  10.  
  11. // Enumerate the suppliers  
  12. foreach (Northwind.SuppliersRow supplier in suppliers)  
  13. {  
  14.     Response.Write("Supplier: " +   
  15.  
  16. supplier.CompanyName);  
  17.     Response.Write("");  
  18.  
  19.     // List the products for this supplier  
  20.     Northwind.ProductsDataTable products = supplier.GetProducts();  
  21.     foreach (Northwind.ProductsRow product in products)  
  22.         Response.Write("" +   
  23.  
  24. product.ProductName + "");  
  25.  
  26.     Response.Write(" ");  
  27. }  
  28.  

  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

  

  1. < %@ Page Language="C#"   
  2.  
  3. AutoEventWireup="true" CodeFile="SuppliersAndProducts.aspx.cs"   
  4.  
  5. Inherits="SuppliersAndProducts" %>  
  6.  
  7. < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   
  8.  
  9. Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  10.  
  11. < html xmlns="http://www.w3.org/1999/xhtml" >  
  12. < head runat="server">  
  13.     < title>Untitled Pagetitle>  
  14.     < link href="Styles.css"   
  15.  
  16. rel="stylesheet"   
  17.  
  18. type="text/css"   
  19.  
  20. />  
  21. head>  
  22. < body>  
  23.     < form id="form1" runat="server">  
  24.     < div>  
  25.         < h1>  
  26.             Suppliers and Their Productsh1>  
  27.         < p>  
  28.             < asp:GridView ID="GridView1" runat="server" 
  29.              AutoGenerateColumns="False" 
  30.              CssClass="DataWebControlStyle">  
  31.                 < HeaderStyle CssClass="HeaderStyle" />  
  32.                 < AlternatingRowStyle CssClass="AlternatingRowStyle" />  
  33.                 < Columns>  
  34.                     < asp:BoundField DataField="CompanyName" 
  35.                       HeaderText="Supplier" />  
  36.                     < asp:TemplateField HeaderText="Products">  
  37.                         < ItemTemplate>  
  38.                             < asp:BulletedList ID="BulletedList1" 
  39.                              runat="server" DataSource="< %#  
  40.               ((Northwind.SuppliersRow)((System.Data.DataRowView)  
  41.               Container.DataItem).Row).GetProducts() %>"  
  42.                                     DataTextField="ProductName">  
  43.                             asp:BulletedList>  
  44.                         ItemTemplate>  
  45.                     asp:TemplateField>  
  46.                 Columns>  
  47.             asp:GridView>  
  48.              p>  
  49.  
  50.     div>  
  51.     form>  
  52. body>  
  53. html>  

  SuppliersAndProducts.aspx.cs

  C#

  

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls