自从有了html与http,就有了浏览器与Web服务器,并有了Web应用,最初的交互模式是这样的:
创新互联建站-专业网站定制、快速模板网站建设、高性价比大邑县网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式大邑县网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖大邑县地区。费用合理售后完善,十余年实体公司更值得信赖。
该模式很好地运行了很多年。然而,随着计算机应用的发展,人们越来越不满足于只有静态内容的页面,而由某种机制动态产生html等代码的需求越来越迫切,于是,很多技术就应运而生,ASP.NET就是这样一种技术。从本质上讲,ASP.NET就是一种服务器端动态产生html、css、javascript等浏览器认识的代码的技术。ASP.NET的交互模式如下:
由该图可知,ASP.NET必须解决两大问题,一是如何与Web服务器(一般就是指IIS)进行交互,二是如何根据不同请求产生不同的html等代码。
***个问题,根据IIS版本(5.*,6.0,7.0)的不同,ASP.NET具有不同的进程模式与不同的交互模式,该问题不是本篇要讲述的。一般来说,大家不必关心该问题,而且要了解该问题,又必须清楚IIS各个版本的模型,而各个版本又各有各的不同,因此,我基本不准备讲述这个问题,大家有兴趣的话,可以自行搜索相关资料。
我们来讨论第二个问题,这里首先要说明的是,因为IIS7.0进程模式的变化比较大,我也没去了解IIS7.0的模型,因此,以下讲述及以后讲述将只针对IIS5.*与IIS6.0。我们有理由认为,针对IIS5.*与IIS6.0的讲述一般同样适用于IIS7.0。
先且按下该问题不表,我们来看一段请求玉帝把大象放到冰箱里的代码(为什么不是上帝?因为我中华不归上帝管),请大家先跟着我的思路来,别急,别急。
using System;
namespace ConsoleApplication3
11_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_45_2811_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_45_2811_Closed_Text').style.display='inline';" src="/files/uploadimg/20090423/1047402.gif" align=top>[[2179]][[2180]]{
[[2181]] class Program
[[2182]][[2183]] [[2180]]{
[[2181]] static void Main(string[] args)
[[2182]][[2183]] [[2180]]{
[[2181]] Emperor emperor = new Emperor();
[[2181]] while (true)
[[2182]][[2183]] [[2180]]{
[[2181]] Console.WriteLine("首先给玉帝准备好大象和冰箱。");
[[2181]]
[[2181]] Console.WriteLine("输入大象的名字:");
[[2181]] string elephantName = Console.ReadLine();
[[2181]] Console.WriteLine("输入大象的体重:");
[[2181]] int elephantWeight = int.Parse(Console.ReadLine());
[[2181]] Console.WriteLine("输入冰箱的名字:");
[[2181]] string refrigeratorName = Console.ReadLine();
[[2181]]
[[2181]] Elephant elephant = new Elephant()
[[2182]][[2183]] [[2180]]{
[[2181]] Name = elephantName,
[[2181]] Weight = elephantWeight
[[2184]] };
[[2181]] Refrigerator refrigerator = new Refrigerator()
[[2182]][[2183]] [[2180]]{
[[2181]] Name = refrigeratorName
[[2184]] };
[[2181]]
[[2181]] Context context = new Context()
[[2182]][[2183]] [[2180]]{
[[2181]] Elephant = elephant,
[[2181]] Refrigerator = refrigerator
[[2184]] };
[[2181]]
[[2181]] emperor.ProcessRequest(context);
[[2181]]
[[2181]] Console.WriteLine("是否要玉帝继续把大象关进冰箱里?");
[[2181]] string answer = Console.ReadLine();
if (answer == "n")
break;
}
[[2184]] }
}
class Emperor
{
[[2181]] public void ProcessRequest(Context context)
[[2182]][[2183]] {
[[2181]] Elephant elephant = context.Elephant;
[[2181]] Refrigerator refrigerator = context.Refrigerator;
[[2181]]
[[2181]] //***步,打开冰箱门
[[2181]] refrigerator.IsOpen = true;
[[2181]] Console.WriteLine(string.Format("玉帝打开了 {0} 的冰箱门。", refrigerator.Name));
[[2181]]
[[2181]] //第二步,把大象放进去
[[2181]] refrigerator.Content = elephant;
[[2181]] Console.WriteLine(string.Format("玉帝把大象 {0} 放到冰箱 {1} 里了。", elephant.Name, refrigerator.Name));
[[2181]]
[[2181]] //第三步,关上冰箱门
[[2181]] refrigerator.IsOpen = false;
[[2181]] Console.WriteLine(string.Format("玉帝关上了 {0} 的冰箱门。", refrigerator.Name));
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] class Elephant
[[2182]][[2183]] [[2180]]{
[[2182]][[2183]] public string Name [[2180]]{ get; set; }
[[2182]][[2183]] public int Weight[[2180]]{ get; set; }
[[2184]] }
[[2181]]
[[2181]] class Refrigerator
[[2182]][[2183]] [[2180]]{
[[2182]][[2183]] public string Name { get; set; }
[[2182]][[2183]] public bool IsOpen { get; set; }
[[2181]]
[[2181]] private object m_Content;
[[2181]] public object Content
[[2182]][[2183]] {
[[2182]][[2183]] get { return this.m_Content; }
[[2181]] set
[[2182]][[2183]] {
[[2181]] if (!this.IsOpen)
[[2181]] throw new InvalidOperationException("冰箱门未打开,无法放进东西。");
[[2181]] if (this.m_Content != null)
[[2181]] throw new InvalidOperationException("冰箱内有东西,无法放进新的东西。");
[[2181]]
[[2181]] this.m_Content = value;
[[2184]] }
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] class Context
[[2182]][[2183]] [[2180]]{
[[2182]][[2183]] public Elephant Elephant [[2180]]{ get; set; }
[[2182]][[2183]] public Refrigerator Refrigerator [[2180]]{ get; set; }
[[2184]] }
[[2185]]}
请大家先认真读懂该代码,该代码不会有问题吧,如果有问题,那么还请你一定先读懂该代码再往下看。
后来,玉帝累了,他想啊,我堂堂玉帝,居然亲自干这种没含量的活,但是顾客是玉帝的玉帝,又不能不干。于是,玉帝找了两个手下(把门大神、神象星宿)来帮他干活。
_Text_112146').style.display='none'; document.getElementById('Code_Open_Image_112146').style.display='inline'; document.getElementById('Code_Open_Text_112146').style.display='inline';" height=16 src="/files/uploadimg/20090423/1047403.gif" width=11 align=top>[[2186]]Code2
using System;
using System.Collections.Generic;
namespace ConsoleApplication3
[[2186]][[2179]][[2180]]{
[[2181]] class Program
[[2182]][[2183]] [[2180]]{
[[2181]] static void Main(string[] args)
[[2182]][[2183]] [[2180]]{
[[2181]] Emperor emperor = new Emperor();
[[2181]] emperor.Init();
[[2181]]
[[2181]] while (true)
[[2182]][[2183]] [[2180]]{
[[2181]] Console.WriteLine("首先给玉帝准备好大象和冰箱。");
[[2181]]
[[2181]] Console.WriteLine("输入大象的名字:");
[[2181]] string elephantName = Console.ReadLine();
[[2181]] Console.WriteLine("输入大象的体重:");
[[2181]] int elephantWeight = int.Parse(Console.ReadLine());
[[2181]] Console.WriteLine("输入冰箱的名字:");
[[2181]] string refrigeratorName = Console.ReadLine();
[[2181]]
[[2181]] Elephant elephant = new Elephant()
[[2182]][[2183]] [[2180]]{
[[2181]] Name = elephantName,
[[2181]] Weight = elephantWeight
[[2184]] };
[[2181]] Refrigerator refrigerator = new Refrigerator()
[[2182]][[2183]] [[2180]]{
[[2181]] Name = refrigeratorName
[[2184]] };
[[2181]]
[[2181]] Context context = new Context()
[[2182]][[2183]] [[2180]]{
[[2181]] Elephant = elephant,
[[2181]] Refrigerator = refrigerator
[[2184]] };
[[2181]]
[[2181]] emperor.ProcessRequest(context);
[[2181]]
[[2181]] Console.WriteLine("是否要玉帝继续把大象关进冰箱里?");
[[2181]] string answer = Console.ReadLine();
[[2181]] if (answer == "n")
[[2181]] break;
[[2184]] }
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] class Emperor
[[2182]][[2183]] [[2180]]{
[[2181]] private IListm_Modules;
[[2182]][[2183]] public Context Context [[2180]]{ get; set; }
[[2181]]
[[2181]] public void Init()
[[2182]][[2183]] [[2180]]{
[[2181]] m_Modules = new List();
[[2181]] m_Modules.Add(new OpenCloseRefrigeratorDoorModule());
[[2181]] m_Modules.Add(new PutElephantToRefrigeratorModule());
[[2181]]
[[2181]] foreach (IEmperorModule module in this.m_Modules)
[[2182]][[2183]] [[2180]]{
[[2181]] module.Init(this);
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] public void ProcessRequest(Context context)
[[2182]][[2183]] [[2180]]{
[[2181]] this.Context = context;
[[2181]]
[[2181]] this.OnOpenRefrigeratorDoor();
[[2181]] this.OnPutElephantToRefrigerator();
[[2181]] this.OnCloseRefrigeratorDoor();
[[2184]] }
[[2181]]
[[2181]] public event EventHandler OpenRefrigeratorDoor;
[[2181]] public event EventHandler PutElephantToRefrigerator;
[[2181]] public event EventHandler CloseRefrigeratorDoor;
[[2181]]
[[2181]] protected virtual void OnOpenRefrigeratorDoor()
[[2182]][[2183]] [[2180]]{
[[2181]] EventHandler handler = this.OpenRefrigeratorDoor;
[[2181]] if (handler != null)
[[2182]][[2183]] [[2180]]{
[[2181]] handler(this, EventArgs.Empty);
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] protected virtual void OnPutElephantToRefrigerator()
[[2182]][[2183]] [[2180]]{
[[2181]] EventHandler handler = this.PutElephantToRefrigerator;
[[2181]] if (handler != null)
[[2182]][[2183]] [[2180]]{
[[2181]] handler(this, EventArgs.Empty);
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] protected virtual void OnCloseRefrigeratorDoor()
[[2182]][[2183]] [[2180]]{
[[2181]] EventHandler handler = this.CloseRefrigeratorDoor;
[[2181]] if (handler != null)
[[2182]][[2183]] [[2180]]{
[[2181]] handler(this, EventArgs.Empty);
[[2184]] }
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] class Elephant
[[2182]][[2183]] [[2180]]{
[[2182]][[2183]] public string Name [[2180]]{ get; set; }
[[2182]][[2183]] public int Weight[[2180]]{ get; set; }
[[2184]] }
[[2181]]
[[2181]] class Refrigerator
[[2182]][[2183]] [[2180]]{
[[2182]][[2183]] public string Name [[2180]]{ get; set; }
[[2182]][[2183]] public bool IsOpen [[2180]]{ get; set; }
[[2181]]
[[2181]] private object m_Content;
[[2181]] public object Content
[[2182]][[2183]] [[2180]]{
[[2182]][[2183]] get [[2180]]{ return this.m_Content; }
[[2181]] set
[[2182]][[2183]] [[2180]]{
[[2181]] if (!this.IsOpen)
[[2181]] throw new InvalidOperationException("冰箱门未打开,无法放进东西。");
[[2181]] if (this.m_Content != null)
[[2181]] throw new InvalidOperationException("冰箱内有东西,无法放进新的东西。");
[[2181]]
[[2181]] this.m_Content = value;
[[2184]] }
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] class Context
[[2182]][[2183]] [[2180]]{
[[2182]][[2183]] public Elephant Elephant [[2180]]{ get; set; }
[[2182]][[2183]] public Refrigerator Refrigerator [[2180]]{ get; set; }
[[2184]] }
[[2181]]
[[2181]] interface IEmperorModule
[[2182]][[2183]] [[2180]]{
[[2181]] void Init(Emperor emperor);
[[2184]] }
[[2181]]
[[2181]] class OpenCloseRefrigeratorDoorModule : IEmperorModule
[[2182]][[2183]] [[2180]]{
[[2183]][[2182]] IEmperorModule 成员#region IEmperorModule 成员
[[2181]] public void Init(Emperor emperor)
[[2182]][[2183]] [[2180]]{
[[2181]] emperor.OpenRefrigeratorDoor += new EventHandler(this.OpenDoor);
[[2181]] emperor.CloseRefrigeratorDoor += new EventHandler(this.CloseDoor);
[[2184]] }
[[2184]] #endregion
[[2181]]
[[2181]] private void OpenDoor(object sender, EventArgs e)
[[2182]][[2183]] [[2180]]{
[[2181]] Emperor emperor = (Emperor)sender;
[[2181]] Refrigerator refrigerator = emperor.Context.Refrigerator;
[[2181]] refrigerator.IsOpen = true;
[[2181]] Console.WriteLine(string.Format("玉帝的把门大神打开了 {0} 的冰箱门。", refrigerator.Name));
[[2184]] }
[[2181]]
[[2181]] private void CloseDoor(object sender, EventArgs e)
[[2182]][[2183]] [[2180]]{
[[2181]] Emperor emperor = (Emperor)sender;
[[2181]] Refrigerator refrigerator = emperor.Context.Refrigerator;
[[2181]] refrigerator.IsOpen = false;
[[2181]] Console.WriteLine(string.Format("玉帝的把门大神关上了 {0} 的冰箱门。", refrigerator.Name));
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] class PutElephantToRefrigeratorModule : IEmperorModule
[[2182]][[2183]] [[2180]]{
[[2183]][[2182]] IEmperorModule 成员#region IEmperorModule 成员
[[2181]] public void Init(Emperor emperor)
[[2182]][[2183]] [[2180]]{
[[2181]] emperor.PutElephantToRefrigerator += new EventHandler(this.PutElephantToRefrigerator);
[[2184]] }
[[2184]] #endregion
[[2181]]
[[2181]] private void PutElephantToRefrigerator(object sender, EventArgs e)
[[2182]][[2183]] [[2180]]{
[[2181]] Emperor emperor = (Emperor)sender;
[[2181]] Elephant elephant = emperor.Context.Elephant;
[[2181]] Refrigerator refrigerator = emperor.Context.Refrigerator;
[[2181]] refrigerator.Content = elephant;
[[2181]] Console.WriteLine(string.Format("玉帝的神象星宿把大象 {0} 放到冰箱 {1} 里了。", elephant.Name, refrigerator.Name));
[[2184]] }
[[2184]] }
[[2185]]}
观测该代码有哪些变化?玉帝增加了一个Init方法,该方法召集了那些干活的手下,并交待手下你们要干的事情(初始化)。所有手下都继承自IEmperorModule接口,该接口有一个Init方法,以初始化该手下能干的活。玉帝的ProcessRequest方法不再亲自干活了,而仅仅是在那边喊,该开门了,该把大象放进去了,该关门了。
来点题外话,上面代码应用了模板方法模式,或者说是模板方法的变种。什么,不是,没有抽象类,没有抽象方法,没有子类?请问,模板方法一定要有抽象类吗?一定要有抽象方法吗?一定要有子类吗?我想,模板方法的精髓,在于它规定了特定的步骤算法,而这些步骤在必要的时候可以委托给其它方法实现。在传统的模板方法中,是委托给子类实现,而在我们拥有委托的情况下,难道我们不能委托给其它类实现吗?其实,我想,学习设计模式,不要拘泥于形式,而要掌握其精髓,并灵活运用。就Gof的23种设计模式来说,我觉得其精髓几乎全部可由一句话概括。)
天有不测风云,人有旦夕祸福,某一天,这神象星宿病了,他赶不动超过1000斤的大象了,怎么办?玉帝于是又召集了超级神象星宿,可但凡有点能耐的都有点脾气,超级神象星宿表示,只有其它人赶不动的大象他才来赶。
[[2179]][[2186]]Code3
using System;
using System.Collections.Generic;
namespace ConsoleApplication3
[[2186]][[2179]][[2180]]{
[[2181]] class Program
[[2182]][[2183]] [[2180]]{
[[2181]] static void Main(string[] args)
[[2182]][[2183]] [[2180]]{
[[2181]] Emperor emperor = new Emperor();
[[2181]] emperor.Init();
[[2181]]
[[2181]] while (true)
[[2182]][[2183]] [[2180]]{
[[2181]] Console.WriteLine("首先给玉帝准备好大象和冰箱。");
[[2181]]
[[2181]] Console.WriteLine("输入大象的名字:");
[[2181]] string elephantName = Console.ReadLine();
[[2181]] Console.WriteLine("输入大象的体重:");
[[2181]] int elephantWeight = int.Parse(Console.ReadLine());
[[2181]] Console.WriteLine("输入冰箱的名字:");
[[2181]] string refrigeratorName = Console.ReadLine();
[[2181]]
[[2181]] Elephant elephant = new Elephant()
[[2182]][[2183]] [[2180]]{
[[2181]] Name = elephantName,
[[2181]] Weight = elephantWeight
[[2184]] };
[[2181]] Refrigerator refrigerator = new Refrigerator()
[[2182]][[2183]] [[2180]]{
[[2181]] Name = refrigeratorName
[[2184]] };
[[2181]]
[[2181]] Context context = new Context()
[[2182]][[2183]] [[2180]]{
[[2181]] Elephant = elephant,
[[2181]] Refrigerator = refrigerator
[[2184]] };
[[2181]]
[[2181]] emperor.ProcessRequest(context);
[[2181]]
[[2181]] Console.WriteLine("是否要玉帝继续把大象关进冰箱里?");
[[2181]] string answer = Console.ReadLine();
[[2181]] if (answer == "n")
[[2181]] break;
[[2184]] }
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] class Emperor
[[2182]][[2183]] [[2180]]{
[[2181]] private IListm_Modules;
[[2182]][[2183]] public Context Context [[2180]]{ get; set; }
[[2181]]
[[2181]] public void Init()
[[2182]][[2183]] [[2180]]{
[[2181]] m_Modules = new List();
[[2181]] m_Modules.Add(new OpenCloseRefrigeratorDoorModule());
[[2181]] m_Modules.Add(new PutElephantToRefrigeratorModule());
[[2181]] m_Modules.Add(new SuperPutElephantToRefrigeratorModule());
[[2181]]
[[2181]] foreach (IEmperorModule module in this.m_Modules)
[[2182]][[2183]] [[2180]]{
[[2181]] module.Init(this);
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] public void ProcessRequest(Context context)
[[2182]][[2183]] [[2180]]{
[[2181]] this.Context = context;
[[2181]]
[[2181]] this.OnOpenRefrigeratorDoor();
[[2181]] this.OnPutElephantToRefrigerator();
[[2181]] this.OnCloseRefrigeratorDoor();
[[2184]] }
[[2181]]
[[2181]] public event EventHandler OpenRefrigeratorDoor;
[[2181]] public event EventHandler PutElephantToRefrigerator;
[[2181]] public event EventHandler CloseRefrigeratorDoor;
[[2181]]
[[2181]] protected virtual void OnOpenRefrigeratorDoor()
[[2182]][[2183]] [[2180]]{
[[2181]] EventHandler handler = this.OpenRefrigeratorDoor;
[[2181]] if (handler != null)
[[2182]][[2183]] [[2180]]{
[[2181]] handler(this, EventArgs.Empty);
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] protected virtual void OnPutElephantToRefrigerator()
[[2182]][[2183]] [[2180]]{
[[2181]] EventHandler handler = this.PutElephantToRefrigerator;
[[2181]] if (handler != null)
[[2182]][[2183]] [[2180]]{
[[2181]] handler(this, EventArgs.Empty);
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] protected virtual void OnCloseRefrigeratorDoor()
[[2182]][[2183]] [[2180]]{
[[2181]] EventHandler handler = this.CloseRefrigeratorDoor;
[[2181]] if (handler != null)
[[2182]][[2183]] [[2180]]{
[[2181]] handler(this, EventArgs.Empty);
[[2184]] }
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] class Elephant
[[2182]][[2183]] [[2180]]{
[[2182]][[2183]] public string Name [[2180]]{ get; set; }
[[2182]][[2183]] public int Weight[[2180]]{ get; set; }
[[2184]] }
[[2181]]
[[2181]] class Refrigerator
[[2182]][[2183]] [[2180]]{
[[2182]][[2183]] public string Name [[2180]]{ get; set; }
[[2182]][[2183]] public bool IsOpen [[2180]]{ get; set; }
[[2181]]
[[2181]] private object m_Content;
[[2181]] public object Content
[[2182]][[2183]] [[2180]]{
[[2182]][[2183]] get [[2180]]{ return this.m_Content; }
[[2181]] set
[[2182]]Id('Codehighlighter1_3485_3771_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_3485_3771_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_3485_3771_Open_Text').style.display='inline';" src="/files/uploadimg/20090423/1047407.gif" align=top> [[2180]]{
[[2181]] if (!this.IsOpen)
[[2181]] throw new InvalidOperationException("冰箱门未打开,无法放进东西。");
[[2181]] if (this.m_Content != null)
[[2181]] throw new InvalidOperationException("冰箱内有东西,无法放进新的东西。");
[[2181]]
[[2181]] this.m_Content = value;
[[2184]] }
[[2184]] }
[[2184]] }
[[2181]]
[[2181]] class Context
[[2182]][[2183]] [[2180]]{
[[2182]][[2183]] public Elephant Elephant [[2180]]{ get; set; }
[[2182]][[2183]] public Refrigerator Refrigerator [[2180]]{ get; set; }
[[2181]]
[[2181]] private IDictionarym_Items;
[[2181]] public IDictionaryItems
[[2182]][[2183]] [[2180]]{
[[2181]] get
[[2182]][[2183]] none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">[[2180]]{
[[2181]] if (this.m_Items == null)
[[2181]] &n文章标题:白话详解ASP.NET的设计思想
文章转载:http://www.shufengxianlan.com/qtweb/news5/90355.html网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联