示例#1
0
文件: Model.cs 项目: gonbike/Destrier
        public static List <Member> GenerateMembers(Type type, Member rootMember = null, Member parentMember = null)
        {
            List <Member> members = new List <Member>();

            foreach (var cm in GenerateColumnMembers(type))
            {
                if (!cm.Skip)
                {
                    cm.Parent = parentMember;
                    cm.Root   = rootMember;
                    members.Add(cm);
                }
            }
            if (ModelCache.HasReferencedObjectMembers(type))
            {
                foreach (var rom in GenerateReferencedObjectMembers(type))
                {
                    rom.Parent = parentMember;
                    rom.Root   = rootMember;
                    members.Add(rom);
                }
            }
            if (ModelCache.HasChildCollectionMembers(type))
            {
                foreach (var ccm in GenerateChildCollectionMembers(type))
                {
                    ccm.Parent = parentMember;
                    ccm.Root   = rootMember;
                    members.Add(ccm);
                }
            }
            return(members);
        }
示例#2
0
文件: Model.cs 项目: gonbike/Destrier
        public static Boolean IsModel(Type t)
        {
            var table_attribute   = ModelCache.GetTableAttribute(t);
            var column_attributes = ModelCache.GetColumnMembers(t);

            return(table_attribute != null && column_attributes != null && column_attributes.Any());
        }
示例#3
0
文件: Model.cs 项目: gonbike/Destrier
        public static void Populate(object instance, IndexedSqlDataReader dr)
        {
            var thisType = instance.GetType();
            var members  = ModelCache.GetColumnMemberLookup(thisType);

            foreach (var col in members.Values)
            {
                col.SetValue(instance, dr.Get(col));
            }
        }
示例#4
0
文件: Model.cs 项目: gonbike/Destrier
        public static ChildCollectionMember[] GenerateChildCollectionMembers(Type type)
        {
            var list = new List <ChildCollectionMember>();

            PropertyInfo[] properties = ModelCache.GetChildCollectionMemberProperties(type);
            foreach (var prop in properties)
            {
                list.Add(new ChildCollectionMember(prop));
            }

            return(list.ToArray());
        }
示例#5
0
文件: Model.cs 项目: gonbike/Destrier
        public static ReferencedObjectMember[] GenerateReferencedObjectMembers(Type type)
        {
            var list = new List <ReferencedObjectMember>();

            PropertyInfo[] properties = ModelCache.GetReferencedObjectMemberProperties(type);
            foreach (var prop in properties)
            {
                list.Add(new ReferencedObjectMember(prop));
            }

            return(list.ToArray());
        }
示例#6
0
文件: Model.cs 项目: gonbike/Destrier
        public static ColumnMember[] GenerateColumnMembers(Type type)
        {
            PropertyInfo[] properties = ModelCache.GetColumnMemberProperties(type);
            var            members    = new List <ColumnMember>();

            foreach (var prop in properties)
            {
                members.Add(new ColumnMember(prop));
            }

            return(members.ToArray());
        }
示例#7
0
文件: Model.cs 项目: gonbike/Destrier
        static void _generateMembersImpl(Type type, List <Member> members, Member rootMember, Member parentMember = null)
        {
            foreach (var cm in GenerateColumnMembers(type))
            {
                if (!cm.Skip)
                {
                    cm.Parent = parentMember;
                    cm.Root   = rootMember;
                    members.Add(cm);
                }
            }

            if (ModelCache.HasReferencedObjectMembers(type))
            {
                foreach (var rom in GenerateReferencedObjectMembers(type))
                {
                    rom.Parent = parentMember;
                    rom.Root   = rootMember;

                    if (!rom.HasCycle)
                    {
                        members.Add(rom);
                        _generateMembersImpl(rom.Type, members, rootMember, rom);
                    }
                }
            }

            if (ModelCache.HasChildCollectionMembers(type))
            {
                foreach (var ccp in GenerateChildCollectionMembers(type))
                {
                    ccp.Parent = parentMember;
                    ccp.Root   = rootMember;

                    if (parentMember != null && parentMember is ReferencedObjectMember)
                    {
                        ccp.ParentReferencedMember.Parent = parentMember;
                    }

                    if (!ccp.HasCycle)
                    {
                        members.Add(ccp);
                        _generateMembersImpl(ccp.CollectionType, members, rootMember, ccp);
                    }
                }
            }
        }
