Cuyahoga研究1: "Hello World" 模版开发
Cuyahoga 是一个不错的开源的站点管理系统 (内容管理系统CMS) ,有以下一些特点:
- 数据库的访问配置通过 NHibernate
- 企业级的设计架构
- 模块化的插件
- 可扩展性,利用 Mono 可以轻松实现多平台(windows,Linux, Mac OS X, Solaris)
- 搜索引擎使用 DotLucene
- ASP .NET 2.0 开发
- log4net 作为log记录系统
首先下载最新的版本 1.5.0 源码 and 官方的基本开发模版教程.
使用的开发工具是VS2005 SP1,注意一定有sp1,不然打不开官方下载的web工程项目。
在开发helloworld前记得安装Cuyahoga数据库。使用1.5.0源码的install/default.aspx安装数据库和默认站点。.
今天我们只是做一个基本用来显示 hello World 的测试模块。
首先用VS2005打开Cuyahoga 1.5.0的源码工程文件。
右键解决方案,添加一个项目,选择一个web项目,项目名称叫Cuyahoga.Modules.Sample
然后建立如图的文件系统。
Domain用来存放Nhibernet的map文件和对象类
install 用来存放用于不同数据库的安装卸载脚本
web目录存放的时开发的自定义的用户控件,和控制类,以及用于显示的图片和资源文件,另外图片和资源文件需要在web中分别存放在不同的目录,比如images和resources。假如你的空间需要用于多语言的话,就需要资源文件管理。
打开项目属性,在生成事件中的生成事件后的命令行输入一下命令
(假如用图片的话,同样需要拷贝到项目对应的目录中)
xcopy /s /y "$(ProjectDir)"Web\*.as?x "$(SolutionDir)"Web\Modules\Sample\ xcopy /s /y "$(ProjectDir)"Install\Database\*.sql "$(SolutionDir)"Web\Modules\Sample\Install\Database\ xcopy /s /y "$(TargetDir)"Cuyahoga.Modules.Sample*.dll "$(SolutionDir)"Web\bin\
- 这里提一下,官方的意思是创建模块遵循以下四点
1 添加module controller
2 添加对象类文件和NHibernate 的orm 文件
3 添加 可管理的 用户自定义控件ascx文件,用来添加到前台页面aspx中。
4 添加sql脚本用来增加和卸载你开发的模块
添加一个module controller
添加的一个SampleModule.cs类文件,继承Cuyahoga.Core.Domain.ModuleBase(首先记得在项目中引用Cuyahoga.Core).
SampleModule.cs 拥有一下的代码。
using System; using Cuyahoga.Core.Domain; namespace Cuyahoga.Modules.Sample { /// <summary> /// Controller class of the module /// </summary> class SampleModule: ModuleBase { /// <summary> /// Controller constructor /// </summary> public SampleModule() { //nothing in the constructor for this basic sample } } }
注意,在你自己开发的项目中,module controller 的命名用项目名字+Module组成,这里我们用 SampleModule.
添加用户自定义控件
在web目录下添加一个Sample.ascx的用户自定义控件。在页面中添加 Hello world!字样
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Sample.ascx.cs" Inherits="Cuyahoga.Modules.Sample.Web.Sample" %>
Hello World!
后台代码继承 Cuyahoga.Web.UI.BaseModuleControl(记得在项目中引用Cuyahoga.Web), Sample.ascx.cs的代码如下
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using Cuyahoga.Web.UI; namespace Cuyahoga.Modules.Sample.Web { /// <summary> /// Sample control displayng in Cuyahoga the "Hello World" text /// </summary> public partial class Sample : BaseModuleControl { protected void Page_Load(object sender, EventArgs e) { //for this basic sample we do not need to do anything } } }
添加SQL安装卸载脚本
你可以在你的Cuyahoga平台中很方便的增加和卸载你的模块,这就需要你的sql安装卸载脚本,假如你使用的是不同的数据库,那就需要增加不同的数据库脚本在不同的目录中区别。
这里测试使用的是MSSQL2000数据库
在MSSQL2000安装目录的install.sql和Uninstall.sql文件中分别输入以下脚本代码
--Install the Sample module for Cuyahoga in SQL Server INSERT INTO cuyahoga_moduletype ([name], assemblyname, classname, path, editpath, inserttimestamp, updatetimestamp) VALUES ('Sample', 'Cuyahoga.Modules.Sample', 'Cuyahoga.Modules.Sample.SampleModule', 'Modules/Sample/Sample.ascx', NULL, '2007-27-04 14:36:28.324', '2007-27-04 14:36:28.324') GO INSERT INTO cuyahoga_version (assembly, major, minor, patch) VALUES ('Cuyahoga.Modules.Sample', 1, 5, 0) GO
--Uninstall the Sample module in Cuyahoga for SQL Server DELETE FROM cuyahoga_version WHERE assembly = 'Cuyahoga.Modules.Sample' go DELETE FROM cuyahoga_moduletype WHERE assemblyname = 'Cuyahoga.Modules.Sample' go
接下来测试一下我们做的模块。
运行工程(设置Cuyahoga.web项目为启动项目,admin/default.aspx为起始页面),登陆你安装时候设置的管理员,你可以进入manage modules,用来安装刚才增加的Sample 模块
安装完你的控件后,可以选择一个页面,添加section,选用我们刚开发的模块Sample:
保存页面,使用右上角的View current page查看我们刚开发的模块的表现效果。