MVC三層架構在ASP中的應用

來源:互聯網
上載者:User

前段時間讀了不少關於MVC的文章,試著在ASP中應用了一下,發現對於小程式,代碼量會大幅度增加,但是邏輯清晰,資料封裝很合理,以前需要仔細規劃的代碼複用竟然成了理所當然的事情。

所謂MVC,即Model(模型),View(視圖),Control(控制)三層架構。各部分各司其職,Model即底層構架,包含與資料庫連接的部分,View即UI展示部分,與使用者直接打交道,Control即控制層,負責接收View的請求並做適當預先處理後交由Model處理,然後接收Model傳回值並做格式化處理返回View層。簡單地說,Model直接與系統底層如資料庫等打交道,而不管資料出去後如何用,View只負責請求和展示資料,而不管詳細流程,Control分別與View和Model打交道,並負責資料的驗證、格式化等工作。

 

寫了一個擷取使用者資料的ASP中應用:

先寫Model層,定義資料庫地址,開啟串連資料庫,擷取記錄。

在寫Model層之前定義了兩個類:Cls_Config整站設定,包括資料庫地址;Cls_DatabaseModel,負責開啟關閉資料庫連接。

 

Cls_ConfigModel.asp代碼如下:

 

<%

Class Cls_ConfigModel

    Private i_datapath,i_sitename,i_sitedomain,i_mastermail

 

    Private Sub Class_Initialize()

        i_datapath="mytdata/mytdata2.mdb"

        i_sitename="中國地質大學(武漢)民族樂團官方網站"

        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

%>

 

 

Cls_DatabaseModel.asp代碼如下:

 

<!--#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是一個緩衝類,這裡緩衝了資料庫地址。

 

使用者Model層Cls_UserModel.asp代碼如下:

 

<!--#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

%>

 

 

Model層構造SQL語句並查詢,然後將記錄集存入UserInfo數組,供Control層調用。

 

Control層Cls_User.asp代碼如下:

 

<!--#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

%>

 

Cls_User類中只定義了兩個屬性:i_uid,i,uname。當View層給i_uid或者i_uname賦值的時候調用Model層查詢使用者資訊,然後用Fillinfo函數填寫屬性供View層調用。

 

View層UserInfo.asp代碼如下:

 

<!--#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>顯示使用者資訊</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>

 

這樣調用邏輯就非常清晰了。

 

試著加了一個迴圈(注釋掉的代碼),查詢100個使用者大約要3秒左右,看來頻繁串連資料庫開銷確實很大。

另外,迴圈的時候變數不能用i,不然只能顯示第一個使用者資訊,不知何故。

 

三層構架的大致思想基本算是掌握了,緩衝應用還不夠自如,模板類的實現還沒開始。任重道遠。

 

PS:今天大年三十,祝所有人幸福安康! 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.