基于Monorail的系统功能模块化
模块化总是让人比较容易理解的,如果你的程序可以让人很容易理解,那是多么美好的事情!不论是对自己或是对别人。N层的开发让人看到了模块化的威力,这里不讨论整个系统的结构,而是细分到系统的每个功能,比如说一个网站的功能,可能有会员管理模块,有内容管理模块等等,DNN等一系列cms在这点的实现是很强大的,但是似乎真的“大”了一点。最近看了一些基于插件式的内容管理系统,不免让人觉得有些过度设计的感觉,web本该如此简单,太复杂的架构感觉有点沉重。最近用到Castle的Monorail,发现了一个好玩的东西,说出来分享下,知道的就不要乱拍啦!
首先我有一个简单的网站,里面已经有几个简单的功能了。因为简单,所以就想不断地完善,添加功能,步骤很简单。
看配置文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="monorail" type="Castle.MonoRail.Framework.Configuration.MonoRailSectionHandler, Castle.MonoRail.Framework" />
</configSections>
<!--
For more on MonoRail configuration see
http://www.castleproject.org/monorail/documentation/v1rc3/index.html
-->
<monorail smtpHost="yoursmtphost" useWindsorIntegration="false">
<controllers>
<assembly>ExPortal</assembly>
<assembly>ControlOther</assembly>
</controllers>
<viewEngines viewPathRoot="Views">
<add xhtml="false" type="Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity" />
</viewEngines>
</monorail>
<system.web>
<httpHandlers>
<add verb="*" path="*.castle" type="Castle.MonoRail.Framework.MonoRailHttpHandlerFactory, Castle.MonoRail.Framework" />
<!-- block direct user access to template files -->
<add verb="*" path="*.vm" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.njs" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.brail" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.brailjs" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.st" type="System.Web.HttpForbiddenHandler" />
</httpHandlers>
<httpModules>
<add name="monorail" type="Castle.MonoRail.Framework.EngineContextModule, Castle.MonoRail.Framework" />
</httpModules>
</system.web>
</configuration>
看看Controllers下的子节点,我配置了两个Assembly,ControlOther里面有我新加的功能模块GameController类,下面只是个例子,<configuration>
<configSections>
<section name="monorail" type="Castle.MonoRail.Framework.Configuration.MonoRailSectionHandler, Castle.MonoRail.Framework" />
</configSections>
<!--
For more on MonoRail configuration see
http://www.castleproject.org/monorail/documentation/v1rc3/index.html
-->
<monorail smtpHost="yoursmtphost" useWindsorIntegration="false">
<controllers>
<assembly>ExPortal</assembly>
<assembly>ControlOther</assembly>
</controllers>
<viewEngines viewPathRoot="Views">
<add xhtml="false" type="Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity" />
</viewEngines>
</monorail>
<system.web>
<httpHandlers>
<add verb="*" path="*.castle" type="Castle.MonoRail.Framework.MonoRailHttpHandlerFactory, Castle.MonoRail.Framework" />
<!-- block direct user access to template files -->
<add verb="*" path="*.vm" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.njs" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.brail" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.brailjs" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.st" type="System.Web.HttpForbiddenHandler" />
</httpHandlers>
<httpModules>
<add name="monorail" type="Castle.MonoRail.Framework.EngineContextModule, Castle.MonoRail.Framework" />
</httpModules>
</system.web>
</configuration>
using System;
using System.Collections.Generic;
using System.Text;
using Castle.MonoRail.Framework;
namespace ControlOther
{
public class GameController:Controller
{
public void index()
{
PropertyBag["name"] = "Awen";
}
}
}
using System.Collections.Generic;
using System.Text;
using Castle.MonoRail.Framework;
namespace ControlOther
{
public class GameController:Controller
{
public void index()
{
PropertyBag["name"] = "Awen";
}
}
}
编译,将dll拷贝到web applaction 的bin文件夹里,然后在Web里面views文件夹里面添加vm(view,视图文件)
ok,现在就可以访问game/index.rail了,不信,你试试看吧!
当然,这里添加模块是基于web.config的,我看了源代码,用如下代码可以设置Assemblies。
ControllersConfig con = MonoRailConfiguration.GetConfig().ControllersConfig;
con.Assemblies = new string[] { "ExPortal", "ControlOther" };
con.Assemblies = new string[] { "ExPortal", "ControlOther" };
或许可以重写Monorail读取配置的代码,改为以数据库为存储,加以缓存依赖,下次再研究下。不知这样算不算一个可插拔的系统呢?但是好像在运行期改变Controller的Assemblies后也没效果哦,暂时没找到,如果有同学知道,说声咯!