• <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)

        更新時(shí)間: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ù)庫,這個(gè)類是一個(gè)優(yōu)秀的數(shù)據(jù)庫的通道,它持有訪問表或視圖的所用的SQL語句,其它的代碼調(diào)用一些方法來完成與數(shù)據(jù)庫的交互。

        控制器

        這種重構(gòu)方式利用代碼隱藏機(jī)制,在負(fù)責(zé)數(shù)據(jù)訪問的模型部分相對(duì)獨(dú)立的情況下,由控制器負(fù)責(zé)事件與方法的控制工作。模型的任務(wù)很明確的,它僅返回一個(gè)DataSet對(duì)象。這種實(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

        }

        溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!

        本類教程下載

        系統(tǒng)下載排行

        主站蜘蛛池模板: 全黄大全大色全免费大片| 亚洲ts人妖网站| 东北美女野外bbwbbw免费| 国产人成免费视频| 男女超爽视频免费播放| 久久久www成人免费毛片 | 曰批视频免费30分钟成人| 亚洲最大中文字幕| 久草在视频免费福利| 亚洲熟伦熟女专区hd高清| 国产精品无码一区二区三区免费| 婷婷亚洲综合一区二区| 亚洲第一网站男人都懂| 丝袜捆绑调教视频免费区| 亚洲国产精品国自产拍电影| 国内精自视频品线六区免费| 亚洲综合色婷婷在线观看| 国产免费怕怕免费视频观看| 男女交性无遮挡免费视频| 亚洲精品蜜桃久久久久久| 91久久精品国产免费直播| 国内精品久久久久影院亚洲| 国产免费av片在线播放| 中国好声音第二季免费播放| 亚洲精品动漫在线| 国产精品国产免费无码专区不卡 | 亚洲美女激情视频| 影音先锋在线免费观看| 人妻仑乱A级毛片免费看| 亚洲人成影院在线| 日本久久久免费高清| 国产在线观看免费视频软件| 亚洲国产日韩在线成人蜜芽| 免费成人午夜视频| 一区二区免费视频| 亚洲av成人无码网站…| 亚洲国产精品热久久| 成年女人永久免费观看片| 免费在线看黄的网站| 国产精品亚洲va在线观看| 亚洲人成在线观看|