白话详解ASP.NET的设计思想

开发 后端
我们看到的技术类文章,大多都隐晦难懂。对于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。

先且按下该问题不表,我们来看一段请求玉帝把大象放到冰箱里的代码(为什么不是上帝?因为我中华不归上帝管),请大家先跟着我的思路来,别急,别急。

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]]
{
[[2181]]    
class
 Program
[[2182]][[2183]]    
{
[[2181]]        
static
 void Main(string[] args)
[[2182]][[2183]]        
{
[[2181]]            Emperor emperor 
= new
 Emperor();
[[2181]]            
while (true)
[[2182]][[2183]]            
{
[[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]]                
{
[[2181]]                    Name 
= elephantName,
[[2181]]                    Weight 
= elephantWeight
[[2184]]                }
;
[[2181]]                Refrigerator refrigerator 
= new Refrigerator()
[[2182]][[2183]]                
{
[[2181]]                    Name 
= refrigeratorName
[[2184]]                }
;
[[2181]]
[[2181]]                Context context 
= new Context()
[[2182]][[2183]]                
{
[[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]]    
{
[[2182]][[2183]]        
public
 string Name getset; }
[[2182]][[2183]]        
public int Weightgetset; }
[[2184]]    }

[[2181]]
[[2181]]    
class Refrigerator
[[2182]][[2183]]    
{
[[2182]][[2183]]        
public
 string Name getset; }
[[2182]][[2183]]        
public bool IsOpen getset; }
[[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]]    
{
[[2182]][[2183]]        
public
 Elephant Elephant getset; }
[[2182]][[2183]]        
public Refrigerator Refrigerator getset; }
[[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]]
using
 System;
using System.Collections.Generic;

namespace ConsoleApplication3
[[2186]][[2179]]
{
[[2181]]    
class
 Program
[[2182]][[2183]]    
{
[[2181]]        
static
 void Main(string[] args)
[[2182]][[2183]]        
{
[[2181]]            Emperor emperor 
= new
 Emperor();
[[2181]]            emperor.Init();
[[2181]]
[[2181]]            
while (true)
[[2182]][[2183]]            
{
[[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]]                
{
[[2181]]                    Name 
= elephantName,
[[2181]]                    Weight 
= elephantWeight
[[2184]]                }
;
[[2181]]                Refrigerator refrigerator 
= new Refrigerator()
[[2182]][[2183]]                
{
[[2181]]                    Name 
= refrigeratorName
[[2184]]                }
;
[[2181]]
[[2181]]                Context context 
= new Context()
[[2182]][[2183]]                
{
[[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]]    
{
[[2181]]        
private
 IList<IEmperorModule> m_Modules;
[[2182]][[2183]]        
public Context Context getset; }
[[2181]]
[[2181]]        
public void Init()
[[2182]][[2183]]        
{
[[2181]]            m_Modules 
= new
 List<IEmperorModule>();
[[2181]]            m_Modules.Add(
new OpenCloseRefrigeratorDoorModule());
[[2181]]            m_Modules.Add(
new PutElephantToRefrigeratorModule());
[[2181]]
[[2181]]            
foreach (IEmperorModule module in this.m_Modules)
[[2182]][[2183]]            
{
[[2181]]                module.Init(
this
);
[[2184]]            }

[[2184]]        }

[[2181]]
[[2181]]        
public void ProcessRequest(Context context)
[[2182]][[2183]]        
{
[[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]]        
{
[[2181]]            EventHandler handler 
= this
.OpenRefrigeratorDoor;
[[2181]]            
if (handler != null)
[[2182]][[2183]]            
{
[[2181]]                handler(
this
, EventArgs.Empty);
[[2184]]            }

[[2184]]        }

[[2181]]
[[2181]]        
protected virtual void OnPutElephantToRefrigerator()
[[2182]][[2183]]        
{
[[2181]]            EventHandler handler 
= this
.PutElephantToRefrigerator;
[[2181]]            
if (handler != null)
[[2182]][[2183]]            
{
[[2181]]                handler(
this
, EventArgs.Empty);
[[2184]]            }

[[2184]]        }

[[2181]]
[[2181]]        
protected virtual void OnCloseRefrigeratorDoor()
[[2182]][[2183]]        
{
[[2181]]            EventHandler handler 
= this
.CloseRefrigeratorDoor;
[[2181]]            
if (handler != null)
[[2182]][[2183]]            
{
[[2181]]                handler(
this
, EventArgs.Empty);
[[2184]]            }

[[2184]]        }

[[2184]]    }

[[2181]]
[[2181]]    
class Elephant
[[2182]][[2183]]    
{
[[2182]][[2183]]        
public
 string Name getset; }
[[2182]][[2183]]        
public int Weightgetset; }
[[2184]]    }

[[2181]]
[[2181]]    
class Refrigerator
[[2182]][[2183]]    
{
[[2182]][[2183]]        
public
 string Name getset; }
[[2182]][[2183]]        
public bool IsOpen getset; }
[[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]]    
{
[[2182]][[2183]]        
public
 Elephant Elephant getset; }
[[2182]][[2183]]        
public Refrigerator Refrigerator getset; }
[[2184]]    }

[[2181]]
[[2181]]    
interface IEmperorModule
[[2182]][[2183]]    
{
[[2181]]        
void
 Init(Emperor emperor);
[[2184]]    }

[[2181]]
[[2181]]    
class OpenCloseRefrigeratorDoorModule : IEmperorModule
[[2182]][[2183]]    
{
[[2183]][[2182]]        
IEmperorModule 成员 IEmperorModule 成员
[[2181]]        
public
 void Init(Emperor emperor)
[[2182]][[2183]]        
{
[[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]]        
{
[[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]]        
{
[[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]]    
{
[[2183]][[2182]]        
IEmperorModule 成员 IEmperorModule 成员
[[2181]]        
public
 void Init(Emperor emperor)
[[2182]][[2183]]        
{
[[2181]]            emperor.PutElephantToRefrigerator 
+= new
 EventHandler(this.PutElephantToRefrigerator);
[[2184]]        }

[[2184]]        
#endregion
[[2181]]
[[2181]]        
private void PutElephantToRefrigerator(object sender, EventArgs e)
[[2182]][[2183]]        
{
[[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]]
using
 System;
using System.Collections.Generic;

namespace ConsoleApplication3
[[2186]][[2179]]
{
[[2181]]    
class
 Program
[[2182]][[2183]]    
{
[[2181]]        
static
 void Main(string[] args)
[[2182]][[2183]]        
{
[[2181]]            Emperor emperor 
= new
 Emperor();
[[2181]]            emperor.Init();
[[2181]]
[[2181]]            
while (true)
[[2182]][[2183]]            
{
[[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]]                
{
[[2181]]                    Name 
= elephantName,
[[2181]]                    Weight 
= elephantWeight
[[2184]]                }
;
[[2181]]                Refrigerator refrigerator 
= new Refrigerator()
[[2182]][[2183]]                
{
[[2181]]                    Name 
= refrigeratorName
[[2184]]                }
;
[[2181]]
[[2181]]                Context context 
= new Context()
[[2182]][[2183]]                
{
[[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]]    
{
[[2181]]        
private
 IList<IEmperorModule> m_Modules;
[[2182]][[2183]]        
public Context Context getset; }
[[2181]]
[[2181]]        
public void Init()
[[2182]][[2183]]        
{
[[2181]]            m_Modules 
= new
 List<IEmperorModule>();
[[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]]            
{
[[2181]]                module.Init(
this
);
[[2184]]            }

[[2184]]        }

[[2181]]
[[2181]]        
public void ProcessRequest(Context context)
[[2182]][[2183]]        
{
[[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]]        
{
[[2181]]            EventHandler handler 
= this
.OpenRefrigeratorDoor;
[[2181]]            
if (handler != null)
[[2182]][[2183]]            
{
[[2181]]                handler(
this
, EventArgs.Empty);
[[2184]]            }

[[2184]]        }

[[2181]]
[[2181]]        
protected virtual void OnPutElephantToRefrigerator()
[[2182]][[2183]]        
{
[[2181]]            EventHandler handler 
= this
.PutElephantToRefrigerator;
[[2181]]            
if (handler != null)
[[2182]][[2183]]            
{
[[2181]]                handler(
this
, EventArgs.Empty);
[[2184]]            }

[[2184]]        }

[[2181]]
[[2181]]        
protected virtual void OnCloseRefrigeratorDoor()
[[2182]][[2183]]        
{
[[2181]]            EventHandler handler 
= this
.CloseRefrigeratorDoor;
[[2181]]            
if (handler != null)
[[2182]][[2183]]            
{
[[2181]]                handler(
this
, EventArgs.Empty);
[[2184]]            }

[[2184]]        }

[[2184]]    }

[[2181]]
[[2181]]    
class Elephant
[[2182]][[2183]]    
{
[[2182]][[2183]]        
public
 string Name getset; }
[[2182]][[2183]]        
public int Weightgetset; }
[[2184]]    }

[[2181]]
[[2181]]    
class Refrigerator
[[2182]][[2183]]    
{
[[2182]][[2183]]        
public
 string Name getset; }
[[2182]][[2183]]        
public bool IsOpen getset; }
[[2181]]
[[2181]]        
private object m_Content;
[[2181]]        
public object Content
[[2182]][[2183]]        
{
[[2182]][[2183]]            
get
 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>            
{
[[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]]    
{
[[2182]][[2183]]        
public
 Elephant Elephant getset; }
[[2182]][[2183]]        
public Refrigerator Refrigerator getset; }
[[2181]]
[[2181]]        
private IDictionary<stringobject> m_Items;
[[2181]]        
public IDictionary<stringobject> Items
[[2182]][[2183]]        
{
[[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]]                    
this.m_Items = new Dictionary<stringobject>();
[[2181]]                
return this.m_Items;
[[2184]]            }

[[2184]]        }

[[2184]]    }

[[2181]]
[[2181]]    
interface IEmperorModule
[[2182]][[2183]]    
{
[[2181]]        
void
 Init(Emperor emperor);
[[2184]]    }

[[2181]]
[[2181]]    
class OpenCloseRefrigeratorDoorModule : IEmperorModule
[[2182]][[2183]]    
{
[[2183]][[2182]]        
IEmperorModule 成员 IEmperorModule 成员
[[2181]]        
public
 void Init(Emperor emperor)
[[2182]][[2183]]        
{
[[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]]        
{
[[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]]        
{
[[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]]    
{
[[2183]][[2182]]        
IEmperorModule 成员 IEmperorModule 成员
[[2181]]        
public
 void Init(Emperor emperor)
[[2182]][[2183]]        
{
[[2181]]            emperor.PutElephantToRefrigerator 
+= new
 EventHandler(this.PutElephantToRefrigerator);
[[2184]]        }

[[2184]]        
#endregion
[[2181]]
[[2181]]        
private void PutElephantToRefrigerator(object sender, EventArgs e)
[[2182]][[2183]]        
{
[[2181]]            Emperor emperor 
= (Emperor)sender;
[[2181]]            Elephant elephant 
= emperor.Context.Elephant;
[[2181]]            Refrigerator refrigerator 
= emperor.Context.Refrigerator;
[[2181]]
[[2181]]            
if
 (elephant.Weight > 1000)
[[2182]][[2183]]            
{
[[2181]]                Console.WriteLine(
"这头大象实在太重了,我神象星宿挪不动阿。");
[[2184]]            }

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

[[2184]]        }

[[2184]]    }

[[2181]]
[[2181]]    
class SuperPutElephantToRefrigeratorModule : IEmperorModule
[[2182]][[2183]]    
{
[[2183]][[2182]]        
IEmperorModule 成员 IEmperorModule 成员
[[2181]]        
public
 void Init(Emperor emperor)
[[2182]][[2183]]        
{
[[2181]]            emperor.PutElephantToRefrigerator 
+= new
 EventHandler(this.PutElephantToRefrigerator);
[[2184]]        }

[[2184]]        
#endregion
[[2181]]
[[2181]]        
private void PutElephantToRefrigerator(object sender, EventArgs e)
[[2182]][[2183]]        
{
[[2181]]            Emperor emperor 
= (Emperor)sender;
[[2181]]            Elephant elephant 
= emperor.Context.Elephant;
[[2181]]            Refrigerator refrigerator 
= emperor.Context.Refrigerator;
[[2181]]
[[2181]]            
object
 hasPutElephant;
[[2181]]            
if (emperor.Context.Items.TryGetValue("HasPutElephant"out hasPutElephant))
[[2182]][[2183]]            
{
[[2181]]                
if
 (hasPutElephant is bool && ((bool)hasPutElephant) == true)
[[2182]][[2183]]                
{
[[2181]]                    Console.WriteLine(
"已经有人把大象放进冰箱了,我超级神象星宿就歇歇了。");
[[2181]]                    
return
;
[[2184]]                }

[[2184]]            }

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

[[2184]]    }

[[2185]]}

仔细琢磨该代码,请问你发现了什么?如果你发现的是灵活的可扩展性和可配置性,以及各手下之间的交互,那么恭喜你,你悟性很高。如果你还发现了职责链模式,那么恭喜你,你悟性非常高。(该代码中两个神象星宿的交互完全可以通过在Context中增加一个bool值属性来实现,之所有这里用Items字典,是为了演示如果你不具有修改Context的源代码的时候,该如何处理。)

好了,ASP.NET的设计思想讲完了。什么,还没开始?你没发现我们的Program就是HttpRuntime、Emperor就是HttpApplication、Context就是HttpContext、IEmperorModule就是IHttpModule吗?什么,没发现?那么,让我们来看看ASP.NET的源代码吧。(本想把整个方法的源代码都贴上来,但是显得过于冗长,因此删掉非关键代码,并以//……省略……代替。)

 

如此,你明白了吗?

【编辑推荐】

  1. 简单理解ASP.NET MVC基本知识
  2. ASP.NET获得新浪天气预报几种方式总结
  3. ASP.NET十个有效性能优化的方法
责任编辑:彭凡 来源: cnblogs
相关推荐

2009-07-29 16:08:07

ASP和ASP.NET

2009-07-23 13:19:51

2009-07-28 16:57:50

ASP.NET Ses

2009-08-05 11:14:33

ASP.NET ISA

2009-07-24 10:14:22

ASP.NET开发

2009-07-22 16:25:41

ASP.NET AJA

2009-08-10 13:32:15

ASP.NET TimASP.NET组件设计

2023-07-04 08:26:15

2009-03-31 09:18:34

客户端内置对象ASP.NET

2009-07-28 13:39:44

加载ViewStateASP.NET

2009-08-04 10:43:59

ASP.NET控件开发

2009-08-19 13:44:00

ASP.NET Lis

2009-08-04 18:10:35

ASP.NET动态编译

2009-08-10 14:31:46

ASP.NET组件设计ASP.NET控件生命

2009-07-23 13:09:23

2009-08-10 10:19:47

ASP.NET组件设计

2009-11-09 08:53:21

ASP.NET缓存

2009-08-05 16:53:14

ASP.NET组件设计

2009-08-13 11:44:25

ASP.NET中的多种

2009-08-05 17:39:13

ASP.NET常用状态
点赞
收藏

51CTO技术栈公众号