static DBDataSourceHelper() { try { //获取数据源类型 string strDBType = ConfigurationManager.AppSettings["DBType"]; if (string.IsNullOrWhiteSpace(strDBType)) { strDBType = "MySql"; } s_dbDataSource = ObjectFactoryHelper.CreateInstance <IDBDataSource>(strDBType + "DBDataSource", "DBDataSource", true); } catch (Exception ex) { string strMessage = SysLogHelper.GetErrorLogInfo(ex, true); SysLogHelper.LogMessage("DataSourceHelper.Static", strMessage, LogLevel.Error); } }
/// <summary> /// 功能:加载组件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="configInfo"></param> /// <returns></returns> public static T Load <T>(JsonConfigInfo configInfo) { var componentTypeName = configInfo.GetString("_type"); if (componentTypeName == null) { var message = $"无法加载组件:{typeof(T).FullName}配置文件中缺少 _type 属性"; SysLogHelper.LogMessage("CacheHelper.Load", message, LogLevel.Error, WriteLogType.FileLog); throw new ArgumentOutOfRangeException(message); } var classNameArray = componentTypeName.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries); if (classNameArray.Length == 1) { var message = $"无法加载组件:{typeof(T).FullName}配置文件中缺少 _type 属性,缺少命名空间:{componentTypeName}"; SysLogHelper.LogMessage("CacheHelper.Load", message, LogLevel.Error, WriteLogType.FileLog); throw new ArgumentOutOfRangeException(message); } var @namespace = string.Join(".", classNameArray.Take(classNameArray.Length - 1)); var className = classNameArray.Last(); var target = ObjectFactoryHelper.CreateInstance <T>(className, @namespace, true); if (target == null) { var componentType = Type.GetType(componentTypeName); target = (T)Activator.CreateInstance(componentType); } JsonConvert.PopulateObject(configInfo.Itemes.ToString(), target); var component = target as IComponent; if (component != null) { component.Init(); } return(target); }
/// <summary> /// 功能:创建缓存实例 /// </summary> /// <param name="csType">缓存策略类别</param> /// <returns>缓存实例</returns> public static ICacheStrategy CreateCacheStrategy(CacheStrategyType csType) { return(ObjectFactoryHelper.CreateInstance <ICacheStrategy>(csType + "CacheStrategy", "CacheStrategy", true)); }
/// <summary> /// 功能:创建数据源实例 /// </summary> /// <param name="dbType">数据源类型</param> /// <returns>数据源实例</returns> public static IDBDataSource CreateDataSource(DBDataSourceType dbType) { return(ObjectFactoryHelper.CreateInstance <IDBDataSource>(dbType + "DBDataSource", "DBDataSource", true)); }