Talking about how to build a three-layer architecture ASP application

zhaozj2021-02-11  250

With the in-depth and development of internet applications, the application model of the three-layer architecture has also been favored by more people. This article describes the concept and advantages of the three-layer structure application, and combines an example of how to build a three-story ASP application.

I. What are the shortcomings of the ASP application of the two-layer structure in the Browser / Server application development field, Microsoft's IIS / ASP is quickly popular with its powerful features, good extension capabilities, and the consistency with other Microsoft products. . It enables a programmer with VB / VC experience, soon become a web programmer, developed very professional applications. However, ASP has a natural disadvantage that the ASP code and HTML code are mixed. The ASP programmer needs to consider dealing with the database, you need to care about how to cooperate with HTML, sometimes you need to generate HTML code directly with ASP. As a result, when the program logic is complex, .asp source file is very long;, regardless of the customer's change, it is still a change in the user interface (for example, in the test system, "qualified" standard may range from 60 Even if you have a qualified, it is necessary to change the top 100. It is necessary to change the .asp file, and the change of business logic is likely to change a lot of files.

Second, the concept of the three-layer structure in the traditional client / server application, there is also the same problem, the application of multi-layer structure is generated on the sum of C / S structures, and has expanded to B. / S application development field. It is about to be divided into three layers (more layers, but the three most common): user interface layers, commercial logic, database layers. The user interface layer is responsible for handling the user's input and outputs to the user, but it is not responsible for explaining its meaning (for efficiency considerations, it may be legitimate verification before the user input), this layer usually uses the front-end tool ( VB, VC, ASP, etc.) development; the business logic layer is the upper and lower lines of bonds. It establishes a practical database connection. Generate a SQL statement to retrieve or update the database according to the user's request, and return the result to the client, this layer usually In the form of a dynamic link library, register into the server's registry (registry), which complies with a particular component standard (such as COM, CORBA) with the interface interface, and can be developed with any tool supporting this standard. The database layer is responsible for actual data storage and retrieval. With such a structure, the above problem is solved: or the qualified standard in the test system as an example, all where the client needs to display the list of qualified personnel, call such a function getqualifiedList, so that this function is written, how to deal with the database What databases that are accessed have nothing to do with them (you must have such experiences, running a good SQL statement on a database system, sometimes changing to another database system must be modified); in the middle This getQualifiedList function is implemented in the layer DLL. If the user is defined for "qualified", it is only necessary to modify this function, as long as the entrance parameters of this function are constant, the client does not need to make any changes in the client. Here, we have seen the advantages of one of the packaged features of the object-oriented programming, and this is especially useful when developing large applications - we can divide the developers into two groups, a group responsible for the development of the interface, another group is responsible Develop business logic layers, both sides can develop in advance, and can be developed in parallel, without having to wait until the previous work must be completed before the work is completed. Of course, such development models require good project coordination and documentation.

You may ask if I put these functions in a separate file, then contain it in the place you need to call it, don't you achieve the purpose? First, this method is not high, no matter you How many files are dispersed, when you need to call one of them, will always contain a function that actually does not need, which undoubtedly aggravates the server's burden, which is especially true for web applications with higher server performance requirements. And the DLL is only transferred in memory and only the required function is required, and multiple application instances can share the same DLL instance; second, ideas for one employee, 20 attributes (labor number, name, age, Sex ...), now give a certain number of job numbers, require all information to return this employee. At this point, if you use a function, you can only define 20 global variables, change these variables in the function, or define a function with 20 path parameters. Obviously, the first method is very troublesome and once a method will need to change the function interface after an attribute is added. In an object, both member methods (ie functions, and procedures), as well as members attributes. If we use objects, you only need to change the properties of the object in the function, and you can directly reference the changed object attribute value outside of the function. This method has some similar to the first method, but the attribute value does not have to be explained instead of the function; 2. These attribute values ​​are only objects, and the code-independent code does not unintentionally change the attribute value; 3. Once the object is released These values ​​will be released together. Third, how to develop the ASP application ASP of the three-layer structure has good expansion, when we access the database, when the ADO object is used, when accessing the file, the file system object (FSO) is used, in fact, the program is already three The application of the layer structure is only aware of the use of built-in objects. These objects follow the COM / Activex interface, so the object we have developed should also follow this interface. Below, we have described the ASP application of how to create its own three-layer structure as an example, showing the "qualified" standard mentioned above. 1. Established the following database table in the database system: Employee: Emplid char (5) Not null, name char (10) Not null, Gender Char (1) Not null, Score Int Null This table Stores employee information and test scores, For the sake of simplicity, there is only three item numbers, names, and gender, and only one test, Emplid is the primary key.

2, establish a dynamic link library

Start VB (here VB as an example, you can use any development tool development you like to support ActiveX interface), new project, engineering type is ActiveX DLL. Create a category in the project, named EMPLOYEE. You can fill the properties and methods in the Class Builder visualize, or can be manually edited directly. First filling 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 property should have a Property Let and Property Two methods of GET, they are called when assigning values ​​and read attribute values ​​to attributes. If an attribute is only assigned, it is never read (this happens on the properties of the primary key of the corresponding database table), then the Property GET method can be omitted. The Property Let method cannot be omitted. You can build three attributes of Name, Gnder, and Score on the previous program. And then create the following method: Public Sub Create (EMPLID as string) dim conn as new Connection dim rs as new Recordset dim sql as string 'Suppose that you create a DSN in the control panel, the connectionstring property' can also be dsn-less string CONN.CONNATIONSTRING = "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 kil = 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 you can create an Employee object according to EMPLID, pay attention to the value in the database is assigned to three private variables instead of directly assigning attributes. If you are single-step debugging, you will find that you will assign the property let emplid to the msemplid, which is assigned to the attribute.

Here we create a class Employees, and fill up as follows: 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 that you 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 while not .eof dim oEmployee as new Employee oEmployee.Create trim (.Fields (" EMPLID ")) colQualifiedList.Add oEmployee SET OEMPLOYEE = NOTHING LOOP End if .close End with set rs = Nothing conn.close set conn = Nothing End Sub First, please note the syntax of the Creative class instance in VB Dim Oemployee as new Employee, will be seen, create classes in ASP The syntax of the instance is different. This method retrieves the score greater than the employee number equal to 60, and creates an Employee object accordingly, add this object to the private collection object. The following two functions through the elements of the set: Public Function GetFirst () as Employee if colQualifiedList.count> 0 then mnCurrentIndex = 1 set GetFirst = colQualifiedList.Item (1) else set GetFirst = nothing end if End Function Public Function GetNext () as Employee mnCurrentIndex = mnCurrentIndex 1 if mnCurrentIndex> colQualifiedList.count then set GetNext = nothing else set GetNext = colQualifiedList.Item (mnCurrentIndex) End if End Function perhaps you will say, why not take a collection of declarations Public, so that ASP is not possible Directly referenced? Indeed, so that it is also available, and the programming is also simpler, but so that this is destroyed the principle of encapsulation.

Because the data is fully stored in the business logic layer, it is independent of the user interface layer. It is assumed that one day you have given the design to store the data in the design, and use the array or record set (RECORDSET) to store , Then you only need to modify both GetFirst and getNext functions, and the user interface layer does not need to be modified. To this class file is created, the project file is used as Test.vbp, and the Make Test.dll option under the File menu will be selected. 3, registration dynamic link library

Start Microsoft Transaction Server on the Web Server (Start - Windows NT Optionpack4 - Internet Information Server - Internet Service Manager), expand the Microsoft Transaction Server - Computer - My Computer - Package Installed, right-click the mouse choose New-- Package - Create Empty Package, Enter the package name Test (here Test is the optional name, not necessarily with the same name with the DLL), OK-INTERACTIVE User-The Current Logon User - finish. Double-click Test - Component, right click to select Component-New-Component-Install New Component (S) - Add file, select the DLL file you just compiled, and MTS will find two classes Employee and Employees in the DLL. To this DLL registration.

4. Write an ASP program

Qualified Employee List

Employee ID Name < / TD> Gender Score <% set Oemployees = Server.createObject ("Test.employees") OEMPLOYEES.GETQUALIFIEDLIST SET OEMPLOYEE = OEMPLOYES.GETFIRST () DO While Not Oemployee Is Nothing%>
<% = OEMPLOYEE.EMPLID%> <% = OEMPLOYE.NAME%> <% = OEMPLOYE.GENDER% > <% = OEMPLOYE.SCORE%> <% set Oemployee = OEMPLOYEES.GETNEXT () loop%> Note Creating the syntax of the instance in ASP SET OEMPLOYEES = Server.createObject ("test.employees"), where Test is the name of the DLL, Employees is the name of the class; of course, if a function returns is an object, Similar to SET OEMPLOYEE = Oemployees.getFirst () This syntax is also possible. At this point, a complete three-layer structure is complete, let us look at the following, if the definition of "qualified" is changed to: Only the results enter the top 100, the program needs to make those modifications. In fact, if your database system is SQL Server, you just change the SQL statement to:

SQL = "SELECT TOP 100 Emplid from Employee Order By Score Desc" is already, even in order to cross the database system compatibility, we only need to modify getQualifiedList: SQL = "Select Emplid from Employee Order by Score DESC" With rs .open sql, conn, 1, 3 if .eof and .bof kiln = 1 do while (not .e (not .e (not .e) and (i <= 100) DIM OEMPLOYEE.CREATE TRIM (.fields) ("Emplid")) colqualifiedList.add Oemployee set Oemployee = Nothing i = i 1 loop end if .close end with ... then recompile the DLL, register, the ASP program does not have to be modified. Fourth, some explanations and precautions 1. Since this example is relatively simple, there is no Create method in the Employee class, and all the information of the employee (IN, Name, Gender, Grade) is read and will be used in the Employee class. Assign the attribute corresponding to the Employee object. However, in practical applications, when the attribute of the Employee object is increased, or the number of tables is increased, the relationship between the tables is complicated, or the method shown in this article is more efficient, the code reuse has a greater opportunity. 2, when the DLL is modified, only after delete it in the MTS, re-register it because each reconfaction is recompiled, re-generates the ID value of the object in the registry. 3. When the class method and function of the belt parameters are called from the ASP, all variable parameters must be converted with the corresponding type conversion function to conversion, otherwise the type does not match the error, because only Variant types in VBScript, it can't Automatically convert into other types.

For example, there is a function definition: public function fun1 (p1 as string, p2 as integer) AS integer End function should call as follows in the ASP program: <% p1 = obj.property1 'Property1 is A String Property P2 = Obj.property2 'Property2 Is An Integer Property A = Obj.fun1 (CSTR (P1), CINT (P2)) A = Obj.Fun1 ("AAA", 10)' Constant Parameter Need Not Be Changd%> The following two ways are error: <% p1 = obj.property1 'Property1 is a string property p2 = obj.property2' Property2 is an integer property a = obj.Fun1 (p1, p2) 'incorrect, p1 and p2 are variant variables p1 = cstr ( P1) P2 = CINT (P2) A = Obj.fun1 (P1, P2) 'STILL INCORRECT%> Here the second way is still erroneous, even after the type conversion, P1 and P2 are still a Variant variable. In VBScript, the data type and type conversion function works only in the expression operation, and the variable only has a type of VARIANT. The above-mentioned theory and practice of multi-layer structures have been concluded, hoping to help your development. There is still a problem here, how to design the class and class members. This involves both the theory of object-oriented programming, as well as certain practical experience. Please refer to the relevant OOP theory book and constantly summarize in practice, I believe you will be able to design your own perfect multi-layer structure.

转载请注明原文地址:https://www.9cbs.com/read-3813.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.031, SQL: 9