上次的內(nèi)容說過asp.net和asp的最大區(qū)別在于編程思維的轉(zhuǎn)換,那么我們現(xiàn)在就來看看如何轉(zhuǎn)換編程思想。以前的web編程從cgi(perl)到asp,php,jsp的編程過程都是這樣:美工人員給出頁面原型,編程人員照頁面填空,最后堆起來算完,下次如果原型變動(dòng),那么就再修改程序,這樣業(yè)務(wù)邏輯和html頁面混在一起,可以說是事倍功半。那么,現(xiàn)在有了asp.net,我們應(yīng)該怎么做呢?
讓我們找個(gè)實(shí)際的例子,就拿論壇來說吧,先從頂至下看看它的業(yè)務(wù)邏輯。我們可以把一個(gè)論壇視做一個(gè)對(duì)象,它有自己的屬性和方法,常見的屬性有名稱、貼子數(shù)、用戶數(shù)、版面數(shù)等等,這樣的話,我們就可以這樣來構(gòu)造論壇對(duì)象:
namespace MyOwnClass { using System; using System.Data.SQL ; using System.Data ; //////////////////////////////////////////////////////////////////// // // Class Name : BBS // // Description: 論壇類,構(gòu)造一個(gè)論壇對(duì)象 // // date: 2000/02/03 // /// //////////////////////////////////////////////////////////////// public class BBS { //私有變量 private string m_strTitle ; //bbs名稱 private int m_intForumCount ; //版面數(shù) private int m_intTopicCount ; //貼子數(shù) private int m_intUserCount ; //注冊(cè)用戶數(shù) //屬性 public string Title { get { return m_strTitle ; } }
public int ForumCount { get { return m_intForumCount ; } }
public int TopicCount { get { return m_intTopicCount ; } }
public int UserCount { get { return m_intUserCount ; } }
//構(gòu)造函數(shù) public BBS(string a_strTitle) { // // TODO: Add Constructor Logic here // m_strTitle = a_strTitle ;
//讀取數(shù)據(jù)庫 MyConnection myConn = new MyConnection() ; SQLCommand myCommand = new SQLCommand() ; myCommand.ActiveConnection = myConn ; myCommand.CommandText = "up_GetBBSInfo" ; //調(diào)用存儲(chǔ)過程 myCommand.CommandType = CommandType.StoredProcedure ;
try { myConn.Open() ; SQLDataReader myReader ; myCommand.Execute(out myReader) ; if (myReader.Read()) { m_intForumCount = (int)myReader["ForumCount"] ; m_intTopicCount = (int)myReader["TopicCount"] ; m_intUserCount = (int)myReader["UserCount"] ; } else { throw(new Exception("表或存儲(chǔ)過程不存在")) ; }
//清場 myReader.Close(); myConn.Close() ; } catch(SQLException e) { throw(new Exception("數(shù)據(jù)庫出錯(cuò):" + e.Message)) ; }
} } }
這個(gè)bbs類很簡單,有四個(gè)私有變量,對(duì)應(yīng)四個(gè)只讀屬性,方法只有一個(gè)帶參數(shù)的構(gòu)造函數(shù),作用是從數(shù)據(jù)庫中讀取相應(yīng)的數(shù)據(jù),填充四個(gè)私有變量。類構(gòu)造好了,讓我們看看如何使用,在需要顯示論壇這些屬性的頁面文件里(.aspx)里,構(gòu)造四個(gè)Label,象這樣: <table width=140 cellpadding=4 cellspacing=1 border=0> <tr> <td class=cn> <font color=white>注冊(cè)用戶數(shù):</font> </td> <td> <asp:label id="lblUserCount" runat=Server class=cn></asp:label> </td> </tr> <tr> <td class=cn> <font color=white>貼子總數(shù):</font> </td> <td> <asp:label id="lblTopicCount" runat=Server class=cn></asp:label> </td> </tr> <tr> <td class=cn> <font color=white>版面數(shù):</font> </td> <td> <asp:label id="lblForumCount" runat=Server class=cn></asp:label> </td> </tr> </table> 然后在對(duì)應(yīng)的c#文件里這樣使用:
protected void Page_Init(object sender, EventArgs e) { // // CODEGEN: This call is required by the ASP+ Windows Form Designer. // InitializeComponent();
//初始化頁面對(duì)象 //創(chuàng)建bbs對(duì)象 try { m_objBBS = new BBS("鷹翔山莊論壇") ; } catch(Exception exp) { #if DEBUG Response.Write ("初始化bbs對(duì)象出錯(cuò):" + exp.Message + "<br>") ; return ; #endif//DEBUG Server.Transfer("error.aspx") ; } //論壇名稱 lblBBSName.ForeColor = Color.White ; lblBBSName.Text = m_objBBS.Title ;
//用戶數(shù) lblUserCount.ForeColor = Color.White ; lblUserCount.Text = m_objBBS.UserCount.ToString() ;
//文章數(shù) lblTopicCount.ForeColor = Color.White ; lblTopicCount.Text = m_objBBS.TopicCount.ToString() ;
//版面數(shù) lblForumCount.ForeColor = Color.White ; lblForumCount.Text = m_objBBS.ForumCount.ToString() ; }
看出這樣使用的好處嗎?對(duì),就是業(yè)務(wù)邏輯和html代碼分開,這樣無論頁面原型如何修改,代碼都不需要做絲毫改動(dòng)。bbs對(duì)象構(gòu)造好了,讓我們看看論壇的其他對(duì)象,他們分別是用戶(BBSUser)、版面(Forum)和貼子(Topic) , 我將在下節(jié)的內(nèi)容里詳細(xì)解釋。
|
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!