如何在Cuyahoga中添加公告模块
下面就来介绍如何为Cuyahoga这个著名的开源网站框架加入具有后台管理的公告模块.可以参考这篇如何在Cuyahoga中新增一个简单的功能模块了解基础的步骤.
为Cuyahoga开发自定义模块时,你可以选择任何数据访问策略.然而Cuyahoga本身是使用NHibernate作为数据持久层,可以做到支持多数据库. 采用Castle.Windsor进行依赖注入,降低模块之间的耦合. 我们的数据访问层也将用NHibernate实现.
最终项目的目录结构如下:
主要步骤如下
1 .创建一个Sql文件(Install.sql)用来安装数据表及添加模块的相关信息 , 该sql文件会在安装模块时,由Cuyahoga自动执行.也可以手工执行进行安装.
IF NOT EXISTS (Select * FROM sysobjects Where id = OBJECT_ID(N'[cm_Announcements]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
Create TABLE [cm_Announcements] (
[AnnouncementsID] [int] IDENTITY (1,1)NOT NULL ,
[sectionid] [int] NOT NULL ,
[createdby] [nvarchar] (100) NULL ,//公告作者
[Title] [nvarchar] (150) NULL ,//公告标题
inserttimestamp datetime DEFAULT current_timestamp NOT NULL,
updatetimestamp datetime DEFAULT current_timestamp NOT NULL,
CONSTRAINT [PK_cm_Announcements] PRIMARY KEY NONCLUSTERED
(
[AnnouncementsID]
),
CONSTRAINT [FK_cm_Announcements_cm_Modules] FOREIGN KEY
(sectionid) REFERENCES cuyahoga_section (sectionid)
)
END
GO
/*加入模块信息*/
Insert INTO cuyahoga_moduletype ([name], assemblyname, classname, path, editpath, inserttimestamp, updatetimestamp) VALUES
('Announcements', 'Cuyahoga.Modules.Announcements', 'Cuyahoga.Modules.Announcements.AnnouncementsModule',
'Modules/Announcements/Announcements.ascx', 'Modules/Announcements/EditAnnouncements.aspx', current_timestamp, current_timestamp)
GO
/*加入模块版本信息*/
Insert INTO cuyahoga_version (assembly, major, minor, patch) VALUES ('Cuyahoga.Modules.Announcements', 1, 5, 0);
go
2.创建域模型 在本例中是实体类
using System;
using Cuyahoga.Core.Domain;
namespace Cuyahoga.Modules.Announcements.Domain
{
public class Announcement
{
private int _id;
private string _title;
private string _content;
private DateTime _expiredate;
private Section _section;
private User _createdBy;
private DateTime _updateTimestamp;
/// <summary>
/// Property Id (int)
/// summary>
public int Id
{
get { return this._id; }
set { this._id = value; }
}
/// <summary>
/// Property Title (string)
/// summary>
public string Title
{
get { return this._title; }
set { this._title = value; }
}
/// <summary>
/// Property Section (Section)
/// summary>
public Section Section
{
get { return this._section; }
set { this._section = value; }
}
/// <summary>
/// Property CreatedBy (User)
/// summary>
public User CreatedBy
{
get { return this._createdBy; }
set { this._createdBy = value; }
}
/// <summary>
/// Property UpdateTimestamp (DateTime)
/// summary>
public DateTime UpdateTimestamp
{
get { return this._updateTimestamp; }
set { this._updateTimestamp = value; }
}
public Announcement()
{
this._id = -1;
}
}
}
为Cuyahoga开发自定义模块时,你可以选择任何数据访问策略.然而Cuyahoga本身是使用NHibernate作为数据持久层,可以做到支持多数据库. 采用Castle.Windsor进行依赖注入,降低模块之间的耦合. 我们的数据访问层也将用NHibernate实现.
最终项目的目录结构如下:
主要步骤如下
1 .创建一个Sql文件(Install.sql)用来安装数据表及添加模块的相关信息 , 该sql文件会在安装模块时,由Cuyahoga自动执行.也可以手工执行进行安装.
IF NOT EXISTS (Select * FROM sysobjects Where id = OBJECT_ID(N'[cm_Announcements]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
Create TABLE [cm_Announcements] (
[AnnouncementsID] [int] IDENTITY (1,1)NOT NULL ,
[sectionid] [int] NOT NULL ,
[createdby] [nvarchar] (100) NULL ,//公告作者
[Title] [nvarchar] (150) NULL ,//公告标题
inserttimestamp datetime DEFAULT current_timestamp NOT NULL,
updatetimestamp datetime DEFAULT current_timestamp NOT NULL,
CONSTRAINT [PK_cm_Announcements] PRIMARY KEY NONCLUSTERED
(
[AnnouncementsID]
),
CONSTRAINT [FK_cm_Announcements_cm_Modules] FOREIGN KEY
(sectionid) REFERENCES cuyahoga_section (sectionid)
)
END
GO
/*加入模块信息*/
Insert INTO cuyahoga_moduletype ([name], assemblyname, classname, path, editpath, inserttimestamp, updatetimestamp) VALUES
('Announcements', 'Cuyahoga.Modules.Announcements', 'Cuyahoga.Modules.Announcements.AnnouncementsModule',
'Modules/Announcements/Announcements.ascx', 'Modules/Announcements/EditAnnouncements.aspx', current_timestamp, current_timestamp)
GO
/*加入模块版本信息*/
Insert INTO cuyahoga_version (assembly, major, minor, patch) VALUES ('Cuyahoga.Modules.Announcements', 1, 5, 0);
go
2.创建域模型 在本例中是实体类
using System;
using Cuyahoga.Core.Domain;
namespace Cuyahoga.Modules.Announcements.Domain
{
public class Announcement
{
private int _id;
private string _title;
private string _content;
private DateTime _expiredate;
private Section _section;
private User _createdBy;
private DateTime _updateTimestamp;
/// <summary>
/// Property Id (int)
/// summary>
public int Id
{
get { return this._id; }
set { this._id = value; }
}
/// <summary>
/// Property Title (string)
/// summary>
public string Title
{
get { return this._title; }
set { this._title = value; }
}
/// <summary>
/// Property Section (Section)
/// summary>
public Section Section
{
get { return this._section; }
set { this._section = value; }
}
/// <summary>
/// Property CreatedBy (User)
/// summary>
public User CreatedBy
{
get { return this._createdBy; }
set { this._createdBy = value; }
}
/// <summary>
/// Property UpdateTimestamp (DateTime)
/// summary>
public DateTime UpdateTimestamp
{
get { return this._updateTimestamp; }
set { this._updateTimestamp = value; }
}
public Announcement()
{
this._id = -1;
}
}
}