您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎樣理解ASP.NET的設計思想,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
自從有了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] IList<IEmperorModule> m_Modules; [object Object][object Object] public Context Context ; set; } [object Object] [object Object] public void Init() [object Object][object Object] List<IEmperorModule>(); [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] IList<IEmperorModule> m_Modules; [object Object][object Object] public Context Context ; set; } [object Object] [object Object] public void Init() [object Object][object Object] List<IEmperorModule>(); [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<string, object> m_Items; [object Object] public IDictionary<string, object> 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<string, object>(); [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的設計思想就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。