解读ASP.NET 2.0 Internals
扼要翻译一下这篇“内幕”,原文http://msdn.microsoft.com/asp.net/whidbey/default.aspx?pull=/library/en-us/dnvs05/html/internals.asp
Introduction
New features are fun and interesting to learn about, but changes to the core structure of ASP.NET speak louder to developers who really want to master the technology. In this white paper, we will cover how the internal structure of ASP.NET 2.0 has changed since version 1.x.
概述ASP.NET1.0到ASP.NET2.0核心结构的改变
The topics covered in this paper will be of use to performance-minded developers and technical architects seeking to fine-tune(调整, 使有规则) applications. Specifically, we will examine key areas of the code model, compilation, page lifecycle, extensibility, performance, and caching.
这个主题用于关注性能的开发人员和技术架构师调整现在应用程序。并专注于代码模型、编译、页面生存期、可扩展性、性能和缓存等多个方面。
- Code Model代码模型
how an ASP.NET Web page is created now - Coding Models in ASP.NET 1.
Two primary options for developing a Web Form.- code-inline, works very well for simple commands. However, for more complex code, writing code-inline results in difficult to read Web pages that mix presentation (HTML) with functionality (code).
代码内嵌模式只适用于simple commands - code-behind
Business logic and event-handling code could be written in a separate, code-only file known as the code-behind file. The code-behind model links a code-only file with the ASPX file that contains presentation tags. By separating the code from the presentation, development teams could work faster by allowing the designers to work on the presentation file while the developers worked on the code file.
商务逻辑和事件处理程序代码与展示页分开
- code-inline, works very well for simple commands. However, for more complex code, writing code-inline results in difficult to read Web pages that mix presentation (HTML) with functionality (code).
- The difficulties in 1.x 1.x的不足
The primary difficulties with the code-behind model related to the way in which the code-behind file had to be synchronized with the ASPX page. Although the ASPX page inherited from the code-behind file in a programming sense, the two files were actually coupled by a more complex relationship.
这种模型主要不足在于,后台代码文件code-behind file必须与aspx页同步。即使从编程的角度看,ASPX页是从后台代码文件继承而来的,两者之间实际上还有更复杂的关系。 - Inheritance Complexity 继承的复杂性
If controls were added to the ASPX page, new code had to be added to the code-behind file. In other words, despite the inheritance relationship pointing the other way, the ASPX page actually drove the design of the code-behind file.
就是说,一方面尽管ASPX页继承于Code behind file,但实际上ASPX页反过来也制约code-behine file的设计。 - Compilation Complexity 编译的复杂性
All of the code-behind files were compiled into an assembly and stored in the /bin directory of the Web application. The compilation step occurred prior to deploying the application. On the other hand, the ASPX page was compiled at runtime the first time the page was requested. The ASP.NET runtime actually compiled the ASPX page into its own temporary assembly.
ASPX页和code-behind file都需要编译,但时间不一,code-behind file 在部署前编译,而ASPX页则在第一请求它的时间临时编译。
The problem with this process is that the ASPX page can be changed without updating the code-behind assembly. That is, a developer may choose to modify a property or change the type of a control on an ASPX page after deployment, and the code-behind file would not be updated, nor would the application assembly be recompiled.
很明显,ASPX页的控件类型在部署后进行修改话,code-behind file必须重新编译。
- Inheritance Complexity 继承的复杂性
- Coding Models in ASP.NET 2.0 2.0的代码模型
ASP.NET 2.0 continues to offer both the code-inline and code-behind coding models. In terms of the code-inline model, very little has changed except for the way that Microsoft Visual Studio 2005 supports single-file development.
2.0继续支持1.x的两种代码模型,对于内嵌模型,VS2005支持所谓的“single-file development”
In ASP.NET 2.0, the code-behind file is no longer a full implementation of the System.Web.UI.Page class. Instead, the code-behind file is a new construct called a partial class. The partial class contains all of the user-defined code, but omits all of the plumbing(垂直?) and connectivity code that was auto-generated by Visual Studio .NET in ASP.NET 1.x.
2.0里,后台代码不再完全实现页面类了;取而代之是一种新的类—局部类partial class。partial class包含所有用户自定义类,而忽略那些由VS.NET自动生成的连接性垂直代码。
When an ASPX page with a new code-behind file is requested, the ASP.NET 2.0 runtime will actually combine the ASPX page and the partial class into a single class, rather than two separate classes.
现在,当一个被绑定了新的后台代码的ASPX页被请求时,2.0运行时将把ASPX页和局部类组合成单一的类,而不是以前的两个不同的类了。
The partial class uses a new keyword (Expands in Visual Basic, or Partial in C#) to indicate that the code in the class should be merged with another class at runtime. Similarly, the ASPX page uses a new directive called compilewith to indicate its association with the code-behind file.
局部类使用新关键字(Expands in Visual Basic, or Partial in C#)来在运行时合关局部类,同样,ASPX页也使用新的指示命令compilewith 来关联后台代码。 - Comparing Code-behind Files 两种Code-behind file的比较
- 1.x
You know that Visual Studio inserts auto-generated control declarations and initialization code. This auto-generated code is a direct result of the bi-directional synchronization between the code-behind file and the ASPX file.- namespace WebApplication1
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender,
System.EventArgs e) { }
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
The auto-generated code not only defines the label (bold line), it also declares a new event (page load) and automatically wires the event to an auto-generated method wrapper (Page_Load()).
1.x里,在ASPX页里添加一个LABEL控件,Code-bind在自动产生一条该控件的声明,从而两者间产生了双向同步问题,前面已经说过,此外, - namespace WebApplication1
- 1.x
- Coding Models in ASP.NET 1.
to be continued……