示例#8
0
文件: Model.cs 项目: gonbike/Destrier
        public static String TableName(Type t)
        {
            var ta = ModelCache.GetTableAttribute(t);

            if (ta == null)
            {
                return(null);
            }

            if (!String.IsNullOrEmpty(ta.TableName))
            {
                return(ta.TableName);
            }
            else
            {
                return(t.Name);
            }
        }
示例#9
0
文件: Model.cs 项目: gonbike/Destrier
        public static void CheckColumns(Type t)
        {
            if (ModelCache.GetTableAttribute(t) != null)
            {
                var databaseColumns = Schema.GetColumnsForTable(Model.TableName(t), Model.DatabaseName(t), Model.ConnectionString(t));
                var columnMembers   = Model.GenerateColumnMembers(t);

                foreach (var cm in columnMembers)
                {
                    var modelColumn = cm.ColumnAttribute;
                    if (!modelColumn.IsForReadOnly)
                    {
                        if (!databaseColumns.Any(c => c.Name.Equals(cm.Name, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            throw new ColumnMissingException(String.Format("\'{0}\' : Column in the model doesn't map to the schema.", cm.Name));
                        }

                        var databaseColumn = databaseColumns.FirstOrDefault(c => c.Name.Equals(cm.Name, StringComparison.InvariantCultureIgnoreCase));

                        if (!modelColumn.IsPrimaryKey && databaseColumn.CanBeNull != modelColumn.CanBeNull)
                        {
                            throw new ColumnNullabilityException(String.Format("Column Nullability Mismatch : {4} DBColumn: {0} {2} ModelColumn: {1} {3}"
                                                                               , databaseColumn.Name
                                                                               , cm.Name
                                                                               , databaseColumn.CanBeNull.ToString()
                                                                               , modelColumn.CanBeNull.ToString()
                                                                               , Model.TableName(t))
                                                                 );
                        }
                    }
                }

                foreach (var column in databaseColumns)
                {
                    if (!column.IsForReadOnly)
                    {
                        if (!columnMembers.Any(c => c.Name.Equals(column.Name, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            throw new ColumnMissingException(String.Format("\'{0}\' : Column in the schema ({1}) doesn't map to the model.", column.Name, Model.TableName(t)));
                        }
                    }
                }
            }
        }
示例#10
0
文件: Model.cs 项目: gonbike/Destrier
        public static String ConnectionString(Type t)
        {
            var ta = ModelCache.GetTableAttribute(t);

            if (ta == null)
            {
                throw new SchemaMetadataException("Base Model classes must have a 'Table' attribute specifying the relation in the database to interact with!");
            }

            if (!String.IsNullOrEmpty(ta.ConnectionName))
            {
                return(DatabaseConfigurationContext.ConnectionStrings[ta.ConnectionName]);
            }
            else if (!String.IsNullOrEmpty(DatabaseConfigurationContext.DefaultConnectionString))
            {
                return(DatabaseConfigurationContext.DefaultConnectionString);
            }
            else
            {
                throw new SchemaMetadataException("No connection string for object.");
            }
        }
示例#11
0
文件: Model.cs 项目: gonbike/Destrier
 public static ColumnMember[] ColumnsPrimaryKey(Type t)
 {
     return(ModelCache.GetColumnMembers(t).Where(cm => cm.IsPrimaryKey).ToArray());
 }
示例#12
0
文件: Model.cs 项目: gonbike/Destrier
        public static String ConnectionName(Type t)
        {
            TableAttribute ta = ModelCache.GetTableAttribute(t);

            return(ta.ConnectionName ?? DatabaseConfigurationContext.DefaultConnectionName);
        }
示例#13
0
文件: Model.cs 项目: gonbike/Destrier
 public static ColumnMember ColumnMemberForPropertyName(Type type, String propertyName)
 {
     return(ModelCache.GetColumnMemberStandardizedLookup(type)[Model.StandardizeCasing(propertyName)]);
 }