六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

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

[摘要]在ASP.NET中實現Model-View-Controller模式(四)模型-視圖-控制器分離的重構為了解決上面所遺留的問題,你必須將模型與控制器角色分離。視圖的實現代碼與前部分相同。模型下面的代...
在ASP.NET中實現Model-View-Controller模式(四)

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

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

視圖的實現代碼與前部分相同。

模型

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

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;

}



現在的代碼只依賴于數據庫,這個類是一個優秀的數據庫的通道,它持有訪問表或視圖的所用的SQL語句,其它的代碼調用一些方法來完成與數據庫的交互。

控制器

這種重構方式利用代碼隱藏機制,在負責數據訪問的模型部分相對獨立的情況下,由控制器負責事件與方法的控制工作。模型的任務很明確的,它僅返回一個DataSet對象。這種實現方式就像視圖代碼一樣,不依賴于數據是如何從數據庫中返回的。

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

}



主站蜘蛛池模板: 伊伊人成亚洲综合人网香 | 日韩理论片西瓜影音 | 色综合天天综合网国产成人网 | 日本高清在线观看视频www | 亚洲波多野结衣日韩在线 | 天天艹综合 | 欧美香蕉人人人人人人爱 | 欧美在线视频免费播放 | 日韩高清在线不卡 | 综合网婷婷 | 日本高清免费网站 | 午夜影院18| 婷婷色六月 | 欧美亚洲综合另类 | 天色噜噜噜噜 | 午夜影院黄色片 | 日韩精品福利在线 | 伊人久久婷婷丁香六月综合基地 | 午夜免费福利网站 | 在线亚洲精品 | 啪啪福利视频 | 天天射天天干 | 日韩黄色精品 | 天天狠天天天天透在线 | 伊人365| 又粗又硬又爽的三级视频 | 在线观看91精品国产下载 | 日本三区四区免费高清不卡 | 中文字幕欧美成人免费 | 欧美午夜一区二区福利视频 | 一级黄色在线 | 日本成人精品 | 水蜜桃一区一区三全集 | 日韩欧美国产成人 | 最近2019中文字幕大全视频一页 | 天天狠狠色噜噜 | 亚洲成人播放 | 日韩色中色 | 日本中文字幕不卡 | 青春草在线观看视频 | 四虎国产精品视频免费看 |