Example #1
0
        void InitMemberInfo()
        {
            Type t       = this.EntityType;
            var  members = t.GetMembers(BindingFlags.Public | BindingFlags.Instance);

            Dictionary <MemberInfo, MappingMemberDescriptor>    mappingMemberDescriptors    = new Dictionary <MemberInfo, MappingMemberDescriptor>();
            Dictionary <MemberInfo, NavigationMemberDescriptor> navigationMemberDescriptors = new Dictionary <MemberInfo, NavigationMemberDescriptor>();

            foreach (var member in members)
            {
                var ignoreFlags = member.GetCustomAttributes(typeof(NotMappedAttribute), false);
                if (ignoreFlags.Count() > 0)
                {
                    continue;
                }

                Type         memberType = null;
                PropertyInfo prop       = null;
                FieldInfo    field      = null;

                if ((prop = member as PropertyInfo) != null)
                {
                    if (prop.GetSetMethod() == null)
                    {
                        continue;//对于没有公共的 setter 直接跳过
                    }
                    memberType = prop.PropertyType;
                }
                else if ((field = member as FieldInfo) != null)
                {
                    memberType = field.FieldType;
                }
                else
                {
                    continue;//只支持公共属性和字段
                }
                if (Utils.IsMapType(memberType))
                {
                    MappingMemberDescriptor memberDescriptor = this.ConstructDbFieldDescriptor(member);
                    mappingMemberDescriptors.Add(member, memberDescriptor);
                }
                else
                {
                    var associationFlags = member.GetCustomAttributes(typeof(AssociationAttribute), true);
                    if (associationFlags.Count() > 0)
                    {
                        AssociationAttribute       associationFlag            = (AssociationAttribute)associationFlags.First();
                        NavigationMemberDescriptor navigationMemberDescriptor = null;
                        if (member.MemberType == MemberTypes.Property)
                        {
                            navigationMemberDescriptor = new NavigationPropertyDescriptor(prop, this, associationFlag.ThisKey, associationFlag.AssociatingKey);
                        }
                        else if (member.MemberType == MemberTypes.Field)
                        {
                            navigationMemberDescriptor = new NavigationFieldDescriptor(field, this, associationFlag.ThisKey, associationFlag.AssociatingKey);
                        }
                        else
                        {
                            continue;
                        }

                        navigationMemberDescriptors.Add(member, navigationMemberDescriptor);
                    }

                    continue;
                }
            }

            this._mappingMemberDescriptors    = Utils.Clone(mappingMemberDescriptors);
            this._navigationMemberDescriptors = Utils.Clone(navigationMemberDescriptors);
        }
Example #2
0
        void InitMemberInfo()
        {
            List <MappingMemberDescriptor> mappingMemberDescriptors = this.ExtractMappingMemberDescriptors();

            int primaryKeyCount = mappingMemberDescriptors.Where(a => a.IsPrimaryKey).Count();

            if (primaryKeyCount > 1)
            {
                throw new NotSupportedException(string.Format("The entity type '{0}' can't define multiple primary keys.", this.EntityType.FullName));
            }
            else if (primaryKeyCount == 1)
            {
                this._primaryKey = mappingMemberDescriptors.Where(a => a.IsPrimaryKey).First();
            }
            else
            {
                //如果没有定义任何主键,则从所有映射的属性中查找名为 id 的属性作为主键
                MappingMemberDescriptor idNameMemberDescriptor = mappingMemberDescriptors.Where(a => a.MemberInfo.Name.ToLower() == "id" && !a.IsDefined(typeof(ColumnAttribute))).FirstOrDefault();

                if (idNameMemberDescriptor != null)
                {
                    idNameMemberDescriptor.IsPrimaryKey = true;
                    this._primaryKey = idNameMemberDescriptor;
                }
            }

            List <MappingMemberDescriptor> autoIncrementMemberDescriptors = mappingMemberDescriptors.Where(a => a.IsDefined(typeof(AutoIncrementAttribute))).ToList();

            if (autoIncrementMemberDescriptors.Count > 1)
            {
                throw new NotSupportedException(string.Format("The entity type '{0}' can not define multiple autoIncrement members.", this.EntityType.FullName));
            }
            else if (autoIncrementMemberDescriptors.Count == 1)
            {
                MappingMemberDescriptor autoIncrementMemberDescriptor = autoIncrementMemberDescriptors[0];
                if (autoIncrementMemberDescriptor.IsDefined(typeof(NonAutoIncrementAttribute)))
                {
                    throw new ChloeException(string.Format("Can't define both 'AutoIncrementAttribute' and 'NotAutoIncrementAttribute' on the same mapping member '{0}'.", autoIncrementMemberDescriptor.MemberInfo.Name));
                }

                if (!IsAutoIncrementType(autoIncrementMemberDescriptor.MemberInfoType))
                {
                    throw new ChloeException("AutoIncrement member type must be Int16,Int32 or Int64.");
                }

                autoIncrementMemberDescriptor.IsAutoIncrement = true;
                this._autoIncrement = autoIncrementMemberDescriptor;
            }
            else
            {
                MappingMemberDescriptor defaultAutoIncrementMemberDescriptor = mappingMemberDescriptors.Where(a => a.IsPrimaryKey && IsAutoIncrementType(a.MemberInfoType) && !a.IsDefined(typeof(NonAutoIncrementAttribute))).FirstOrDefault();
                if (defaultAutoIncrementMemberDescriptor != null)
                {
                    defaultAutoIncrementMemberDescriptor.IsAutoIncrement = true;
                    this._autoIncrement = defaultAutoIncrementMemberDescriptor;
                }
            }

            this._mappingMemberDescriptors = new Dictionary <MemberInfo, MappingMemberDescriptor>(mappingMemberDescriptors.Count);
            foreach (MappingMemberDescriptor mappingMemberDescriptor in mappingMemberDescriptors)
            {
                this._mappingMemberDescriptors.Add(mappingMemberDescriptor.MemberInfo, mappingMemberDescriptor);
            }
        }
