惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
宝玉的分享
宝玉的分享
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
V
Visual Studio Blog
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
D
Docker
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
P
Proofpoint News Feed
L
LangChain Blog
Blog — PlanetScale
Blog — PlanetScale
The GitHub Blog
The GitHub Blog
博客园 - 【当耐特】
Martin Fowler
Martin Fowler

博客园 - 流星石

水晶报表中如何改变报表的背景色、显示行数等。 如何动态生成水晶报表(ASP.NET) C编写的SQL Server 数据库连接通用类库 使用Facade模式分析 DreamWeaver MX 2004中 设为首页和加入收藏的实现 DreamWeaver MX 2004制作树状菜单 DreamWeaver MX 2004利用层进行下拉菜单的制作 Grove---------.NET中的ORM实现 .NET下的加密编程 在ASP.NET中写一个数据层基类-----DbObject 在ASP.NET页面中显示年月日和星期的代码实现 在ASP.NET中实现多文件上传 构建简单的Web Service服务 C#中写COM+组件 数据库操作源代码 asp.net中DataGrid双行跨列表头设计心得 用C#写一个Web自定义日期时间控件 C#.net常用函数和方法集 Together for .net建模入门
在ASP.NET 中实现Model-View-Controller
流星石 · 2005-07-17 · via 博客园 - 流星石

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; 
   } 
控制器:
该重构使用代码隐藏功能来改写模型代码,以适应网页上存在的数据控件,并将控制器转发的事件映射到具体的操作方法。因为此处的模型返回 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: 此调用是 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的优缺点:
优点:
1、降低了依赖性。
2、减少代码重复。
3、分离职责和问题。
4、优化的可能性。
5、可测试性。
缺点:
增加了代码和复杂性。