• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        當(dāng)前位置:雨林木風(fēng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁面

        在ASP.NET中完成MVC模式(4)

        在ASP.NET中完成MVC模式(4)

        更新時間:2022-05-13 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):

        在ASP.NET中實(shí)現(xiàn)Model-View-Controller模式(四)

        模型-視圖-控制器分離的重構(gòu)

        為了解決上面所遺留的問題,你必須將模型與控制器角色分離。

        視圖的實(shí)現(xiàn)代碼與前部分相同。

        模型

        下面的代碼例子使模型角色僅僅依賴于數(shù)據(jù)庫,而不包含任何與視圖相依賴的代碼。

        using System;

        using System.Collections;

        using System.Data;

        using System.Data.SqlClient;



        public class DatabaseGateway

        {

        public static DataSet GetRecordings()

        {

        String selectCmd = "select * from Recording";



        SqlConnection myConnection =

        new SqlConnection(

        "server=(local);database=recordings;Trusted_Connection=yes");

        SqlDataAdapter myCommand = new SqlDataAdapter(selectCmd, myConnection);



        DataSet ds = new DataSet();

        myCommand.Fill(ds, "Recording");

        return ds;

        }



        public static DataSet GetTracks(string recordingId)

        {

        String selectCmd =

        String.Format(

        "select * from Track where recordingId = {0} order by id",

        recordingId);



        SqlConnection myConnection =

        new SqlConnection(

        "server=(local);database=recordings;Trusted_Connection=yes");

        SqlDataAdapter myCommand = new SqlDataAdapter(selectCmd, myConnection);



        DataSet ds = new DataSet();

        myCommand.Fill(ds, "Track");

        return ds;

        }



        現(xiàn)在的代碼只依賴于數(shù)據(jù)庫,這個類是一個優(yōu)秀的數(shù)據(jù)庫的通道,它持有訪問表或視圖的所用的SQL語句,其它的代碼調(diào)用一些方法來完成與數(shù)據(jù)庫的交互。

        控制器

        這種重構(gòu)方式利用代碼隱藏機(jī)制,在負(fù)責(zé)數(shù)據(jù)訪問的模型部分相對獨(dú)立的情況下,由控制器負(fù)責(zé)事件與方法的控制工作。模型的任務(wù)很明確的,它僅返回一個DataSet對象。這種實(shí)現(xiàn)方式就像視圖代碼一樣,不依賴于數(shù)據(jù)是如何從數(shù)據(jù)庫中返回的。

        using System;

        using System.Data;

        using System.Collections;

        using System.Web.UI.WebControls;



        public class Solution : System.Web.UI.Page

        {

        protected System.Web.UI.WebControls.Button submit;

        protected System.Web.UI.WebControls.DataGrid MyDataGrid;

        protected System.Web.UI.WebControls.DropDownList recordingSelect;



        private void Page_Load(object sender, System.EventArgs e)

        {

        if(!IsPostBack)

        {

        DataSet ds = DatabaseGateway.GetRecordings();

        recordingSelect.DataSource = ds;

        recordingSelect.DataTextField = "title";

        recordingSelect.DataValueField = "id";

        recordingSelect.DataBind();

        }

        }



        void SubmitBtn_Click(Object sender, EventArgs e)

        {

        DataSet ds =

        DatabaseGateway.GetTracks(

        (string)recordingSelect.SelectedItem.Value);



        MyDataGrid.DataSource = ds;

        MyDataGrid.DataBind();

        }



        #region Web Form Designer generated code

        override protected void OnInit(EventArgs e)

        {

        //

        // CODEGEN: This call is required by the ASP.NET Web Form Designer.

        //

        InitializeComponent();

        base.OnInit(e);

        }



        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

        this.submit.Click += new System.EventHandler(this.SubmitBtn_Click);

        this.Load += new System.EventHandler(this.Page_Load);



        }

        #endregion

        }

        溫馨提示:喜歡本站的話,請收藏一下本站!

        本類教程下載

        系統(tǒng)下載排行

        主站蜘蛛池模板: 少妇性饥渴无码A区免费 | 亚洲无人区午夜福利码高清完整版| 亚洲五月午夜免费在线视频| 国产在线观看www鲁啊鲁免费| 亚洲成a∧人片在线观看无码| 永久免费看bbb| 国产成人人综合亚洲欧美丁香花 | jzzjzz免费观看大片免费| 亚洲精品无码专区2| 一个人看的免费视频www在线高清动漫 | 亚洲妇女熟BBW| 中文永久免费观看网站| 亚洲成年看片在线观看| 一个人看的在线免费视频| 亚洲欧洲美洲无码精品VA| 一级毛片不卡片免费观看| 亚洲一区中文字幕在线电影网 | 免费萌白酱国产一区二区| 一个人晚上在线观看的免费视频| 亚洲自偷自偷在线制服| 日本免费大黄在线观看| 亚洲色偷拍另类无码专区| 在线日本高清免费不卡| 在线aⅴ亚洲中文字幕| 亚洲 综合 国产 欧洲 丝袜| 两个人日本WWW免费版 | 91亚洲国产成人精品下载| 免费无码AV片在线观看软件| 美女被吸屁股免费网站| 国产亚洲av片在线观看播放| 国产精品视频免费观看| 精品免费AV一区二区三区| 亚洲精品456播放| 久久久久av无码免费网| 国产精品亚洲一区二区三区在线观看 | 久久免费看黄a级毛片 | 13一14周岁毛片免费| 国产成人亚洲综合a∨| 亚洲av日韩av天堂影片精品| 处破痛哭A√18成年片免费| 好男人资源在线WWW免费|