All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
浅谈数据库设计技巧(上)_mssql学习_编程技术
Asp.Net中对Cookie的基本操作_.net资料_编程技术
安全知识:如何隐藏,PHP文件后门的技巧_php资料_编程技术
ASP.NET&Spring.NET&NHibernate最佳实践(四)——第3章人事子系统(1)_[Asp.Net教程]
XP上安装SqlServer2000_[SQL Server教程]
用C#对DBF数据库的操作_.net资料_编程技术
visual c++ MFC调试宏
asp.net在ie里调试_.net资料_编程技术
ubuntu下安装php5+pdo_[PHP教程]
动态修改.Net,StreamReader,Encoding编码_.net资料_编程技术
Reading number is top 10 pictures
美丽的少女1
Japan sexy beauty passion photo
Parking technology is great, that give you the keys can't stolen
漂亮脸蛋魔鬼身材1
美奂绝伦的风景
The world's top ten most beautiful railway station2
Wild animals melee moment of life and death1
中国的阶级现状
[猫扑大杂烩]华东师范墙上看到的捐精告示 15毫升3600元
Exquisite decoration is not paying too much2
Download software ranking
C#COM编程指南
Tram sex maniac 2 (H) rar bag17
Ashlynn Video4
SP4 for SQL2000
Sora aoi, the maid, students' uniforms
SQL2000 For 4IN1
Unix video tutorial13
WebService在.NET中的实战应用教学视频 → 第4集
Sora aoi‘s film--Lust fan wall
Tram sex maniac 2 (H) rar bag7
delv published in(发表于) 2014/1/6 9:11:13 Edit(编辑)
在ASP.NET中实现Model,View,Controller模式(2)_[Asp.Net教程]

在ASP.NET中实现Model,View,Controller模式(2)_[Asp.Net教程]

在ASP.NET中实现Model-View-Controller模式(2)_[Asp.Net教程]

模型-视图-控制器分离的重构

 为了解决上面所遗留的问题,你必须将模型与控制器角色分离。

 视图的实现代码与前部分相同。

 模型

 下面的代码例子使模型角色仅仅依赖于数据库,而不包含任何与视图相依赖的代码。

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

}

///



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

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

///


private void InitializeComponent()

{

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

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

}

#endregion

}


测试

 将模型部分从ASP.NET环境中分离出来能够使模型部分更容易的被测试。在ASP.NET环境中进行测试的话,你必须同时测试很多方面,如HTML代码是否正确,而读取HTML代码的工作是非常烦闷的。将模型部分分离出来,使你能够对模型部分做单独的单元测试。下面是NUnit (http://nunit.org)对模型部分进行单元测试的例子。


using System;

using NUnit.Framework;

using System.Collections;

using System.Data;

using System.Data.SqlClient;

[TestFixture]

public class GatewayFixture

{

[Test]

public void Tracks1234Query()

{

DataSet ds = DatabaseGateway.GetTracks("1234");

Assertion.AssertEquals(10, ds.Tables["Track"].Rows.Count);

}

[Test]

public void Tracks2345Query()

{

DataSet ds = DatabaseGateway.GetTracks("2345");

Assertion.AssertEquals(3, ds.Tables["Track"].Rows.Count);

}

[Test]

public void Recordings()

{

DataSet ds = DatabaseGateway.GetRecordings();

Assertion.AssertEquals(4, ds.Tables["Recording"].Rows.Count);

DataTable recording = ds.Tables["Recording"];

Assertion.AssertEquals(4, recording.Rows.Count);

DataRow firstRow = recording.Rows[0];

string title = (string)firstRow["title"];

Assertion.AssertEquals("Up", title.Trim());

}

}


 结论:

 在ASP.NET中实现MVC模式有如下优缺点:

 优势:

 能够减少依赖。 程序员可以在一个ASP.NET页面中实现所有的代码。单页的实现方式,对于一些小型的且生存周期不长的程序是适用的。但如果想在不断增加的页面间共享代码的话,将代码的不同部分进行分离是非常有效果的。

 能够减少代码的复制。 DatabaseGateway 类中的GetRecordings 和 GetTracks方法能够直接被其它的页面使用,减少了必须将方法的代码拷贝到不同页面的情况。

 能够把不同人员的责任分开。修改页面的外观与修改数据访问的代码所用的技术是不同的,将模型与视图分开能够使负责不同工作的专家协同的工作。

 使性能优化的成为可能 按将系统不同的职责分成不同的类,使性能的优化成为可能。前面的例子中,由于每次请求页面的时都要从数据库中读取数据。因此可在某种情况下将数据缓存,从而提高整个程序的性能。如果不将代码进行分离的话是无法做到的这点的。

 易测试性 将模型与视图相分离使在ASP.NET环境外进行单元测试成为可能。

 缺点:

 增加了代码的数量及复杂度。这个例子在早期单页的实现方式的基础上增加了新的文件和代码,在无形中增加了维护的开销。一旦修改系统的话,会修改所有三种角色的代码。在一些情况下,一个文件中的修改比一些文件中修改要方便。所以在考虑是否使用MVC模式时。这种额外的开销一定要被计算在内,对一些小的程序来说,这种开销是不值得的。


来源:助跑学院





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.