了解之后我们也可以知道哪一部分是可以由我们改造的。
基于Castle 1.0 RC3版本,和Castle 1.0稍微有点区别
首先看一下最简的一个web.config配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="monorail" type="Castle.MonoRail.Framework.Configuration.MonoRailSectionHandler, Castle.MonoRail.Framework" />
</configSections>
<monorail>
<controllers>
<assembly>TestSiteNVelocity</assembly>
</controllers>
<viewEngines viewPathRoot="views">
<add type="Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity" xhtml="false" />
</viewEngines>
</monorail>
<system.web>
<httpHandlers>
<add verb="*" path="*.rails" type="Castle.MonoRail.Framework.MonoRailHttpHandlerFactory, Castle.MonoRail.Framework" />
</httpHandlers>
<httpModules>
<add name="monorail" type="Castle.MonoRail.Framework.EngineContextModule, Castle.MonoRail.Framework" />
</httpModules>
</system.web>
</configuration>
<configuration>
<configSections>
<section name="monorail" type="Castle.MonoRail.Framework.Configuration.MonoRailSectionHandler, Castle.MonoRail.Framework" />
</configSections>
<monorail>
<controllers>
<assembly>TestSiteNVelocity</assembly>
</controllers>
<viewEngines viewPathRoot="views">
<add type="Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity" xhtml="false" />
</viewEngines>
</monorail>
<system.web>
<httpHandlers>
<add verb="*" path="*.rails" type="Castle.MonoRail.Framework.MonoRailHttpHandlerFactory, Castle.MonoRail.Framework" />
</httpHandlers>
<httpModules>
<add name="monorail" type="Castle.MonoRail.Framework.EngineContextModule, Castle.MonoRail.Framework" />
</httpModules>
</system.web>
</configuration>
可以看到在web.config中出现了三个类,下面我就按照程序执行的顺序来分析一下:
(以输入最简单的http://localhost:***/home/index.rails为例)
首先执行EngineContextModule类的Init方法:
public void Init(HttpApplication context)
{
if (context.Context.Error != null)
{
throw new Exception(
"An exception happened on Global application or on a module that run before MonoRail's module. " +
"MonoRail will not be initialized and further requests are going to fail. " +
"Fix the cause of the error reported below.", context.Context.Error);
}
lock(initLock)
{
CreateAndStartContainer(context);
}
context.BeginRequest += new EventHandler(OnStartMonoRailRequest);
context.AuthorizeRequest += new EventHandler(CreateControllerAndRunStartRequestFilters);
SubscribeToApplicationHooks(context);
}
{
if (context.Context.Error != null)
{
throw new Exception(
"An exception happened on Global application or on a module that run before MonoRail's module. " +
"MonoRail will not be initialized and further requests are going to fail. " +
"Fix the cause of the error reported below.", context.Context.Error);
}
lock(initLock)
{
CreateAndStartContainer(context);
}
context.BeginRequest += new EventHandler(OnStartMonoRailRequest);
context.AuthorizeRequest += new EventHandler(CreateControllerAndRunStartRequestFilters);
SubscribeToApplicationHooks(context);
}
在Init方法中会读取配置文件,根据配置文件生成相应的类,
如:XmlNodeList services = section.SelectNodes("services/service");
比如我们下面提到的DefaultUrlTokenizer类,当我们在web.config中配置了相应的类后,在解析Url时会使用我们自己配置的类,缺省情况下才使用DefaultUrlTokenizer类
然后执行OnStartMonoRailRequest方法:
主要是把Url等信息解析好放入context中,context.Items[RailsContextKey] = newContext; 这里放入的是一个DefaultRailsEngineContext对象,这也是特别重要的一个对象,里面包含我们要调用的Controller类等信息。
在解析Url时默认用的是DefaultUrlTokenizer类,主要以下一段话:
string[] parts = rawUrl.Split('/');
if (parts.Length < 2)
{
throw new UrlTokenizerException("Url smaller than 2 tokens");
}
action = parts[parts.Length - 1];
也就是当我们输入http://localhost:****/index.rails时会报错,必须有两个/的情况才允许的if (parts.Length < 2)
{
throw new UrlTokenizerException("Url smaller than 2 tokens");
}
action = parts[parts.Length - 1];
接下来执行CreateControllerAndRunStartRequestFilters方法,这个方法最主要的就是生成Controller类:
return (Controller) Activator.CreateInstance(type);
然后进入MonoRailHttpHandlerFactory类的GetHandler方法
默认情况下调用以下语句:
mrHandler = new MonoRailHttpHandler(logger);
接着再进入MonoRailHttpHandler类的ProcessRequest方法
这里首先取得处理好的context对象:
IRailsEngineContext mrContext = EngineContextModule.ObtainRailsEngineContext(context);
通过mrContext对象最终调用动态方法:
method.Invoke(this, new object[0]);
也就是执行HomeController中的index方法,执行完之后,显示页面:
private void ProcessView()
{
if (controller._selectedViewName != null)
{
viewEngineManager.Process(context, controller, controller._selectedViewName);
}
}
显示页面时就会调用我们web.config中配置的NVelocityViewEngine类去实际输出页面{
if (controller._selectedViewName != null)
{
viewEngineManager.Process(context, controller, controller._selectedViewName);
}
}
小结:
从以上分析可以看出MonoRail的设计是很灵活的,很多思想可以借鉴的,
首先Module部分可以被替换,我们可以实现自己的Module,
其次viewEngines部分可以替换(这个很多人都知道了)
然后一些Services也可以动态替换,比如我前面提到的解析Url的类,可以用我们自己的方式解析,来实现不同的Controller和Url的映射关系。
本文转自永春博客园博客,原文链接:http://www.cnblogs.com/firstyi/archive/2007/10/19/929150.html,如需转载请自行联系原作者