ASP.NET 2.0數(shù)據(jù)教程:給DAL添加定制編碼
第六步:給DAL添加定制編碼
添加到強(qiáng)類型DataSet中的TableAdapter和DataTable是在一個XML Schema定義文 件(Northwind.xsd)中定義的。你可以在解決方案資源管理器里在Northwind.xsd 文件上按右鼠標(biāo),選擇“查看編碼(View Code)”,打開這個Schema文件來查看其中內(nèi)容。
圖32:Northwinds強(qiáng)類型DataSet的XML Schema定義文件
這個schema信息在設(shè)計(jì)時編譯之后會被翻譯成C#或Visual Basic 編碼,或者如果有必要的話,會在運(yùn)行時翻譯,然后你就能在調(diào)試器里單步遍歷執(zhí)行。想查看這些自動生成的編碼的話,在類視圖里,展開TableAdapter 類或者強(qiáng)類型的DataSet 類。如果在屏幕上看不到類視圖的話,在“查看”(View)菜單里選擇“ 類視圖”,或者按鍵組合Ctrl+Shift+C。在類視圖里,你能看到強(qiáng)類型的DataSet類和TableAdapter類的屬性,方法和事件。想看某個特定的方法的編碼話,在類視圖雙擊對應(yīng)方法的名字或者在方法上按右鼠標(biāo),選擇“移至定義區(qū)(Go To Definition)”。
圖33:在類視圖里選擇“移至定義區(qū)(Go To Definition)”,查看自動生成的編碼
雖然自動生成的編碼省時省力,但這樣的編碼往往是非常通用化的(generic),為滿足一個應(yīng)用程序特有的需求需要做些定制,就是所謂的定制編碼。但擴(kuò)展自動生成的編碼的風(fēng)險在于,如果生成這些編碼的工具決定該是重新生成這些編碼的時候了,則會把你定制的編碼沖掉。使用.NET 2.0中的一個新的部分(partial)類的概念,很容易將一個類的定義分寫在幾個文件里。這允許我們給自動生成的類添加我們自己的方法,屬性,和事件,而不用擔(dān)心Visual Studio會沖掉我們的定制編碼。
為示范如何定制DAL起見,讓我們來給SuppliersRow 添加一個GetProducts()方法。這個SuppliersRow類代表了Suppliers表的個別記錄,每個供應(yīng)商(supplier)可以 提供0個到多個產(chǎn)品,所以GetProducts()將返回指定的供應(yīng)商的這些產(chǎn)品。做法如下,在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類時,應(yīng)該包含我們剛定義的這個GetProducts()方法。如果你編譯你的項(xiàng)目,然后返回類視圖,你就會看到GetProducts()已被列為Northwind.SuppliersRow的一個方法。
圖34: 定制編碼:GetProducts()方法成為Northwind.SuppliersRow類的一部分
GetProducts()方法現(xiàn)在就能用來枚舉一個指定供應(yīng)商的產(chǎn)品列單,如下列編碼所示:
C#
- 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:數(shù)據(jù)也可以在任何一種asp.net的Web控件中顯示。下面這個網(wǎng)頁 使用了含有2個字段的GridView 控件:
一個BoundField用以顯示每個供應(yīng)商的名字,
另一個TemplateField,包含了一個BulletedList控件,用來綁定針對每個供應(yīng)商調(diào)用 的GetProducts()方法返回的結(jié)果
我們將在以后的教程里討論怎樣來顯示這樣的主/從(master-detail)報表。在這里,這個例子的目的是用來示范如何使用添加到Northwind.SuppliersRow類中的自定義的方法的。
SuppliersAndProducts.aspx
asp.net
- < %@ 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 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.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: 定制編碼:供應(yīng)商的公司名字列在左欄,他們的產(chǎn)品列在右欄
總結(jié)
構(gòu)造web應(yīng)用時,創(chuàng)建DAL應(yīng)該是你最先做的步驟之一,應(yīng)該在你開始創(chuàng)建表現(xiàn)層之前進(jìn)行。使用Visual Studio的話,創(chuàng)建基于強(qiáng)類型DataSet的DAL是個可以不寫一行編碼,在10到15分鐘內(nèi)就可完成的任務(wù)。以后的教程將建立在這個DAL基礎(chǔ)之上。在下一個教程里,我們將定義一堆業(yè)務(wù)規(guī)則,然后看一下如何在一個分開的業(yè)務(wù)邏輯層里實(shí)現(xiàn)這些規(guī)則。
【編輯推薦】