コード例 #1
0
ファイル: TableInfoCache.cs プロジェクト: FredBao/ORMs
 public void InsertTableInfo(Type T, TableInfo tableInfo)
 {
     _TableInfoCache._TableInfos.Add(T, tableInfo);
 }
コード例 #2
0
ファイル: TableInfo.cs プロジェクト: FredBao/ORMs
        //public TableInfo GetTableInfo(IEntity t)
        //{
        //    if (t == null)
        //    {
        //        throw new Exception("Entity cannot be null");
        //    }
        //    return GetTableInfo(t.GetType());
        //}
        public TableInfo GetTableInfo(Type type)
        {
            //TableInfo tableInfo = EntityTypeCache.GetTableInfo(type);
            //if (tableInfo == null)
            //{
            //    EntityTypeCache.InsertTableInfo(type, tableInfo);
            //}
            //return tableInfo;
            if (type == null)
            {
                throw new Exception("type cannot be null");
            }
            TableInfo tableInfo = new TableInfo();
            var TableAttributes = type.CustomAttributes;
            if (TableAttributes.Count() == LENGTH)
            {
                string tablename = TableAttributes.FirstOrDefault().ConstructorArguments.FirstOrDefault().Value.ToString();
                string primarykeyname = TableAttributes.FirstOrDefault().ConstructorArguments.LastOrDefault().Value.ToString();
                tableInfo.table = new SqlServerTableAttribute(tablename, primarykeyname);
            }

            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                tableInfo.DicProperties.Add(property.Name, property);
                var ColumnAttributes = property.CustomAttributes;
                if (ColumnAttributes.Count() == LENGTH)
                {
                    SqlServerColumnAttribute sqlservercolumnattribute = new SqlServerColumnAttribute(
                        (string)ColumnAttributes.FirstOrDefault().ConstructorArguments[0].Value,
                        (bool)ColumnAttributes.FirstOrDefault().ConstructorArguments[1].Value,
                        (bool)ColumnAttributes.FirstOrDefault().ConstructorArguments[2].Value,
                        (SqlServerDataType)ColumnAttributes.FirstOrDefault().ConstructorArguments[3].Value,
                        (int)ColumnAttributes.FirstOrDefault().ConstructorArguments[4].Value,
                       (bool)ColumnAttributes.FirstOrDefault().ConstructorArguments[5].Value,
                        (bool)ColumnAttributes.FirstOrDefault().ConstructorArguments[6].Value
                    );
                    tableInfo.DicColumns.Add(property.Name, sqlservercolumnattribute);
                    tableInfo.columns.Add(sqlservercolumnattribute);
                }
                //if (property.GetCustomAttributes(typeof(LinkTableAttribute), false).Length == LENGTH)
                //{
                //    tableInfo.DicLinkTable.Add(property.Name, property.GetCustomAttributes(typeof(LinkTableAttribute), false)[0] as LinkTableAttribute);
                //}
                //if (property.GetCustomAttributes(typeof(LinkTablesAttribute), false).Length == LENGTH)
                //{
                //    tableInfo.DicLinkTables.Add(property.Name, property.GetCustomAttributes(typeof(LinkTablesAttribute), false)[0] as LinkTablesAttribute);
                //}
            }
            //FieldInfo[] fields = type.GetFields();
            //foreach (FieldInfo field in fields)
            //{
            //    tableInfo.DicFields.Add(field.Name, field);
            //}

            //if (tableAttribute.Length == LENGTH)
            //{
            //    tableInfo.Table = tableAttribute[0];
            //}
            //else
            //{
            //    throw new Exception("一个实体类上不能有相同的特性");
            //}
            //tableInfo.Columns = tableInfo.DicColumns.Values.ToArray();
            //tableInfo.Fields = tableInfo.DicFields.Values.ToArray();
            //tableInfo.LinkTable = tableInfo.DicLinkTable.Values.ToArray();
            //tableInfo.LinkTables = tableInfo.DicLinkTables.Values.ToArray();
            //tableInfo.Properties = tableInfo.DicProperties.Values.ToArray();
            TableInfoCache.GetInstance().InsertTableInfo(type, tableInfo);
            return tableInfo;
        }