日本国产亚洲-日本国产一区-日本国产一区二区三区-日本韩国欧美一区-日本韩国欧美在线-日本韩国欧美在线观看

當前位置:雨林木風下載站 > 技術開發(fā)教程 > 詳細頁面

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

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

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

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

模型-視圖-控制器分離的重構

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

視圖的實現(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語句,其它的代碼調用一些方法來完成與數(shù)據(jù)庫的交互。

控制器

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

主站蜘蛛池模板: 国产浮力第一页 | 精品一区 二区三区免费毛片 | 精选国产门事件福利在线观看 | 精品日本亚洲一区二区三区 | 久久精品中文字幕极品 | 亚洲视频一区二区三区四区 | 91看片网| 精品72久久久久久久中文字幕 | 久久机热这里只有精品无需 | 中文字幕在线播放第一页 | 夜色爽爽| 国产精品一区二区免费福利视频 | aiai视频在线观看 | 国产淫语对白在线视频 | 亚洲精品美女久久久久网站 | 国产免费一区二区在线看 | 日日夜夜精品视频 | 韩国日本一级毛片免费视频 | 国产精品ⅴ视频免费观看 | 亚洲在线观看一区二区 | 不卡视频免费在线观看 | 中文字幕在线亚洲 | 亚洲国产中文字幕 | 人人精品久久 | 久热中文字幕在线 | 精品久久久久久久久久中文字幕 | 国产视频精品免费 | 久久爱www成人 | 国产三级国产精品国产国在线观看 | 久久精品一区二区三区四区 | 香港aa三级久久三级不卡 | 国内精品久久国产 | 欧美激情毛片裸推荐 | 国产99久9在线视频 国产99久久 | 美女拍拍拍免费视频观看 | 99久久精品费精品国产一区二 | 日本精品久久久一区二区三区 | 成人欧美一区在线视频在线观看 | 操久久 | 欧美成在线观看 | 久久亚洲综合网 |