自從有了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。
先且按下該問題不表,我們來看一段請求玉帝把大象放到冰箱里的代碼(為什么不是上帝?因為我中華不歸上帝管),請大家先跟著我的思路來,別急,別急。

Code1
using System;

namespace ConsoleApplication3


{
class Program

{
static void Main(string[] args)

{
Emperor emperor = new Emperor();
while (true)

{
Console.WriteLine("首先給玉帝準備好大象和冰箱。");

Console.WriteLine("輸入大象的名字:");
string elephantName = Console.ReadLine();
Console.WriteLine("輸入大象的體重:");
int elephantWeight = int.Parse(Console.ReadLine());
Console.WriteLine("輸入冰箱的名字:");
string refrigeratorName = Console.ReadLine();

Elephant elephant = new Elephant()

{
Name = elephantName,
Weight = elephantWeight
};
Refrigerator refrigerator = new Refrigerator()

{
Name = refrigeratorName
};

Context context = new Context()

{
Elephant = elephant,
Refrigerator = refrigerator
};

emperor.ProcessRequest(context);

Console.WriteLine("是否要玉帝繼續把大象關進冰箱里?");
string answer = Console.ReadLine();
if (answer == "n")
break;
}
}
}

class Emperor

{
public void ProcessRequest(Context context)

{
Elephant elephant = context.Elephant;
Refrigerator refrigerator = context.Refrigerator;

//第一步,打開冰箱門
refrigerator.IsOpen = true;
Console.WriteLine(string.Format("玉帝打開了 {0} 的冰箱門。", refrigerator.Name));

//第二步,把大象放進去
refrigerator.Content = elephant;
Console.WriteLine(string.Format("玉帝把大象 {0} 放到冰箱 {1} 里了。", elephant.Name, refrigerator.Name));

//第三步,關上冰箱門
refrigerator.IsOpen = false;
Console.WriteLine(string.Format("玉帝關上了 {0} 的冰箱門。", refrigerator.Name));
}
}

class Elephant

{

public string Name
{ get; set; }

public int Weight
{ get; set; }
}

class Refrigerator

{

public string Name
{ get; set; }

public bool IsOpen
{ get; set; }

private object m_Content;
public object Content

{

get
{ return this.m_Content; }
set

{
if (!this.IsOpen)
throw new InvalidOperationException("冰箱門未打開,無法放進東西。");
if (this.m_Content != null)
throw new InvalidOperationException("冰箱內有東西,無法放進新的東西。");

this.m_Content = value;
}
}
}

class Context

{

public Elephant Elephant
{ get; set; }

public Refrigerator Refrigerator
{ get; set; }
}
}
請大家先認真讀懂該代碼,該代碼不會有問題吧,如果有問題,那么還請你一定先讀懂該代碼再往下看。
后來,玉帝累了,他想啊,我堂堂玉帝,居然親自干這種沒含量的活,但是顧客是玉帝的玉帝,又不能不干。于是,玉帝找了兩個手下(把門大神、神象星宿)來幫他干活。

Code2
using System;
using System.Collections.Generic;

namespace ConsoleApplication3


