這個應用程式套件組合含三個對象:
1.OrderForm.aspx----代表使用者介面層的asp.net頁面。
2.BizObject-----代表業務層的組件。
3.DataObject-----代表資料層的組件。
首先,建立使用者介面層。構成使用者介面的asp.net頁面如下代碼。
OrderForm.aspx
<%@ Import Namespace="myComponents" %>
<Script Runat="Server">
Sub Button_Click(s as Object, e as EventArgs)
Dim myBizObject as new BizObject
If IsValid Then
Try
myBizObject.CheckOrder(txtCustomer.Text,dropProduct.SelectedItem.Text,txtUnitPrice.Text,txtQuantity.Text,dropState.SelectedItem.Text)
Catch expException As Exception
lblError.Text=expException.Message
End Try
End If
End Sub
</Script>
<html>
<head>
<title>OrderForm.aspx</title>
</head>
<body>
<form Runat="Server">
<h2>Enter an Order:</h2>
<asp:Label id="lblError" ForeColor="Red" Font-Bold="True" EnableViewState="False" Ruant="Server" />
<p>
Customer Name:<br>
<asp:TextBox ID="txtCustomer" Runat="Server" />
<asp:RequiredFieldValidator Text="必須輸入一個顧客名!"
ControlToValidate="txtCustomer" Runat="Server" />
<p>
Product:<br>
<asp:ListBox ID="dropProduct" Runat="Server">
<asp:ListItem Text="Hair Dryer" />
<asp:ListItem Text="Shaving Cream" />
<asp:ListItem Text="Electric Comb" />
</asp:ListBox>
<asp:RequiredFieldValidator
ControlToValidate="dropProduct"
Text="你必須選擇一件產品!"
Runat="Server" />
<p>
Unit Price:
<br>
<asp:TextBox ID="txtUnitPrice" Runat="Server" />
<asp:RequiredFieldValidator ControlToValidate="txtUnitPrice" Text="你必須輸入一個產品的單價!"
Runat="Server" />
<p>
Quantity:
<br>
<asp:TextBox ID="txtQuantity" Runat="Server" />
<asp:RequiredFieldValidator ControlToValidate="txtQuantity" Text="你必須輸入產品數量。"
Runat="Server" />
<asp:ComparValidator ControlToValidate="txtQuantity" Text="數量必須是數字!"
Operator="DataTypeCheck"
Type="Integer" Runat="Server" />
<p>
Customer State:
<br>
<asp:DropDownLIst
ID="dropState"
Runat="Server">
<asp:ListItemText="California" />
<asp:ListItem Text="Nevada" />
<asp:ListItem Text="Washington" />
</asp:DropDownList>
<p>
<asp:Button Text="Place Order" OnClick="Button_Click" Runat="Server" />
</form>
</body>
</html>
這個頁面請求5項資訊:顧客名,產品,產品的單價,數量和顧客所在的州。
當在表單中輸入所有資訊並且點擊Place Order按鈕時,執行Button_Click子常式。這個子常式建立BizOrder組件的執行個體並且將所有表單資訊傳遞給組件的ChekcOrder()方法。
BiZObject.vb
Imports System
NameSpace myComponents
Public Class BizObject
Sub CheckOrder(Customer as String,Product as String,UnitPrice as Double,Quantity as Integer,State _ As String)
If Quantity<=0 Or Quantity>100 Then
Throw New ArgumentException("無效數字。")
End If
If State="Califonia" and Product="Hair Dryer" Then
Throw New ArgumentException("Californians 不能購買Hair Dryers!")
End If
If State="Washington" Then
UnitPrice+=UnitPrice * 0.6
End If
Dim myDataObject as New DataObject
myDataObject.SaveOrder(Customer,Product,UnitPrice,Quantity,State)
End Sub
End Class
End NameSpace
業務組件包含應用程式的所有商務規則。此組件封裝了以下規則:
1。產品數量必須大於0並且小於100。
2。住在加利福尼來的人不能買吹風機。
3。住在華盛頓的人必須多付6%的銷售銳。
如果訂單不滿足前二個要求之一,那麼引發一個異常。錯誤在OrderEntry.aspx頁面中被捕獲並且被顯示在一個Label控制項中。
DataObject.vb
Imports System
Improts System.IO
NameSpace myComponents
Public Class DataObject
Sub SaveOrder(Customer as String,Product as String,UnitPrice as Double,Quantity as Integer,State _ As String)
Dim strPath as string="C:\Orders.txt"
Dim strmFile as StreamWriter
strmFile=File.AppendText(strPath)
strmFile.write("Customer:" &Customer & Environment.newline)
strmFile.write("Product:" & product & Environment.newLine)
strmFile.Write("Unit Price:" & UnitPrice.ToString() & Environment.NewLine)
strmFile.Write("Quantity:" & quantity.ToString() & Environment.NewLine)
strmFile.Write("State:" & state & Environment.NewLine)
strmFile.Write("==============="& Environment.NewLine)
strmFile.Close()
End Sub
End Class
End NameSpace
一定要編譯BizObject和DataObject組件並且將它們複製到你的應用程式的/Bin目錄下,才能使用這些組件。編譯的次序很重要。因為BiZObject組件要引用DataObject組件,你必須首先創造DataObject組件。可以在Dos提示符下使用以下語句編譯這個組件:
vbc /t: Library DataObject.vb
在將DataObject.dll複製到你的應用程式的/BIN目錄之後,可以使用如下語句編譯BizObject組件。
vbc /t:Library /r:DataObject.dll bizObject.vb
注意,在編譯BizObject組件時使用了/r選項;它引用DataObject.dll組件。如果不包含此引用,會收到錯誤資訊User-defined type not defined:DataObject.