protected void OnStartUp()
        {
            rootPath = Path.GetDirectoryName(
                Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory));

            XmlTextReader configReader = new XmlTextReader(new MemoryStream(Properties.Resources.Configuration));
            Configuration cfg = new Configuration();
            cfg.Configure(configReader);
            cfg.AddDirectory(new DirectoryInfo(Path.Combine(rootPath, "Hbm")));


            string strConnection = string.Format(ConfigurationManager.ConnectionStrings["dbc2"].ConnectionString, rootPath);
            cfg.SetProperty("connection.connection_string", strConnection);

            if (BeforeBuilding != null)
                this.BeforeBuilding.Invoke(cfg);

            sessionFactory = cfg.BuildSessionFactory();
        }
Пример #2
0
        /// <summary>
        /// 读取配置文件,根据配置文件中的应用程序 节点,完成sessionFactory的初始化
        /// </summary>
        public override void Config()
        {
            sessionFactoryCache = new Dictionary<string, ISessionFactory>();
            foreach (TelChina.AF.Sys.Configuration.AddElement item in AFConfigurationManager.PLGroup.Storages)
            {
                var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, item.FileName);
                var config = new NHibernate.Cfg.Configuration();

                var entityListener = new EntityListener();
                config.EventListeners.PostLoadEventListeners = new IPostLoadEventListener[] {entityListener};
                config.EventListeners.PostUpdateEventListeners = new IPostUpdateEventListener[] {entityListener};
                config.EventListeners.PostInsertEventListeners = new IPostInsertEventListener[] {entityListener};
                config.EventListeners.PostDeleteEventListeners = new IPostDeleteEventListener[] {entityListener};

                config.Configure(fileName);

                //设置实体映射文件关联
                var mappingFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, CONFIGPATH, MAPPINGPATH);
                config.AddDirectory(new System.IO.DirectoryInfo(mappingFileName));

                sessionFactoryCache.Add(item.AppName, config.BuildSessionFactory());
                Console.WriteLine(string.Format("{0}对应的sessionfactory已经创建", item.AppName));
            }
            if(sessionFactoryCache.Count <1)
            {
                throw new UnhandledException("NHibernate相关配置不正确");
            }
            //加载ibatis的相关配置,用于动态查询
            DynamicSqlBuilder.InitSqlMapper();
        }