{
class Program

{
static void Main(string[] args)

{
Emperor emperor = new Emperor();
emperor.Init();

while (true)

{
Console.WriteLine("首先給玉帝準備好大象和冰箱。");

Console.WriteLine("輸入大象的名字:");
string elephantName = Console.ReadLine();
Console.WriteLine("輸入大象的體重:");
int elephantWeight = int.Parse(Console.ReadLine());
Console.WriteLine("輸入冰箱的名字:");
string refrigeratorName = Console.ReadLine();

Elephant elephant = new Elephant()

{
Name = elephantName,
Weight = elephantWeight
};
Refrigerator refrigerator = new Refrigerator()

{
Name = refrigeratorName
};

Context context = new Context()

{
Elephant = elephant,
Refrigerator = refrigerator
};

emperor.ProcessRequest(context);

Console.WriteLine("是否要玉帝繼續把大象關進冰箱里?");
string answer = Console.ReadLine();
if (answer == "n")
break;
}
}
}

class Emperor

{
private IList<IEmperorModule> m_Modules;

public Context Context
{ get; set; }

public void Init()

{
m_Modules = new List<IEmperorModule>();
m_Modules.Add(new OpenCloseRefrigeratorDoorModule());
m_Modules.Add(new PutElephantToRefrigeratorModule());

foreach (IEmperorModule module in this.m_Modules)

{
module.Init(this);
}
}

public void ProcessRequest(Context context)

{
this.Context = context;

this.OnOpenRefrigeratorDoor();
this.OnPutElephantToRefrigerator();
this.OnCloseRefrigeratorDoor();
}

public event EventHandler OpenRefrigeratorDoor;
public event EventHandler PutElephantToRefrigerator;
public event EventHandler CloseRefrigeratorDoor;

protected virtual void OnOpenRefrigeratorDoor()

{
EventHandler handler = this.OpenRefrigeratorDoor;
if (handler != null)

{
handler(this, EventArgs.Empty);
}
}

protected virtual void OnPutElephantToRefrigerator()

{
EventHandler handler = this.PutElephantToRefrigerator;
if (handler != null)

{
handler(this, EventArgs.Empty);
}
}

protected virtual void OnCloseRefrigeratorDoor()

{
EventHandler handler = this.CloseRefrigeratorDoor;
if (handler != null)

{
handler(this, EventArgs.Empty);
}
}
}

class Elephant

{

public string Name
{ get; set; }

public int Weight
{ get; set; }
}

class Refrigerator

{

public string Name
{ get; set; }

public bool IsOpen
{ get; set; }

private object m_Content;
public object Content

{

get
{ return this.m_Content; }
set

{
if (!this.IsOpen)
throw new InvalidOperationException("冰箱門未打開,無法放進東西。");
if (this.m_Content != null)
throw new InvalidOperationException("冰箱內有東西,無法放進新的東西。");

this.m_Content = value;
}
}
}

class Context

{

public Elephant Elephant
{ get; set; }

public Refrigerator Refrigerator
{ get; set; }
}

interface IEmperorModule

{
void Init(Emperor emperor);
}

class OpenCloseRefrigeratorDoorModule : IEmperorModule

{

IEmperorModule 成員#region IEmperorModule 成員
public void Init(Emperor emperor)

{
emperor.OpenRefrigeratorDoor += new EventHandler(this.OpenDoor);
emperor.CloseRefrigeratorDoor += new EventHandler(this.CloseDoor);
}
#endregion

private void OpenDoor(object sender, EventArgs e)

{
Emperor emperor = (Emperor)sender;
Refrigerator refrigerator = emperor.Context.Refrigerator;
refrigerator.IsOpen = true;
Console.WriteLine(string.Format("玉帝的把門大神打開了 {0} 的冰箱門。", refrigerator.Name));
}

private void CloseDoor(object sender, EventArgs e)

{
Emperor emperor = (Emperor)sender;
Refrigerator refrigerator = emperor.Context.Refrigerator;
refrigerator.IsOpen = false;
Console.WriteLine(string.Format("玉帝的把門大神關上了 {0} 的冰箱門。", refrigerator.Name));
}
}

class PutElephantToRefrigeratorModule : IEmperorModule

{

IEmperorModule 成員#region IEmperorModule 成員
public void Init(Emperor emperor)

{
emperor.PutElephantToRefrigerator += new EventHandler(this.PutElephantToRefrigerator);
}
#endregion

private void PutElephantToRefrigerator(object sender, EventArgs e)

{
Emperor emperor = (Emperor)sender;
Elephant elephant = emperor.Context.Elephant;
Refrigerator refrigerator = emperor.Context.Refrigerator;
refrigerator.Content = elephant;
Console.WriteLine(string.Format("玉帝的神象星宿把大象 {0} 放到冰箱 {1} 里了。", elephant.Name, refrigerator.Name));
}
}
}
觀測該代碼有哪些變化?玉帝增加了一個Init方法,該方法召集了那些干活的手下,并交待手下你們要干的事情(初始化)。所有手下都繼承自IEmperorModule接口,該接口有一個Init方法,以初始化該手下能干的活。玉帝的ProcessRequest方法不再親自干活了,而僅僅是在那邊喊,該開門了,該把大象放進去了,該關門了。
(來點題外話,上面代碼應用了模板方法模式,或者說是模板方法的變種。什么,不是,沒有抽象類,沒有抽象方法,沒有子類?請問,模板方法一定要有抽象類嗎?一定要有抽象方法嗎?一定要有子類嗎?我想,模板方法的精髓,在于它規定了特定的步驟算法,而這些步驟在必要的時候可以委托給其它方法實現。在傳統的模板方法中,是委托給子類實現,而在我們擁有委托的情況下,難道我們不能委托給其它類實現嗎?其實,我想,學習設計模式,不要拘泥于形式,而要掌握其精髓,并靈活運用。就Gof的23種設計模式來說,我覺得其精髓幾乎全部可由一句話概括。)
天有不測風云,人有旦夕禍福,某一天,這神象星宿病了,他趕不動超過1000斤的大象了,怎么辦?玉帝于是又召集了超級神象星宿,可但凡有點能耐的都有點脾氣,超級神象星宿表示,只有其它人趕不動的大象他才來趕。

