Creating Multitiered Web Applications


You can use components to divide your Web application into multiple layers , thereby creating a multitiered application. For example, in a two-tiered application, you might have a user interface layer built from ASP.NET pages and a data layer built from components. In a three-tiered application, you might introduce a third layer, representing your application's business logic, between the user interface and data layers.

Why would you want to create multiple layers? The advantage of dividing an application into multiple layers is that you can more easily manage and change the application.

Earlier in the chapter, for example, you created a component that enables you to save form information to a file. In essence, you created a single component for the data layer. Now suppose that your needs change, and you decide to save the data to a database table instead of a file. In that case, you need to modify only the component. You don't have to touch a line of code in the ASP.NET page itself. In other words, you can isolate changes in the data layer from changes in the user interface layer.

An additional benefit of dividing an application into multiple layers is that doing so makes it easier for multiple teams of programmers to work on the same application at the same time. If all the application logic is contained in a single page, you face the problem of multiple programmers stumbling over each other while performing modifications.

To illustrate these points, you can construct a simple three-tiered Web application consisting of user interface, business, and data layers to create a simple order entry application.

The application consists of the following three objects:

  • OrderForm.aspx ” This ASP.NET page represents the user interface layer.

  • BizObject ” This component represents the business layer.

  • DataObject ” This component represents the data layer.

First, build the user interface layer. The ASP.NET page for the user interface is contained in Listing 6.8. The page requests five pieces of information: the customer name , product, unit price of the product, quantity, and customer's state of residence (see Figure 6.3).

Listing 6.8 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"   Runat="Server" /> <p> Customer Name: <br> <asp:TextBox   id="txtCustomer"   Runat="Server" /> <asp:RequiredFieldValidator   ControlToValidate="txtCustomer"   Text="You must enter a customer name!"   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="You must select a product!"   Runat="Server" /> <p> Unit Price: <br> <asp:TextBox   id="txtUnitPrice"   Runat="Server" /> <asp:RequiredFieldValidator   ControlToValidate="txtUnitPrice"   Text="You must enter a unit price!"   Runat="Server" /> <p> Quantity: <br> <asp:TextBox   id="txtQuantity"   Runat="Server" /> <asp:RequiredFieldValidator   ControlToValidate="txtQuantity"   Text="You must enter a quantity!"   Runat="Server" /> <asp:CompareValidator   ControlToValidate="txtQuantity"   Text="Quantity must be a number!"   Operator="DataTypeCheck"   Type="Integer"   Runat="Server" /> <p> Customer State: <br> <asp:DropDownList   id="dropState"   Runat="Server">   <asp:ListItem Text="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> 

The C# version of this code can be found on the CD-ROM.

Figure 6.3. A simple order form.

graphics/06fig03.jpg

When all the information is entered into the form, and the Place Order button is clicked, the Button_Click subroutine executes. This subroutine creates an instance of the BizObject component and passes all the form information to the component's CheckOrder() method.

The business component is contained in Listing 6.9.

Listing 6.9 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( "Invalid Quantity!" )     End If     If State = "California" And Product="Hair Dryer" Then       Throw New ArgumentException( "Californians cannot own Hair Dryers!" )     End If     If State = "Washington" Then       unitPrice += unitPrice * .06     End If     Dim myDataObject As New DataObject     myDataObject.SaveOrder( customer, product, unitPrice, quantity, state )   End Sub End Class End Namespace 

The C# version of this code can be found on the CD-ROM.

The business component contains all the business rules for the application. The component encapsulates the following rules:

  1. The product quantity must be greater than 0 and less than 100.

  2. People who live in California cannot buy hair dryers.

  3. People who live in Washington must pay an additional 6% sales tax.

If an order fails either of the first two requirements, an exception is raised. The error is caught in the OrderEntry.aspx page and displayed in a Label control. For example, Figure 6.4 displays what happens when you try to order more than 100 products.

Figure 6.4. Raising an exception in the order form.

graphics/06fig04.jpg

NOTE

Exceptions and Try...Catch blocks are discussed in detail in Chapter 18, "Application Tracing and Error Handling."


If an order satisfies all the business rules, it is passed to the SaveOrder() method of the DataObject component, which saves the order to disk. This component is contained in Listing 6.10.

Listing 6.10 DataObject.vb
 Imports System Imports 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 

The C# version of this code can be found on the CD-ROM.

Remember that you must compile both the BizObject and DataObject components and copy them to your application's /BIN directory before you can use them. The order of compilation is important. Because the BizObject component refers to the DataObject component, you must create the DataObject component first. You can compile this component by using the following statement (executed from a DOS prompt):

 
 vbc /t:library DataObject.vb 

After you copy the DataObject.dll file to your application's /BIN directory, you can compile the BizObject component by using the following statement:

 
 vbc /t:library /r:DataObject.dll bizObject.vb 

Notice the /r option used when compiling the BizObject component; it references the DataObject.dll file. If you don't include this reference, you receive the error User-defined type not defined: DataObject .



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net