[kooboo] 应用程序启动分析

从 Niject Loader 看到过 , [assembly: System.Web.PreApplicationStartMethod ….]

这次再看一下 kooboo , 也同样通过这样的方式,启动代码方式,有意思,

 

先是注册启动方法

 

[assembly: System.Web.PreApplicationStartMethod(typeof(Kooboo.CMS.Sites.Providers.SqlServer.AssemblyInitializer), "Initialize")]
namespace Kooboo.CMS.Sites.Providers.SqlServer
{
    public static class AssemblyInitializer
    {
        public static void Initialize()
        {
            ApplicationInitialization.RegisterInitializerMethod(delegate()
            {
                ResetProviders();
            }, 1);
        }
//.......
}
}

 

然后在 Global.asax ,

   protected void Application_Start()
        {
            //execute the initializer method.
            ApplicationInitialization.Execute();      
 // .....
 }

namespace Kooboo
{
    public static class ApplicationInitialization
    {
        private class InitializationItem
        {
            public Action InitializeMethod { get; set; }
            public int Priority { get; set; }
        }
        private static List<InitializationItem> items = new List<InitializationItem>();
        public static void RegisterInitializerMethod(Action method, int priority)
        {
            items.Add(new InitializationItem() { InitializeMethod = method, Priority = priority });
        }
        public static void Execute()
        {
            lock (items)
            {
                foreach (var item in items.OrderBy(it => it.Priority))
                {
                    item.InitializeMethod();
                }
                items.Clear();
            }
 
        }
    }
}

posted @ 2013-01-14 11:16  张保维  阅读(280)  评论(0编辑  收藏  举报