using System; using System.Collections; using System.Reflection; using CSFrameworkV4_5.Core; using CSFrameworkV4_5.Core.CodeGenerator; using CSFrameworkV4_5.Server.DataAccess.DAL_Base; namespace CSFrameworkV4_5.Server.DataAccess { /// <summary> /// 加载的DAL数据访问层的类型,注意:此类必须放在数据访问层模块 /// </summary> public static class DALTypeLoader { //哈希表, 保存已加载的DAL类的引用地址 private static Hashtable _LoadedDalTypes = null; //数据访问层的程序集 private static Assembly _DALAssembly = null; /// <summary> /// 数据访问层的程序集 /// </summary> public static Assembly DALAssembly { get { if (DALTypeLoader._DALAssembly == null) DALTypeLoader._DALAssembly = typeof(DALTypeLoader).Assembly; return DALTypeLoader._DALAssembly; } } static DALTypeLoader() { _LoadedDalTypes = new Hashtable(); } /// <summary> /// 跟据DAL类名创建数据层实例 /// </summary> /// <param name="derivedClassName">DAL类名,如:CSFrameworkV4_5.Server.DataAccess.DAL_Business.dalSO</param> /// <returns></returns> internal static Type GetDALTypeByClassName(string derivedClassName) { Type T = DALTypeLoader.DALAssembly.GetType(derivedClassName, true, true); return T; } /// <summary> /// 根据表名获取对应的DAL层 /// </summary> /// <param name="tableName">表名</param> /// <returns></returns> internal static Type GetDALTypeByTableName(string tableName) { Type _DAL_Type = null; #region 获取DAL的Type类型 //哈希表存在该类,从哈希表取出. if (_LoadedDalTypes.ContainsKey(tableName)) _DAL_Type = _LoadedDalTypes[tableName] as Type; else { //获取DataAccess程序集中所有Public类 Type[] types = DALTypeLoader.DALAssembly.GetExportedTypes(); //枚举程序集中所有Public类 foreach (Type T in types) { //只查找dalBaseDataDict的子类 if (false == T.IsSubclassOf(typeof(dalBaseDataDict))) continue; //查找DefaultORM_UpdateMode特性 object[] atts = T.GetCustomAttributes(typeof(DefaultORM_UpdateMode), false); if ((atts == null) || (atts.Length == 0)) continue; //该类有定义DefaultORM_UpdateMode特性 DefaultORM_UpdateMode att = atts[0] as DefaultORM_UpdateMode; if (ORM_Tools.GetTableName(att.ORM) == tableName) { _DAL_Type = T; break; } } //第一次加载后添加到哈希表中, 基于性能优化. if (_DAL_Type != null) _LoadedDalTypes.Add(tableName, _DAL_Type); } #endregion return _DAL_Type; } /// <summary> /// 根据ORM模型类全名获取对应的DAL层 /// </summary> /// <param name="ORM_TypeName"></param> /// <returns></returns> internal static Type GetDALTypeByORM(string ORM_TypeName) { Type _DAL_Type = null; #region 获取_DAL_Type //哈希表存在该类,从哈希表取出. if (_LoadedDalTypes.ContainsKey(ORM_TypeName)) _DAL_Type = _LoadedDalTypes[ORM_TypeName] as Type; else { // //哈希表不存在,表示第一次加载,需要从CSFrameworkV4_5.Server.DataAccess程序集中查询该类 // //获取DataAccess程序集中所有Public类 Type[] types = DALTypeLoader.DALAssembly.GetExportedTypes(); //枚举程序集中所有Public类 foreach (Type T in types) { //只查找dalBaseDataDict的子类 if (false == T.IsSubclassOf(typeof(dalBaseDataDict))) continue; //查找DefaultORM_UpdateMode特性 object[] atts = T.GetCustomAttributes(typeof(DefaultORM_UpdateMode), false); if ((atts == null) || (atts.Length == 0)) continue; //该类有定义DefaultORM_UpdateMode特性 DefaultORM_UpdateMode att = atts[0] as DefaultORM_UpdateMode; if (false == att.IsOverrideClass) continue; //仅查找具体类(子类) //比较ORM的全名是否相同 if (att.ORM.FullName.ToUpper() == ORM_TypeName.ToUpper()) { _DAL_Type = T; break; } } //第一次加载后添加到哈希表中, 基于性能优化. if (_DAL_Type != null) _LoadedDalTypes.Add(ORM_TypeName, _DAL_Type); } #endregion return _DAL_Type; } } }