Example #3
0
        void InitMemberInfo()
        {
            List <MappingMemberDescriptor> mappingMemberDescriptors = this.ExtractMappingMemberDescriptors();

            List <MappingMemberDescriptor> primaryKeys = mappingMemberDescriptors.Where(a => a.IsPrimaryKey).ToList();

            if (primaryKeys.Count == 0)
            {
                //如果没有定义任何主键,则从所有映射的属性中查找名为 id 的属性作为主键
                MappingMemberDescriptor idNameMemberDescriptor = mappingMemberDescriptors.Where(a => a.MemberInfo.Name.ToLower() == "id" && !a.IsDefined(typeof(ColumnAttribute))).FirstOrDefault();

                if (idNameMemberDescriptor != null)
                {
                    idNameMemberDescriptor.IsPrimaryKey = true;
                    primaryKeys.Add(idNameMemberDescriptor);
                }
            }

            this._primaryKeys = primaryKeys.AsReadOnly();

            List <MappingMemberDescriptor> autoIncrementMemberDescriptors = mappingMemberDescriptors.Where(a => a.IsDefined(typeof(AutoIncrementAttribute))).ToList();

            if (autoIncrementMemberDescriptors.Count > 1)
            {
                throw new NotSupportedException(string.Format("The entity type '{0}' can not define multiple auto increment members.", this.EntityType.FullName));
            }
            else if (autoIncrementMemberDescriptors.Count == 1)
            {
                MappingMemberDescriptor autoIncrementMemberDescriptor = autoIncrementMemberDescriptors[0];
                if (autoIncrementMemberDescriptor.IsDefined(typeof(NonAutoIncrementAttribute)))
                {
                    throw new ChloeException(string.Format("Can't define both 'AutoIncrementAttribute' and 'NotAutoIncrementAttribute' on the same mapping member '{0}'.", autoIncrementMemberDescriptor.MemberInfo.Name));
                }

                if (!IsAutoIncrementType(autoIncrementMemberDescriptor.MemberInfoType))
                {
                    throw new ChloeException("Auto increment member type must be Int16, Int32 or Int64.");
                }

                if (autoIncrementMemberDescriptor.IsPrimaryKey && primaryKeys.Count > 1)
                {
                    /* 自增列不能作为联合主键 */
                    throw new ChloeException("Auto increment member can not be union key.");
                }

                autoIncrementMemberDescriptor.IsAutoIncrement = true;
                this._autoIncrement = autoIncrementMemberDescriptor;
            }
            else if (primaryKeys.Count == 1)
            {
                /* 如果没有显示定义自增成员,并且主键只有 1 个,如果该主键满足一定条件,则默认其是自增列 */
                MappingMemberDescriptor primaryKeyDescriptor = primaryKeys[0];
                if (IsAutoIncrementType(primaryKeyDescriptor.MemberInfoType) && !primaryKeyDescriptor.IsDefined(typeof(NonAutoIncrementAttribute)))
                {
                    primaryKeyDescriptor.IsAutoIncrement = true;
                    this._autoIncrement = primaryKeyDescriptor;
                }
            }

            this._mappingMemberDescriptors = new Dictionary <MemberInfo, MappingMemberDescriptor>(mappingMemberDescriptors.Count);
            foreach (MappingMemberDescriptor mappingMemberDescriptor in mappingMemberDescriptors)
            {
                this._mappingMemberDescriptors.Add(mappingMemberDescriptor.MemberInfo, mappingMemberDescriptor);
            }
        }