MVC的ASP.NET实现
为了解释如何在ASP.NET中实现Model-View-Controller模式,并说明在软件中分离模型、视图和控制器角色的好处,下面的示例将一个没有分离所有三个角色的单页面解决方案重构为分离这三个角色的解决方案。示例应用程序是一个带有下拉列表的网页(如栏下图所示),该页面显示了存储在数据库中的记录。
 图5
利用Microsoft Visual Studio(r) .NET开发系统的代码隐藏功能,可以很容易地将表示(视图)代码与Model-Controller代码分离开来。每个ASP.NET页都有一种机制,这种机制允许在单独的类中实现从网页调用的方法。该机制是通过Visual Studio .NET提供的,它有许多优点,例如Microsoft IntelliSense(r)技术。当您使用代码隐藏功能来实现网页时,可以使用IntelliSense来显示网页后面的代码中所使用的对象的可用方法列表。IntelliSense不适用于.aspx页。与此同时,为了展现Model-Controller的分离,对于数据库操作提取了DatabaseGateway,这样就实现了三者的完整分离。
视图
<%@ Page language="c#" Codebehind="Solution.aspx.cs" AutoEventWireup="false" Inherits="Solution" %> <html> <head> <title>解决方案</title> </head> <body> <form id="Solution" method="post" runat="server"> <h3>录音</h3> 选择录音:<br/> <asp:dropdownlist id="recordingSelect" runat="server" /> <asp:button id="submit" runat="server" text="Submit" enableviewstate="False" /> <p/> <asp:datagrid id="MyDataGrid" runat="server" width="700" backcolor="#ccccff" bordercolor="black" showfooter="false" cellpadding="3" cellspacing="0" font-name="Verdana" font-size="8pt" headerstyle-backcolor="#aaaadd" enableviewstate="false" /> </form> </body> </html> | 模型
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; } | 控制
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: 此调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); }
/// <summary> /// 设计器支持所必需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.submit.Click += new System.EventHandler(this.SubmitBtn_Click); this.Load += new System.EventHandler(this.Page_Load);
} #endregion } | 以上示例简单的说明了ASP.NET的MVC实现,在实际项目中,商务逻辑远远不止这样,但是上述的代码展现了一个基本的模型,在增加了代码和复杂度的同时也带来了显而易见的好处,如:模块依赖的降低、代码重复的减少、职责和问题的分离、代码的可测试性等等。
到现在您是否决定了使用Model-View-Controller (MVC)模式来将动态Web应用程序的用户界面组件与业务逻辑分隔开来,要构建的应用程序将以动态方式构造网页,但是目前的页面导航都是基于静态导航的形式。 在更加复杂的应用系统中,如何考虑尽可能避免导航代码的重复,甚至考虑基于可配置的规则来动态确定页面导航,那么Page Controller和Front Controller某种意义来说是对于MVC模式在更加复杂的系统的优化。
|
|