using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using Zhaizj.Framework.Data; using Zhaizj.Framework.Reflection; namespace Zhaizj.Framework.ORM { /// /// 实体类的元数据信息 /// [Serializable] public class EntityInfo { private Assembly _assembly; private List _childEntityList = new List(); private List _entityPropertyList = new List(); private List _savedPropertyList = new List(); private List _PropertyListAll = new List(); private Hashtable _propertyHashTable = new Hashtable(); private Hashtable _propertyHashTableByColumn = new Hashtable(); private String _columnList; private String _tableName; private Type _type; private String _fullName; private String _label; private String _name; private EntityPropertyInfo _relationProperty; private DatabaseType _dbtype = DatabaseType.Other; private IDatabaseDialect _dialect; /// /// 实体类在 dbconfig 配置文件中所对应的数据库名称 /// public String Database { get; set; } /// /// 数据库类型 /// public DatabaseType DbType { get { if (_dbtype == DatabaseType.Other) { _dbtype = getDbType(); } return _dbtype; } } /// /// 实体类的 dialect /// public IDatabaseDialect Dialect { get { if (_dialect == null) { _dialect = DataFactory.GetDialect( this.DbType ); } return _dialect; } } private DatabaseType getDbType() { DatabaseType dbtype = getDbTypeFromConfig(); if (dbtype != DatabaseType.Other) return dbtype; return DbTypeChecker.GetDatabaseType( DbConfig.GetConnectionString( this.Database ) ); } private DatabaseType getDbTypeFromConfig() { if (DbConfig.Instance.DbType.ContainsKey( this.Database )) { return DbConfig.Instance.GetConnectionStringMap()[this.Database].DbType; } return DatabaseType.Other; } /// /// 所属的程序集 /// public Assembly Assembly { get { return _assembly; } set { _assembly = value; } } /// /// 所有实体类属性的 EntityInfo 的列表 /// public List ChildEntityList { get { return _childEntityList; } set { _childEntityList = value; } } /// /// 对应的数据表中的所有列的名称 /// public String ColumnList { get { return _columnList; } set { _columnList = value; } } /// /// 只是实体性质的属性的列表,比如 BlogPost 的某个属性是 BlogCategory 这样的实体类型 /// public List EntityPropertyList { get { return _entityPropertyList; } set { _entityPropertyList = value; } } /// /// 所有属性的列表(属性已经封装成EntityPropertyInfo) /// public List PropertyListAll { get { return _PropertyListAll; } set { _PropertyListAll = value; } } /// /// 所有需要保存的属性 /// public List SavedPropertyList { get { return _savedPropertyList; } set { _savedPropertyList = value; } } /// /// 实体类全名,比如 Zhaizj.Framework.apps.BlogApp /// public String FullName { get { return _fullName; } set { _fullName = value; } } /// /// 实体类在表单中的名称,用于表单代码自动生成 /// public String Label { get { return _label; } set { _label = value; } } /// /// 实体类名称,等同于type.Name,比如BlogApp /// public String Name { get { return _name; } set { _name = value; } } /// /// 当前实体类的父类,如果它是继承于某个基类的话 /// public EntityInfo Parent { get { if (Type.BaseType.IsAbstract || OrmHelper.IsEntityBase( Type.BaseType )) {// 1029 return null; } return (MappingClass.Instance.ClassList[Type.BaseType.FullName] as EntityInfo); } } /// /// 实体类对应的数据表名称 /// public String TableName { get { return _tableName; } set { _tableName = value; } } /// /// 实体类对应的Type /// public Type Type { get { return _type; } set { _type = value; } } private static String addPrefixToTableName( String tableName ) { if (strUtil.HasText( DbConfig.Instance.TablePrefix )) { tableName = tableName.Replace( "[", "" ).Replace( "]", "" ); tableName = DbConfig.Instance.TablePrefix + tableName; } return tableName; } internal void AddPropertyToHashtable( EntityPropertyInfo p ) { _propertyHashTable[p.Name] = p; if (strUtil.HasText( p.ColumnName )) { _propertyHashTableByColumn[p.ColumnName.ToLower()] = p; } } internal EntityPropertyInfo FindRelationProperty( Type t ) { if (_relationProperty == null) { for (int i = 0; i < EntityPropertyList.Count; i++) { EntityPropertyInfo info = EntityPropertyList[i]; if (info.Type != t) { _relationProperty = info; } } } return _relationProperty; } /// /// 根据类型Type,初始化EntityInfo;注意:因为不是从缓存中取,所以速度较慢 /// /// /// internal static EntityInfo GetByType( Type t ) { EntityInfo info = new EntityInfo(); info.Type = t; info.Name = t.Name; info.FullName = t.FullName; info.TableName = addPrefixToTableName( GetTableName( t ) ); info.Database = getDatabase( t ); checkCustomMapping( info ); info.Label = getTypeLabel( t ); IList propertyList = ReflectionUtil.GetPropertyList( t ); for (int i = 0; i < propertyList.Count; i++) { PropertyInfo property = propertyList[i] as PropertyInfo; EntityPropertyInfo ep = EntityPropertyInfo.Get( property ); ep.ParentEntityInfo = info; if (!(!ep.SaveToDB || ep.IsList)) { info.SavedPropertyList.Add( ep ); } info.PropertyListAll.Add( ep ); } if (info.SavedPropertyList.Count == 1) { throw new Exception( "class's properties have not been setted '[save]' attribute." ); } return info; } private static void checkCustomMapping( EntityInfo info ) { Dictionary map = DbConfig.Instance.GetMappingInfo(); if (map.ContainsKey( info.Type.FullName )) { MappingInfo mi = map[info.Type.FullName]; if (strUtil.HasText( mi.Table )) info.TableName = mi.Table; if (strUtil.HasText( mi.Database )) info.Database = mi.Database; } } /// /// 获取某个属性在数据库中对应的数据列名称 /// /// /// public String GetColumnName( String propertyName ) { for (int i = 0; i < _savedPropertyList.Count; i++) { EntityPropertyInfo ep = _savedPropertyList[i] as EntityPropertyInfo; if (ep.Name == propertyName) { return ep.ColumnName; } } return null; } /// /// 获取某个属性的元数据信息(已封装成EntityPropertyInfo) /// /// /// public EntityPropertyInfo GetProperty( String propertyName ) { return (_propertyHashTable[propertyName] as EntityPropertyInfo); } /// /// 根据column名称,获取获取某个属性的元数据信息(已封装成EntityPropertyInfo) /// /// /// public EntityPropertyInfo GetPropertyByColumn( String columnName ) { return (_propertyHashTableByColumn[columnName.ToLower()] as EntityPropertyInfo); } /// /// 根据属性的类型,比如BlogCategory,获取符合要求的第一个属性的名称 /// /// /// public String GetPropertyName( Type propertyType ) { for (int i = 0; i < _savedPropertyList.Count; i++) { EntityPropertyInfo ep = _savedPropertyList[i] as EntityPropertyInfo; if (ep.Type.FullName == propertyType.FullName) { return ep.Name; } } return null; } internal String GetRelationPropertyName( Type propertyType ) { String name = null; for (int i = 0; i < _savedPropertyList.Count; i++) { EntityPropertyInfo ep = _savedPropertyList[i] as EntityPropertyInfo; if (ep.Type.FullName == propertyType.FullName) { return ep.Name; } if (propertyType.IsSubclassOf( ep.Type )) { name = ep.Name; } } return name; } private static String GetTableName( Type t ) { TableAttribute attribute = ReflectionUtil.GetAttribute( t, typeof( TableAttribute ) ) as TableAttribute; if (attribute == null) { return t.Name; } return attribute.TableName; } private static String getDatabase( Type t ) { DatabaseAttribute attribute = ReflectionUtil.GetAttribute( t, typeof( DatabaseAttribute ) ) as DatabaseAttribute; if (attribute == null) { return DbConfig.DefaultDbName; } return attribute.Database; } private static String getTypeLabel( Type t ) { LabelAttribute attribute = ReflectionUtil.GetAttribute( t, typeof( LabelAttribute ) ) as LabelAttribute; if (attribute == null) { return t.Name; } return attribute.Label; } } }