/// <summary> /// 注册缓存 /// </summary> /// <param name="modelType"></param> private static DbCacheDescriptor RegisterCache(Type modelType) { DbCacheDescriptor des = new DbCacheDescriptor(); object[] databaseAttrs = modelType.GetCustomAttributes(typeof(DbDatabaseAttribute), false); if (null != databaseAttrs && databaseAttrs.Length > 0) { des.DbDatabase = databaseAttrs[0] as DbDatabaseAttribute; } else { throw new Exception("无法注册DB模型,缺少DBDatabaseAttribute"); } object[] tableAttrs = modelType.GetCustomAttributes(typeof(DbTableAttribute), false); if (null != tableAttrs && tableAttrs.Length > 0) { des.DbTable = tableAttrs[0] as DbTableAttribute; } else { throw new Exception("无法注册DB模型,缺少DBTableAttribute"); } des.DbColumns = new Dictionary <PropertyInfo, DbColumnAttribute>(); PropertyInfo[] pis = modelType.GetProperties(s_propertyBindingFlags); foreach (PropertyInfo pi in pis) { object[] columnAttrs = pi.GetCustomAttributes(typeof(DbColumnAttribute), false); if (null != columnAttrs && columnAttrs.Length > 0) { DbColumnAttribute column = columnAttrs[0] as DbColumnAttribute; column.PropertyNameMapping = pi.Name; des.DbColumns.Add(pi, column); } } return(des); }
/// <summary> /// 获取列信息 /// </summary> /// <param name="modelType">模型类型</param> /// <returns></returns> public static Dictionary <PropertyInfo, DbColumnAttribute> GetColumns(Type modelType) { if (null == modelType) { throw new ArgumentNullException("The Method 'GetColumn' Of Parameter 'modelType' is null"); } if (!s_modelCache.ContainsKey(modelType.FullName)) { object operatekey = GetKey(modelType); lock (operatekey) { if (!s_modelCache.ContainsKey(modelType.FullName)) { DbCacheDescriptor register = RegisterCache(modelType); s_modelCache.Add(modelType.FullName, register); } } } DbCacheDescriptor dbTableDes = s_modelCache[modelType.FullName]; return(dbTableDes.DbColumns); }
/// <summary> /// 获取数据实例信息 /// </summary> /// <param name="modelType">模型类型</param> /// <returns></returns> public static DbDatabaseAttribute GetDBDatabase(Type modelType) { if (null == modelType) { throw new ArgumentNullException("The Method 'GetDBInstance' Of Parameter 'modelType' is null"); } if (!s_modelCache.ContainsKey(modelType.FullName)) { object operatekey = GetKey(modelType); lock (operatekey) { if (!s_modelCache.ContainsKey(modelType.FullName)) { DbCacheDescriptor register = RegisterCache(modelType); s_modelCache.Add(modelType.FullName, register); } } } DbCacheDescriptor dbTableDes = s_modelCache[modelType.FullName]; return(dbTableDes.DbDatabase); }