Code3
using System;
using System.Collections.Generic;

namespace ConsoleApplication3


{
class Program

{
static void Main(string[] args)

{
Emperor emperor = new Emperor();
emperor.Init();

while (true)

{
Console.WriteLine("首先給玉帝準備好大象和冰箱。");

Console.WriteLine("輸入大象的名字:");
string elephantName = Console.ReadLine();
Console.WriteLine("輸入大象的體重:");
int elephantWeight = int.Parse(Console.ReadLine());
Console.WriteLine("輸入冰箱的名字:");
string refrigeratorName = Console.ReadLine();

Elephant elephant = new Elephant()

{
Name = elephantName,
Weight = elephantWeight
};
Refrigerator refrigerator = new Refrigerator()

{
Name = refrigeratorName
};

Context context = new Context()

{
Elephant = elephant,
Refrigerator = refrigerator
};

emperor.ProcessRequest(context);

Console.WriteLine("是否要玉帝繼續把大象關進冰箱里?");
string answer = Console.ReadLine();
if (answer == "n")
break;
}
}
}

class Emperor

{
private IList<IEmperorModule> m_Modules;

public Context Context
{ get; set; }

public void Init()

{
m_Modules = new List<IEmperorModule>();
m_Modules.Add(new OpenCloseRefrigeratorDoorModule());
m_Modules.Add(new PutElephantToRefrigeratorModule());
m_Modules.Add(new SuperPutElephantToRefrigeratorModule());

foreach (IEmperorModule module in this.m_Modules)

{
module.Init(this);
}
}

public void ProcessRequest(Context context)

{
this.Context = context;

this.OnOpenRefrigeratorDoor();
this.OnPutElephantToRefrigerator();
this.OnCloseRefrigeratorDoor();
}

public event EventHandler OpenRefrigeratorDoor;
public event EventHandler PutElephantToRefrigerator;
public event EventHandler CloseRefrigeratorDoor;

protected virtual void OnOpenRefrigeratorDoor()

{
EventHandler handler = this.OpenRefrigeratorDoor;
if (handler != null)

{
handler(this, EventArgs.Empty);
}
}

protected virtual void OnPutElephantToRefrigerator()

{
EventHandler handler = this.PutElephantToRefrigerator;
if (handler != null)

{
handler(this, EventArgs.Empty);
}
}

protected virtual void OnCloseRefrigeratorDoor()

{
EventHandler handler = this.CloseRefrigeratorDoor;
if (handler != null)

{
handler(this, EventArgs.Empty);
}
}
}

class Elephant

{

public string Name
{ get; set; }

public int Weight
{ get; set; }
}

class Refrigerator

{

public string Name
{ get; set; }

public bool IsOpen
{ get; set; }

private object m_Content;
public object Content

{

get
{ return this.m_Content; }
set

{
if (!this.IsOpen)
throw new InvalidOperationException("冰箱門未打開,無法放進東西。");
if (this.m_Content != null)
throw new InvalidOperationException("冰箱內有東西,無法放進新的東西。");

this.m_Content = value;
}
}
}

class Context

{

public Elephant Elephant
{ get; set; }

public Refrigerator Refrigerator
{ get; set; }

private IDictionary<string, object> m_Items;
public IDictionary<string, object> Items

{
get

{
if (this.m_Items == null)
this.m_Items = new Dictionary<string, object>();
return this.m_Items;
}
}
}

interface IEmperorModule

{
void Init(Emperor emperor);
}

class OpenCloseRefrigeratorDoorModule : IEmperorModule

{

IEmperorModule 成員#region IEmperorModule 成員
public void Init(Emperor emperor)

{
emperor.OpenRefrigeratorDoor += new EventHandler(this.OpenDoor);
emperor.CloseRefrigeratorDoor += new EventHandler(this.CloseDoor);
}
#endregion

private void OpenDoor(object sender, EventArgs e)

{
Emperor emperor = (Emperor)sender;
Refrigerator refrigerator = emperor.Context.Refrigerator;
refrigerator.IsOpen = true;
Console.WriteLine(string.Format("玉帝的把門大神打開了 {0} 的冰箱門。", refrigerator.Name));
}

private void CloseDoor(object sender, EventArgs e)

{
Emperor emperor = (Emperor)sender;
Refrigerator refrigerator = emperor.Context.Refrigerator;
refrigerator.IsOpen = false;
Console.WriteLine(string.Format("玉帝的把門大神關上了 {0} 的冰箱門。", refrigerator.Name));
}
}

class PutElephantToRefrigeratorModule : IEmperorModule

{

IEmperorModule 成員#region IEmperorModule 成員
public void Init(Emperor emperor)

{
emperor.PutElephantToRefrigerator += new EventHandler(this.PutElephantToRefrigerator);
}
#endregion

private void PutElephantToRefrigerator(object sender, EventArgs e)

{
Emperor emperor = (Emperor)sender;
Elephant elephant = emperor.Context.Elephant;
Refrigerator refrigerator = emperor.Context.Refrigerator;

if (elephant.Weight > 1000)

{
Console.WriteLine("這頭大象實在太重了,我神象星宿挪不動阿。");
}
else

{
refrigerator.Content = elephant;
emperor.Context.Items["HasPutElephant"] = true;
Console.WriteLine(string.Format("玉帝的神象星宿把大象 {0} 放到冰箱 {1} 里了。", elephant.Name, refrigerator.Name));
}
}
}

class SuperPutElephantToRefrigeratorModule : IEmperorModule

{

IEmperorModule 成員#region IEmperorModule 成員
public void Init(Emperor emperor)

{
emperor.PutElephantToRefrigerator += new EventHandler(this.PutElephantToRefrigerator);
}
#endregion

private void PutElephantToRefrigerator(object sender, EventArgs e)

{
Emperor emperor = (Emperor)sender;
Elephant elephant = emperor.Context.Elephant;
Refrigerator refrigerator = emperor.Context.Refrigerator;

object hasPutElephant;
if (emperor.Context.Items.TryGetValue("HasPutElephant", out hasPutElephant))

{
if (hasPutElephant is bool && ((bool)hasPutElephant) == true)

{
Console.WriteLine("已經有人把大象放進冰箱了,我超級神象星宿就歇歇了。");
return;
}
}

refrigerator.Content = elephant;
emperor.Context.Items["HasPutElephant"] = true;
Console.WriteLine(string.Format("玉帝的超級神象星宿把大象 {0} 放到冰箱 {1} 里了。", elephant.Name, refrigerator.Name));
}
}
}
仔細琢磨該代碼,請問你發現了什么?如果你發現的是靈活的可擴展性和可配置性,以及各手下之間的交互,那么恭喜你,你悟性很高。如果你還發現了職責鏈模式,那么恭喜你,你悟性非常高。(該代碼中兩個神象星宿的交互完全可以通過在Context中增加一個bool值屬性來實現,之所有這里用Items字典,是為了演示如果你不具有修改Context的源代碼的時候,該如何處理。)