Building an ASP application with a three-tier structure

Source: Internet
Author: User
Tags exit file system functions key variables reference require trim
Program This article describes the concept and advantages of a three-tier architecture application, and describes how to build a three-tier structure of ASP applications with an example.


What is the disadvantage of ASP application of two-tier structure
In the field of Browser/server application development, Microsoft's iis/asp is rapidly catching on with its powerful capabilities, good scalability, and consistency with other Microsoft products. It enables a programmer with VB/VC experience to quickly become a web programmer and develop applications that look very professional. However, the ASP has a natural disadvantage, that is, ASP code and HTML code is mixed together, ASP programmers need to consider dealing with the database, need to care about how to work with HTML, and sometimes need to use ASP directly generate HTML code. The result is that when the logic of the program is complex enough,. asp source files are very long, and regardless of customer interface changes, or business logic changes (for example, in the examination system, "qualified" standards may be from 60 points even if qualified, to enter the top 100 to be qualified), All need to make changes to the. asp file, and the change in business logic is likely to require many files to be changed.


The concept of two or three-layer structure
In the traditional Client/server application, there are also the same problems, the application of multi-layer structure is based on the summary of C/s structure, and has been extended to the B/s Application development field. The application will be divided into three layers (can have more layers, but the three layer is most common): User interface layer, business logic layer, database layer. The user interface layer is responsible for processing user input and output to the user, but it is not responsible for explaining its meaning (for efficiency reasons, it may be validated before the user input is transmitted upward), which is typically developed using front-end tools (vb,vc,asp, etc.); The business logic layer is the upper and lower layer of the bond, It establishes the actual database connection, generate SQL statements to retrieve or update the database based on the user's request, and return the result to the client, which typically exists in the form of a dynamic-link library and registers with the server's registry (Registry), and its interface with the client communicates with a specific component standard ( such as Com,corba), can be developed with any tool that supports this standard, and the database layer is responsible for the actual data storage and retrieval. With such a structure, the above problem solved: or take the test system in the eligibility criteria for example, in the client all need to display the list of qualified personnel, call such a function getqualifiedlist, as to how this function is written, how to deal with the database, It doesn't matter what kind of database you're visiting. (You must have had the experience that a SQL statement that runs well on a database system is sometimes changed to a different database system); Implement this getqualifiedlist function in the middle-tier DLL, if the user's definition of "qualified" changes, only need to modify the function on it, as long as the function of the entry parameters and return content unchanged, the client does not need to make any changes. Here, we see one of the features of object-oriented programming, the advantages of encapsulation, this is especially useful when developing large applications-we can split the developers into two groups, one for the development interface layer and the other for the development of the business logic layer, which can be developed in parallel as long as the agreed function interfaces are in place. Instead of having to do that, the rest of the work has to wait until the previous work is completed before it can begin. Of course, such a development model requires good project coordination and documentation support.


You might ask, if I put some of these functions in a separate file and then include it in the place where I need to call it, wouldn't that be the same? First, this method is inefficient, no matter how many files you scatter these functions into, when you need to call one of them, it always contains some functions that are not actually needed, which undoubtedly adds to the burden on the server, especially for Web applications that require higher server performance. The DLL only calls into memory when it is needed and only calls into the desired function. and multiple application instances can share the same DLL instance; Second, imagine an employee with 20 attributes (work number, name, age, sex ...), now given a certain work number, request to return all information about this employee. At this point, if you simply use a function, you can define only 20 global variables, change the values in the function, or define a function with 20 parameters (by reference). Obviously, the first approach is cumbersome and once a property is added, one method needs to change the function interface. In an object, it includes both member methods (functions and procedures), and member properties. If we take the object's approach, we just need to change the object's properties in the function, and we can directly reference the changed object property value outside the function. This method is somewhat similar to the first method, but 1. property values do not need to be described separately from the function; 2. These property values belong only to the object, and the object-independent code does not inadvertently change the value of the property; 3. Once the object is freed, the values are released together.


Third, how to develop three-tier structure of ASP applications
ASP has a good extensibility, we access the database, when the use of the ADO object, access to files, the use of File system objects (FSO), in fact, the program is already a three-tier structure of the application, but because the use of built-in objects and to realize it. These objects all follow the Com/activex interface, so the objects we develop ourselves also follow this interface. Here's how we can create our own three-tier ASP application, as an example of the "qualifying" standard mentioned above.


1. The following database tables are established in the database system:


Employee:emplid char (5) NOT NULL,
Name Char (a) NOT NULL,
Gender char (1) NOT NULL,
Score int NOT NULL
This table stores employee information and exam results, for simplicity, here only includes work numbers, names and sex three items, and only one exam, emplid as the primary key.


2. Building a dynamic link library
Start VB (here in VB, for example, you can use any of your favorite development tools to support the ActiveX interface), a new project, the project type is an ActiveX DLL. Create a new class in the project, named employee. You can add attributes and methods to the Class builder visually, or you can edit them manually. First fill in the Emplid property as follows:
Private Msemplid As String
Property Let Emplid (Semplid as String)
Msemplid=semplid
End Property
Property Get Emplid () As String
Emplid=msemplid
End Property


Generally speaking, each attribute should have a properties let and Property Get two methods, which are called when assigning values to properties and reading property values. If a property is only assigned and is never read (this happens most of the property on the primary key of the corresponding database table), the properties get method can be omitted. The Property Let method cannot be omitted. You can then build the Name,gender and score three properties by imitating the above procedure. Then create the following methods:


Public Sub Create (Emplid as String)
Dim conn As New Connection
Dim rs As New Recordset
Dim sql As String
"Suppose," create a DSN in the Control Panel, the ConnectionString property
"Can also be dsn-less string
Conn. Connectionstring= "Dsn=dsnname;uid=username;password=pwd"
Conn.Open
Sql= "SELECT * from Employee where emplid=" "& Emplid &" ""
With RS
. Open sql,conn,1,3
If. EOF and. BOF Then
Exit Sub
Else
Msemplid=trim (. Fields ("Emplid"))
Msname=trim (. Fields ("Name"))
Msgender=trim (. Fields ("Gender"))
Msscore=. Fields ("Score")
End If
. Close
End With
Set rs=nothing
Conn.close
Set conn=nothing
End Sub
Here according to Emplid create an Employee object, note that the value in the database is assigned to three private variables, rather than directly assign to the attribute, if you step into the debugging will find that the Msemplid assignment will call the property Let Emplid, that is to assign values to the attribute.
Next, we create a class employees and add the following methods:
Private colqualifiedlist as New Collection
Private Mncurrentindex As Integer
Public Sub getqualifiedlist ()
Dim conn As New Connection
Dim rs As New Recordset
Dim sql As String
"Suppose," create a DSN in the Control Panel, the ConnectionString property
"Can also be dsn-less string
Conn. Connectionstring= "Dsn=dsnname;uid=username;password=pwd"
Conn.Open
Sql= "Select Emplid from Employee where score>=60 order by Score Desc"
With RS
. Open sql,conn,1,3
If. EOF and. BOF Then
Exit Sub
Else
Do as not. EOF
Dim oemployee As New Employee
Oemployee.crea



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.