WIKI使用導(dǎo)航
站長(zhǎng)百科導(dǎo)航
站長(zhǎng)專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢(qián)
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計(jì)算
- 微博營(yíng)銷
- 虛擬主機(jī)管理系統(tǒng)
- 開(kāi)放平臺(tái)
- WIKI程序與應(yīng)用
- 美國(guó)十大主機(jī)
SPB-設(shè)計(jì)機(jī)制-擴(kuò)展模塊
來(lái)自站長(zhǎng)百科
導(dǎo)航: 上一頁(yè)
除了序列化字段以外,擴(kuò)展模塊是SpaceBuilder提供的另外一種擴(kuò)展機(jī)制。可以通過(guò)編寫(xiě)自己的擴(kuò)展模塊為SpaceBuilder增加新的功能。
一、設(shè)計(jì)原理
- 定義委托、事件:
public delegate void UserEventHandler(User user, GlobalEventArgs e); /// <summary> /// 創(chuàng)建、更新、刪除用戶,持久化以后觸發(fā)的事件 /// </summary> public event UserEventHandler AfterUserChange { add { Events.AddHandler(EventKey_AfterUserChange, value); } remove { Events.RemoveHandler(EventKey_AfterUserChange, value); } }
來(lái)自:SpaceBuilder.Common.GlobalEventManager
- 定義執(zhí)行事件的方法,并在相應(yīng)的業(yè)務(wù)邏輯類中調(diào)用:
/// <summary> /// 創(chuàng)建、更新用戶,持久化以后調(diào)用 /// </summary> /// <remarks>刪除用戶時(shí)不允許使用該事件</remarks> public static void AfterUserChange(User user, ObjectState state) { …… GlobalEventManager.Instance().ExecuteAfterUserChange(user, state); } 來(lái)自:SpaceBuilder.Common.GlobalEvents /// <summary> /// 創(chuàng)建用戶 /// </summary> public static CreateUpdateUserStatuses Create(User user, bool sendEmail, bool ignoreDisallowNames) { …… //執(zhí)行BeforeUser事件 GlobalEvents.BeforeUserChange(user, ObjectState.Create); …… //執(zhí)行AfterUser事件 GlobalEvents.AfterUserChange(createdUser, ObjectState.Create); return status; }
來(lái)自:SpaceBuilder.Common.Users
- 定義附加模塊接口,該接口包含一個(gè)Init方法,實(shí)現(xiàn)該接口的類中需要對(duì)步驟1中的事件注冊(cè)新的事件處理程序:
/// <summary> /// 全局事件Module接口 /// </summary> public interface IGlobalModule { void Init(GlobalEventManager gem, XmlNode node); }
來(lái)自:SpaceBuilder.Common.IGlobalModule
- 開(kāi)發(fā)附加模塊并實(shí)現(xiàn)附加模塊接口:
/// <summary> /// 個(gè)人用戶注冊(cè),自動(dòng)創(chuàng)建博客 /// </summary> public class AutoCreateBlog : IGlobalModule { /// <summary> /// 初始化(實(shí)現(xiàn)IGlobalModule中的方法) /// </summary> public void Init(GlobalEventManager pa, XmlNode node) { pa.AfterUserChange += new UserEventHandler(pa_PostUserUpdate); } /// <summary> /// 自動(dòng)創(chuàng)建用戶博客 /// </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); } } } }
來(lái)自:SpaceBuilder.Blog.Modules.AutoCreateBlog
- 在配置文件配置附加模塊:
在SpaceBuilder.config的GlobalEventExtensionModules節(jié)點(diǎn)配置AutoCreateBlog
{<}}GlobalEventExtensionModules> <add name = "AutoCreateBlog" type="SpaceBuilder.Blog.Modules.AutoCreateBlog, SpaceBuilder.Blog.Web" /> </GlobalEventExtensionModules>
- 對(duì)配置文件的附加模塊實(shí)例化,并且調(diào)用Init方法:
/// <summary> /// 創(chuàng)建實(shí)例 /// </summary> /// <returns>返回一個(gè)GlobalEventManager的實(shí)例</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來(lái)自:SpaceBuilder.Common.GlobalEventManager
二、典型用途
- 用戶的blog、FileSection…自動(dòng)創(chuàng)建;
- 積分的實(shí)現(xiàn);
- 通知、動(dòng)態(tài)的實(shí)現(xiàn);
- 帖子內(nèi)容的敏感詞的清除、非法html/javascript代碼的清除;
- 全文檢索中需要生成索引數(shù)據(jù)的記錄;