怎样理解ASP.NET的设计思想
这篇文章给大家介绍怎样理解ASP.NET的设计思想,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
成都创新互联成立10多年来,这条路我们正越走越好,积累了技术与客户资源,形成了良好的口碑。为客户提供成都做网站、成都网站制作、网站策划、网页设计、主机域名、网络营销、VI设计、网站改版、漏洞修补等服务。网站是否美观、功能强大、用户体验好、性价比高、打开快等等,这些对于网站建设都非常重要,成都创新互联通过对建站技术性的掌握、对创意设计的研究为客户提供一站式互联网解决方案,携手广大客户,共同发展进步。
自从有了html与http,就有了浏览器与Web服务器,并有了Web应用,最初的交互模式是这样的:
该模式很好地运行了很多年。然而,随着计算机应用的发展,人们越来越不满足于只有静态内容的页面,而由某种机制动态产生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。
先且按下该问题不表,我们来看一段请求玉帝把大象放到冰箱里的代码(为什么不是上帝?因为我中华不归上帝管),请大家先跟着我的思路来,别急,别急。
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>[object Object] Program [object Object][object Object] void Main(string[] args) [object Object][object Object] Emperor(); [object Object] while (true) [object Object][object Object] elephantName = Console.ReadLine(); [object Object] Console.WriteLine("输入大象的体重:"); [object Object] int elephantWeight = int.Parse(Console.ReadLine()); [object Object] Console.WriteLine("输入冰箱的名字:"); [object Object] string refrigeratorName = Console.ReadLine(); [object Object] [object Object] Elephant elephant = new Elephant() [object Object][object Object] ; [object Object] Refrigerator refrigerator = new Refrigerator() [object Object][object Object] ; [object Object] [object Object] Context context = new Context() [object Object][object Object] ; [object Object] [object Object] emperor.ProcessRequest(context); [object Object] [object Object] Console.WriteLine("是否要玉帝继续把大象关进冰箱里?"); [object Object] string answer = Console.ReadLine(); if (answer == "n") break; } [object Object] } } class Emperor void ProcessRequest(Context context) [object Object][object Object] ; [object Object] Console.WriteLine(string.Format("玉帝打开了 {0} 的冰箱门。", refrigerator.Name)); [object Object] [object Object] //第二步,把大象放进去 [object Object] refrigerator.Content = elephant; [object Object] Console.WriteLine(string.Format("玉帝把大象 {0} 放到冰箱 {1} 里了。", elephant.Name, refrigerator.Name)); [object Object] [object Object] //第三步,关上冰箱门 [object Object] refrigerator.IsOpen = false; [object Object] Console.WriteLine(string.Format("玉帝关上了 {0} 的冰箱门。", refrigerator.Name)); [object Object] } [object Object] } [object Object] [object Object] class Elephant [object Object][object Object] string Name ; set; } [object Object][object Object] public int Weight; set; } [object Object] } [object Object] [object Object] class Refrigerator [object Object][object Object] string Name ; set; } [object Object][object Object] public bool IsOpen ; set; } [object Object] [object Object] private object m_Content; [object Object] public object Content [object Object][object Object] this.m_Content; } [object Object] set [object Object][object Object] (!this.IsOpen) [object Object] throw new InvalidOperationException("冰箱门未打开,无法放进东西。"); [object Object] if (this.m_Content != null) [object Object] throw new InvalidOperationException("冰箱内有东西,无法放进新的东西。"); [object Object] [object Object] this.m_Content = value; [object Object] } [object Object] } [object Object] } [object Object] [object Object] class Context [object Object][object Object] Elephant Elephant ; set; } [object Object][object Object] public Refrigerator Refrigerator ; set; } [object Object] } [object Object]}
请大家先认真读懂该代码,该代码不会有问题吧,如果有问题,那么还请你一定先读懂该代码再往下看。
后来,玉帝累了,他想啊,我堂堂玉帝,居然亲自干这种没含量的活,但是顾客是玉帝的玉帝,又不能不干。于是,玉帝找了两个手下(把门大神、神象星宿)来帮他干活。
_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>[object Object] System; using System.Collections.Generic; namespace ConsoleApplication3 [object Object][object Object] Program [object Object][object Object] void Main(string[] args) [object Object][object Object] Emperor(); [object Object] emperor.Init(); [object Object] [object Object] while (true) [object Object][object Object] elephantName = Console.ReadLine(); [object Object] Console.WriteLine("输入大象的体重:"); [object Object] int elephantWeight = int.Parse(Console.ReadLine()); [object Object] Console.WriteLine("输入冰箱的名字:"); [object Object] string refrigeratorName = Console.ReadLine(); [object Object] [object Object] Elephant elephant = new Elephant() [object Object][object Object] ; [object Object] Refrigerator refrigerator = new Refrigerator() [object Object][object Object] ; [object Object] [object Object] Context context = new Context() [object Object][object Object] ; [object Object] [object Object] emperor.ProcessRequest(context); [object Object] [object Object] Console.WriteLine("是否要玉帝继续把大象关进冰箱里?"); [object Object] string answer = Console.ReadLine(); [object Object] if (answer == "n") [object Object] break; [object Object] } [object Object] } [object Object] } [object Object] [object Object] class Emperor [object Object][object Object] IListm_Modules; [object Object][object Object] public Context Context ; set; } [object Object] [object Object] public void Init() [object Object][object Object] List (); [object Object] m_Modules.Add(new OpenCloseRefrigeratorDoorModule()); [object Object] m_Modules.Add(new PutElephantToRefrigeratorModule()); [object Object] [object Object] foreach (IEmperorModule module in this.m_Modules) [object Object][object Object] ); [object Object] } [object Object] } [object Object] [object Object] public void ProcessRequest(Context context) [object Object][object Object] .Context = context; [object Object] [object Object] this.OnOpenRefrigeratorDoor(); [object Object] this.OnPutElephantToRefrigerator(); [object Object] this.OnCloseRefrigeratorDoor(); [object Object] } [object Object] [object Object] public event EventHandler OpenRefrigeratorDoor; [object Object] public event EventHandler PutElephantToRefrigerator; [object Object] public event EventHandler CloseRefrigeratorDoor; [object Object] [object Object] protected virtual void OnOpenRefrigeratorDoor() [object Object][object Object] .OpenRefrigeratorDoor; [object Object] if (handler != null) [object Object][object Object] , EventArgs.Empty); [object Object] } [object Object] } [object Object] [object Object] protected virtual void OnPutElephantToRefrigerator() [object Object][object Object] .PutElephantToRefrigerator; [object Object] if (handler != null) [object Object][object Object] , EventArgs.Empty); [object Object] } [object Object] } [object Object] [object Object] protected virtual void OnCloseRefrigeratorDoor() [object Object][object Object] .CloseRefrigeratorDoor; [object Object] if (handler != null) [object Object][object Object] , EventArgs.Empty); [object Object] } [object Object] } [object Object] } [object Object] [object Object] class Elephant [object Object][object Object] string Name ; set; } [object Object][object Object] public int Weight; set; } [object Object] } [object Object] [object Object] class Refrigerator [object Object][object Object] string Name ; set; } [object Object][object Object] public bool IsOpen ; set; } [object Object] [object Object] private object m_Content; [object Object] public object Content [object Object][object Object] this.m_Content; } [object Object] set [object Object][object Object] (!this.IsOpen) [object Object] throw new InvalidOperationException("冰箱门未打开,无法放进东西。"); [object Object] if (this.m_Content != null) [object Object] throw new InvalidOperationException("冰箱内有东西,无法放进新的东西。"); [object Object] [object Object] this.m_Content = value; [object Object] } [object Object] } [object Object] } [object Object] [object Object] class Context [object Object][object Object] Elephant Elephant ; set; } [object Object][object Object] public Refrigerator Refrigerator ; set; } [object Object] } [object Object] [object Object] interface IEmperorModule [object Object][object Object] Init(Emperor emperor); [object Object] } [object Object] [object Object] class OpenCloseRefrigeratorDoorModule : IEmperorModule [object Object][object Object] void Init(Emperor emperor) [object Object][object Object] EventHandler(this.OpenDoor); [object Object] emperor.CloseRefrigeratorDoor += new EventHandler(this.CloseDoor); [object Object] } [object Object] #endregion [object Object] [object Object] private void OpenDoor(object sender, EventArgs e) [object Object][object Object] ; [object Object] Console.WriteLine(string.Format("玉帝的把门大神打开了 {0} 的冰箱门。", refrigerator.Name)); [object Object] } [object Object] [object Object] private void CloseDoor(object sender, EventArgs e) [object Object][object Object] ; [object Object] Console.WriteLine(string.Format("玉帝的把门大神关上了 {0} 的冰箱门。", refrigerator.Name)); [object Object] } [object Object] } [object Object] [object Object] class PutElephantToRefrigeratorModule : IEmperorModule [object Object][object Object] void Init(Emperor emperor) [object Object][object Object] EventHandler(this.PutElephantToRefrigerator); [object Object] } [object Object] #endregion [object Object] [object Object] private void PutElephantToRefrigerator(object sender, EventArgs e) [object Object][object Object] .Format("玉帝的神象星宿把大象 {0} 放到冰箱 {1} 里了。", elephant.Name, refrigerator.Name)); [object Object] } [object Object] } [object Object]}
观测该代码有哪些变化?玉帝增加了一个Init方法,该方法召集了那些干活的手下,并交待手下你们要干的事情(初始化)。所有手下都继承自IEmperorModule接口,该接口有一个Init方法,以初始化该手下能干的活。玉帝的ProcessRequest方法不再亲自干活了,而仅仅是在那边喊,该开门了,该把大象放进去了,该关门了。
来点题外话,上面代码应用了模板方法模式,或者说是模板方法的变种。什么,不是,没有抽象类,没有抽象方法,没有子类?请问,模板方法一定要有抽象类吗?一定要有抽象方法吗?一定要有子类吗?我想,模板方法的精髓,在于它规定了特定的步骤算法,而这些步骤在必要的时候可以委托给其它方法实现。在传统的模板方法中,是委托给子类实现,而在我们拥有委托的情况下,难道我们不能委托给其它类实现吗?其实,我想,学习设计模式,不要拘泥于形式,而要掌握其精髓,并灵活运用。就Gof的23种设计模式来说,我觉得其精髓几乎全部可由一句话概括。)
天有不测风云,人有旦夕祸福,某一天,这神象星宿病了,他赶不动超过1000斤的大象了,怎么办?玉帝于是又召集了超级神象星宿,可但凡有点能耐的都有点脾气,超级神象星宿表示,只有其它人赶不动的大象他才来赶。
[object Object][object Object] System; using System.Collections.Generic; namespace ConsoleApplication3 [object Object][object Object] Program [object Object][object Object] void Main(string[] args) [object Object][object Object] Emperor(); [object Object] emperor.Init(); [object Object] [object Object] while (true) [object Object][object Object] elephantName = Console.ReadLine(); [object Object] Console.WriteLine("输入大象的体重:"); [object Object] int elephantWeight = int.Parse(Console.ReadLine()); [object Object] Console.WriteLine("输入冰箱的名字:"); [object Object] string refrigeratorName = Console.ReadLine(); [object Object] [object Object] Elephant elephant = new Elephant() [object Object][object Object] ; [object Object] Refrigerator refrigerator = new Refrigerator() [object Object][object Object] ; [object Object] [object Object] Context context = new Context() [object Object][object Object] ; [object Object] [object Object] emperor.ProcessRequest(context); [object Object] [object Object] Console.WriteLine("是否要玉帝继续把大象关进冰箱里?"); [object Object] string answer = Console.ReadLine(); [object Object] if (answer == "n") [object Object] break; [object Object] } [object Object] } [object Object] } [object Object] [object Object] class Emperor [object Object][object Object] IListm_Modules; [object Object][object Object] public Context Context ; set; } [object Object] [object Object] public void Init() [object Object][object Object] List (); [object Object] m_Modules.Add(new OpenCloseRefrigeratorDoorModule()); [object Object] m_Modules.Add(new PutElephantToRefrigeratorModule()); [object Object] m_Modules.Add(new SuperPutElephantToRefrigeratorModule()); [object Object] [object Object] foreach (IEmperorModule module in this.m_Modules) [object Object][object Object] ); [object Object] } [object Object] } [object Object] [object Object] public void ProcessRequest(Context context) [object Object][object Object] .Context = context; [object Object] [object Object] this.OnOpenRefrigeratorDoor(); [object Object] this.OnPutElephantToRefrigerator(); [object Object] this.OnCloseRefrigeratorDoor(); [object Object] } [object Object] [object Object] public event EventHandler OpenRefrigeratorDoor; [object Object] public event EventHandler PutElephantToRefrigerator; [object Object] public event EventHandler CloseRefrigeratorDoor; [object Object] [object Object] protected virtual void OnOpenRefrigeratorDoor() [object Object][object Object] .OpenRefrigeratorDoor; [object Object] if (handler != null) [object Object][object Object] , EventArgs.Empty); [object Object] } [object Object] } [object Object] [object Object] protected virtual void OnPutElephantToRefrigerator() [object Object][object Object] .PutElephantToRefrigerator; [object Object] if (handler != null) [object Object][object Object] , EventArgs.Empty); [object Object] } [object Object] } [object Object] [object Object] protected virtual void OnCloseRefrigeratorDoor() [object Object][object Object] .CloseRefrigeratorDoor; [object Object] if (handler != null) [object Object][object Object] , EventArgs.Empty); [object Object] } [object Object] } [object Object] } [object Object] [object Object] class Elephant [object Object][object Object] string Name ; set; } [object Object][object Object] public int Weight; set; } [object Object] } [object Object] [object Object] class Refrigerator [object Object][object Object] string Name ; set; } [object Object][object Object] public bool IsOpen ; set; } [object Object] [object Object] private object m_Content; [object Object] public object Content [object Object][object Object] this.m_Content; } [object Object] set [object Object]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> (!this.IsOpen) [object Object] throw new InvalidOperationException("冰箱门未打开,无法放进东西。"); [object Object] if (this.m_Content != null) [object Object] throw new InvalidOperationException("冰箱内有东西,无法放进新的东西。"); [object Object] [object Object] this.m_Content = value; [object Object] } [object Object] } [object Object] } [object Object] [object Object] class Context [object Object][object Object] Elephant Elephant ; set; } [object Object][object Object] public Refrigerator Refrigerator ; set; } [object Object] [object Object] private IDictionary m_Items; [object Object] public IDictionary Items [object Object][object Object] [object Object][object Object] none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">[object Object] (this.m_Items == null) [object Object] this.m_Items = new Dictionary (); [object Object] return this.m_Items; [object Object] } [object Object] } [object Object] } [object Object] [object Object] interface IEmperorModule [object Object][object Object] Init(Emperor emperor); [object Object] } [object Object] [object Object] class OpenCloseRefrigeratorDoorModule : IEmperorModule [object Object][object Object] void Init(Emperor emperor) [object Object][object Object] EventHandler(this.OpenDoor); [object Object] emperor.CloseRefrigeratorDoor += new EventHandler(this.CloseDoor); [object Object] } [object Object] #endregion [object Object] [object Object] private void OpenDoor(object sender, EventArgs e) [object Object][object Object] ; [object Object] Console.WriteLine(string.Format("玉帝的把门大神打开了 {0} 的冰箱门。", refrigerator.Name)); [object Object] } [object Object] [object Object] private void CloseDoor(object sender, EventArgs e) [object Object][object Object] ; [object Object] Console.WriteLine(string.Format("玉帝的把门大神关上了 {0} 的冰箱门。", refrigerator.Name)); [object Object] } [object Object] } [object Object] [object Object] class PutElephantToRefrigeratorModule : IEmperorModule [object Object][object Object] void Init(Emperor emperor) [object Object][object Object] EventHandler(this.PutElephantToRefrigerator); [object Object] } [object Object] #endregion [object Object] [object Object] private void PutElephantToRefrigerator(object sender, EventArgs e) [object Object][object Object] (elephant.Weight > 1000) [object Object][object Object] [object Object] else [object Object][object Object] ; [object Object] Console.WriteLine(string.Format("玉帝的神象星宿把大象 {0} 放到冰箱 {1} 里了。", elephant.Name, refrigerator.Name)); [object Object] } [object Object] } [object Object] } [object Object] [object Object] class SuperPutElephantToRefrigeratorModule : IEmperorModule [object Object][object Object] void Init(Emperor emperor) [object Object][object Object] EventHandler(this.PutElephantToRefrigerator); [object Object] } [object Object] #endregion [object Object] [object Object] private void PutElephantToRefrigerator(object sender, EventArgs e) [object Object][object Object] hasPutElephant; [object Object] if (emperor.Context.Items.TryGetValue("HasPutElephant", out hasPutElephant)) [object Object][object Object] (hasPutElephant is bool && ((bool)hasPutElephant) == true) [object Object][object Object] ; [object Object] } [object Object] } [object Object] [object Object] refrigerator.Content = elephant; [object Object] emperor.Context.Items["HasPutElephant"] = true; [object Object] Console.WriteLine(string.Format("玉帝的超级神象星宿把大象 {0} 放到冰箱 {1} 里了。", elephant.Name, refrigerator.Name)); [object Object] } [object Object] } [object Object]}
仔细琢磨该代码,请问你发现了什么?如果你发现的是灵活的可扩展性和可配置性,以及各手下之间的交互,那么恭喜你,你悟性很高。如果你还发现了职责链模式,那么恭喜你,你悟性非常高。(该代码中两个神象星宿的交互完全可以通过在Context中增加一个bool值属性来实现,之所有这里用Items字典,是为了演示如果你不具有修改Context的源代码的时候,该如何处理。)
好了,ASP.NET的设计思想讲完了。什么,还没开始?你没发现我们的Program就是HttpRuntime、Emperor就是HttpApplication、Context就是HttpContext、IEmperorModule就是IHttpModule吗?什么,没发现?那么,让我们来看看ASP.NET的源代码吧。(本想把整个方法的源代码都贴上来,但是显得过于冗长,因此删掉非关键代码,并以//……省略……代替。)
如此,你明白了吗?
关于怎样理解ASP.NET的设计思想就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
分享标题:怎样理解ASP.NET的设计思想
分享链接:http://hbruida.cn/article/jpsojd.html