SPB-设计机制-扩展模块
查看全部词条命名空间:Main
导航: 上一页
除了序列化字段以外,扩展模块是SpaceBuilder提供的另外一种扩展机制。可以通过编写自己的扩展模块为SpaceBuilder增加新的功能。
一、设计原理
- 定义委托、事件:
public delegate void UserEventHandler(User user, GlobalEventArgs e);
/// <summary>
/// 创建、更新、删除用户,持久化以后触发的事件
/// </summary>
public event UserEventHandler AfterUserChange
{
add { Events.AddHandler(EventKey_AfterUserChange, value); }
remove { Events.RemoveHandler(EventKey_AfterUserChange, value); }
}
来自:SpaceBuilder.Common.GlobalEventManager
- 定义执行事件的方法,并在相应的业务逻辑类中调用:
/// <summary>
/// 创建、更新用户,持久化以后调用
/// </summary>
/// <remarks>删除用户时不允许使用该事件</remarks>
public static void AfterUserChange(User user, ObjectState state)
{
……
GlobalEventManager.Instance().ExecuteAfterUserChange(user, state);
}
来自:SpaceBuilder.Common.GlobalEvents
/// <summary>
/// 创建用户
/// </summary>
public static CreateUpdateUserStatuses Create(User user, bool sendEmail, bool ignoreDisallowNames)
{ ……
//执行BeforeUser事件
GlobalEvents.BeforeUserChange(user, ObjectState.Create);
……
//执行AfterUser事件
GlobalEvents.AfterUserChange(createdUser, ObjectState.Create);
return status;
}
来自:SpaceBuilder.Common.Users
- 定义附加模块接口,该接口包含一个Init方法,实现该接口的类中需要对步骤1中的事件注册新的事件处理程序:
/// <summary>
/// 全局事件Module接口
/// </summary>
public interface IGlobalModule
{
void Init(GlobalEventManager gem, XmlNode node);
}
来自:SpaceBuilder.Common.IGlobalModule
- 开发附加模块并实现附加模块接口:
/// <summary>
/// 个人用户注册,自动创建博客
/// </summary>
public class AutoCreateBlog : IGlobalModule
{
/// <summary>
/// 初始化(实现IGlobalModule中的方法)
/// </summary>
public void Init(GlobalEventManager pa, XmlNode node)
{
pa.AfterUserChange += new UserEventHandler(pa_PostUserUpdate);
}
/// <summary>
/// 自动创建用户博客
/// </summary>
private void pa_PostUserUpdate(User user, GlobalEventArgs e)
{
if (user != null && user.UserType == UserTypes.PersonUser && e.State == ObjectState.Create)
{
WeblogConfiguration config = WeblogConfiguration.Instance();
if (config.EnableAutoCreate)
{
Weblog w = new Weblog();
w.OwnerUserID = user.UserID;
w.WeblogName = user.DisplayName + "的博客";
w.IsActive = true;
w.EnableSearch = true;
w.EnableAggBugs = true;
//Create The Blog
Weblogs.Create(w);
}
}
}
}
来自:SpaceBuilder.Blog.Modules.AutoCreateBlog
- 在配置文件配置附加模块:
在SpaceBuilder.config的GlobalEventExtensionModules节点配置AutoCreateBlog
{<}}GlobalEventExtensionModules>
<add name = "AutoCreateBlog" type="SpaceBuilder.Blog.Modules.AutoCreateBlog, SpaceBuilder.Blog.Web" />
</GlobalEventExtensionModules>
- 对配置文件的附加模块实例化,并且调用Init方法:
/// <summary>
/// 创建实例
/// </summary>
/// <returns>返回一个GlobalEventManager的实例</returns>
internal static GlobalEventManager Instance()
{
SPBCacheManager cacheManager = SPBCacheManagerFactory.GetCacheManager
(CacheManagerNames.Instance().Configuration());
const string key = "CacheKey_GlobalEventManager";
GlobalEventManager app = cacheManager.Get(key) as GlobalEventManager;
if (app == null)
{
lock (sync)
{
app = cacheManager.Get(key) as GlobalEventManager;
if (app == null)
{
SPBConfig config = SPBConfig.Instance();
XmlNode node = config.GetConfigSection("SpaceBuilder/GlobalEventExtensionModules");
app = new GlobalEventManager();
if (node != null)
{
foreach (XmlNode n in node.ChildNodes)
{
if (n.NodeType != XmlNodeType.Comment)
{
switch (n.Name)
{
…
case "add":
string name = n.Attributes["name"].Value;
string itype = n.Attributes["type"].Value;
Type type = Type.GetType(itype);
if (type == null)
throw new Exception(itype + " does not exist");
IGlobalModule mod = Activator.CreateInstance(type) as IGlobalModule;
if (mod == null)
throw new Exception(itype + "does not implement IGlobalModule or is not configured correctly");
mod.Init(app, n);
app.modules[name] = mod;
break;
}
}
}
}
cacheManager.Add(key, app, config.ConfigFullFilePath);
}
}
}
return app;
}
#endregion来自:SpaceBuilder.Common.GlobalEventManager
二、典型用途
- 用户的blog、FileSection…自动创建;
- 积分的实现;
- 通知、动态的实现;
- 帖子内容的敏感词的清除、非法html/javascript代码的清除;
- 全文检索中需要生成索引数据的记录;