I have read a lot of articles about MVC some time ago and tried to apply it in ASP. I found that the amount of code for small programs will increase significantly, but the logic is clear and the Data encapsulation is reasonable, code reuse that was previously well planned has become a matter of course.
The so-called MVC, namely model, view, control, three-layer architecture. Each part performs its own duties. The model is the underlying architecture, which includes the parts connected to the database. The view is the UI display part, which deals with users directly, and the control layer is the control layer, receives view requests, pre-processes them properly, and submits them to the model for processing. Then, receives model return values and formats them to the view layer. Simply put, the model directly deals with the underlying system, such as a database. No matter how much data is used after the data goes out, the view is only responsible for requesting and displaying data, regardless of the detailed process, control deals with views and models respectively, and is responsible for data validation and formatting.
I wrote an ASP application for getting user information:
First, write the model layer, define the database address, open the database connection, and obtain records.
Before writing the model layer, two classes are defined: cls_config full-site settings, including the database address and cls_databasemodel, which enable and disable the database connection.
The cls_configmodel.asp code is as follows:
<%
Class cls_configmodel
Private I _datapath, I _sitename, I _sitedomain, I _mastermail
Private sub class_initialize ()
I _datapath = "mytdata/mytdata2.mdb"
I _sitename = "China University of Geosciences (Wuhan) National Orchestra Official Website"
I _sitedomain = "www.cugmyt.cn"
I _mastermail = "master@cugmyt.cn"
End sub
Public property get datapath
Datapath = server. mappath (I _datapath)
End Property
Public property get sitename
Sitename = I _sitename
End Property
Public property get sitedomain
Sitedomain = I _sitedomain
End Property
Public property get mastermail
Mastermail = I _mastermail
End Property
End Class
%>
The cls_databasemodel.asp code is as follows:
<! -- # Include file = "cls_configmodel.asp" -->
<! -- # Include file = "../public/cls_cache.asp" -->
<%
Class cls_databasemodel
Private I _config, I _cache, I _datapath
Private I _conn
Private sub class_initialize ()
Set I _cache = new cls_cache
If I _cache.getcache ("Config", "datapath") = "" then
Set I _config = new cls_configmodel
I _datapath = I _config.datapath
Call I _cache.setcache ("Config", "datapath", I _datapath)
Else
I _datapath = I _cache.getcache ("Config", "datapath ")
End if
Set I _cache = nothing
End sub
Private sub class_terminate ()
Set I _config = nothing
End sub
Public property get Conn
Set conn = I _conn
End Property
Public Function openconn ()
Set I _conn = server. Createobject ("ADODB. Connection ")
I _conn.open "provider = Microsoft. Jet. oledb.4.0; Data Source =" & I _datapath & "; user id = admin; Password =; Jet oledb: Database Password ="
End Function
Public Function closeconn ()
Set I _conn = nothing
End Function
End Class
%>
Cls_cache is a cache class, where the database address is cached.
The user model layer cls_usermodel.asp code is as follows:
<! -- # Include file = "cls_databasemodel.asp" -->
<%
Class cls_usermodel
Private I _database, I _ SQL, I _rs
Public userinfo ()
Private sub class_initialize ()
Set I _database = new cls_databasemodel
End sub
Private sub class_terminate ()
Set I _database = nothing
End sub
Public Function getuserinfo (keytype, key)
Select case keytype
Case "uid"
I _ SQL = "select * from [myt_user] Where userid =" & Key
Case "uname"
I _ SQL = "select * from [myt_user] Where username = '" & Key &"'"
End select
I _database.openconn ()
Set I _rs = I _database.conn.execute (I _ SQL)
If not I _rs.eof then
Redim userinfo (I _rs.fields.count)
For I = 0 to I _rs.fields.count-1
Userinfo (I) = I _rs (I)
Next
Else
Redim userinfo (2)
Userinfo (0) = 0
Userinfo (1) = "guest"
End if
I _rs.close
Set I _rs = nothing
I _database.closeconn ()
End Function
End Class
%>
The model layer constructs and queries SQL statements, and then stores the record set in the userinfo array for the control layer to call.
The control layer cls_user.asp code is as follows:
<! -- # Include file = "../model/cls_usermodel.asp" -->
<%
Class cls_user
Private I _uid, I _uname, I _user
Private sub class_initialize ()
Set I _user = new cls_usermodel
End sub
Private sub class_terminate ()
Set I _user = nothing
End sub
Public property get uid
Uid = I _uid
End Property
Public property get uname
Uname = I _uname
End Property
Public property let UID (userid)
I _uid = userid
Call I _user.getuserinfo ("uid", userid)
Fillinfo ()
End Property
Public property let uname (username)
I _uname = Username
Call I _user.getuserinfo ("uname", username)
Fillinfo ()
End Property
Private function fillinfo ()
I _uid = I _user.userinfo (0)
I _uname = I _user.userinfo (1)
End Function
End Class
%>
The cls_user class only defines two attributes: I _uid, I, and uname. When the view layer assigns values to I _uid or I _uname, the model layer is called to query user information, and the fillinfo function is used to fill in attributes for the view layer to call.
The userinfo. ASP code of the view layer is as follows:
<! -- # Include file = "control/cls_user.asp" -->
<! 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>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> Display User Information </title>
</Head>
<Body>
<%
Set User = new cls_user
'For IIi = 1 to 100
User. uid = 2
Response. Write user. uid
Response. Write user. uname
'Next
Set User = nothing
%>
</Body>
</Html>
In this way, the call logic is very clear.
I tried to add a loop (commented out code) and it took about 3 seconds to query 100 users. It seems that the overhead of frequently connected databases is indeed high.
In addition, I cannot be used for the variable during the loop. Otherwise, only the first user information can be displayed, somehow.
The general idea of the three-tier architecture is basically mastered, And the cache application is not comfortable enough, and the implementation of the template class has not started yet. There is a long way to go.
PS: I wish everyone happiness and well-being!