久久精品水蜜桃av综合天堂,久久精品丝袜高跟鞋,精品国产肉丝袜久久,国产一区二区三区色噜噜,黑人video粗暴亚裔

SPB-設計機制-擴展模塊

來自站長百科
跳轉至: 導航、? 搜索

導航: 上一頁

除了序列化字段以外,擴展模塊是SpaceBuilder提供的另外一種擴展機制??梢酝ㄟ^編寫自己的擴展模塊為SpaceBuilder增加新的功能。

一、設計原理

  • 定義委托、事件:
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); }
}

來自:SpaceBuilder.Common.GlobalEventManager

  • 定義執(zhí)行事件的方法,并在相應的業(yè)務邏輯類中調用:
    /// <summary>
    /// 創(chuàng)建、更新用戶,持久化以后調用
    /// </summary>        
    /// <remarks>刪除用戶時不允許使用該事件</remarks>
    public static void AfterUserChange(User user, ObjectState state)
    {
        ……        
        GlobalEventManager.Instance().ExecuteAfterUserChange(user, state);
}

來自: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;
}

來自:SpaceBuilder.Common.Users

  • 定義附加模塊接口,該接口包含一個Init方法,實現(xiàn)該接口的類中需要對步驟1中的事件注冊新的事件處理程序:
/// <summary>
    /// 全局事件Module接口
    /// </summary>
    public interface IGlobalModule
    {
        void Init(GlobalEventManager gem, XmlNode node);
}

來自:SpaceBuilder.Common.IGlobalModule

  • 開發(fā)附加模塊并實現(xiàn)附加模塊接口:
   /// <summary>
    /// 個人用戶注冊,自動創(chuàng)建博客
    /// </summary>
    public class AutoCreateBlog : IGlobalModule
    {
        /// <summary>
        /// 初始化(實現(xiàn)IGlobalModule中的方法)
        /// </summary>
       public void Init(GlobalEventManager pa, XmlNode node)
        {
            pa.AfterUserChange += new UserEventHandler(pa_PostUserUpdate);
        }
/// <summary>
        /// 自動創(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);
                }
            }
        }
    }

來自:SpaceBuilder.Blog.Modules.AutoCreateBlog

  • 在配置文件配置附加模塊:

在SpaceBuilder.config的GlobalEventExtensionModules節(jié)點配置AutoCreateBlog

{<}}GlobalEventExtensionModules>
<add name = "AutoCreateBlog" type="SpaceBuilder.Blog.Modules.AutoCreateBlog, SpaceBuilder.Blog.Web" />
	</GlobalEventExtensionModules>
  • 對配置文件的附加模塊實例化,并且調用Init方法:
/// <summary>
    /// 創(chuàng)建實例
    /// </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

二、典型用途

  1. 用戶的blog、FileSection…自動創(chuàng)建;
  2. 積分的實現(xiàn);
  3. 通知、動態(tài)的實現(xiàn);
  4. 帖子內容的敏感詞的清除、非法html/javascript代碼的清除;
  5. 全文檢索中需要生成索引數(shù)據(jù)的記錄;

參考資料[ ]