상속: IEntityMapping
예제 #1
0
        internal MemberMapping(MemberInfo member, MemberAttribute attribute, EntityMapping entity)
        {
            this.member = member;
            this.entity = entity;

            memberType = member.GetMemberType();
            var isEnumerableType = memberType != Types.String
                && memberType != typeof(byte[])
                && Types.IEnumerable.IsAssignableFrom(memberType);

            if (attribute == null)
            {
                InitializeConversionMapping(isEnumerableType);
            }
            else
                InitializeAttributeMapping(attribute, isEnumerableType);

            getter = member.GetGetter();
            if (storageMember != null)
                setter = storageMember.GetSetter();
            else
                setter = member.GetSetter();
            if (columnName == null)
                columnName = member.Name;
        }
예제 #2
0
        /// <summary>
        /// 初始化约定映射
        /// </summary>
        /// <param name="isEnumerableType"></param>
        private void InitializeConversionMapping(bool isEnumerableType)
        {
            if (!isEnumerableType)
            {
                var underlyingType = memberType;
                if (underlyingType.IsNullable())
                    underlyingType = Nullable.GetUnderlyingType(underlyingType);
                if (underlyingType.IsEnum)
                    underlyingType = Enum.GetUnderlyingType(underlyingType);
                if (Converter.IsPrimitiveType(underlyingType)
                    || memberType == typeof(byte[]))
                {
                    isColumn = true;
                    columnName = MappingConversion.Current.ColumnName(member.Name);
                    bool required = false;
                    int length = 0;
                    sqlType = SqlType.Get(underlyingType);
                    isUpdatable = true;

                    if (EFDataAnnotiationAdapter.Instance != null)
                        PopulateEFDataAnnotitions();
                    if (DataAnnotationMappingAdapter.Instance != null)
                    {
                        object attr = null;
            #if !SDK35

                        attr = member.GetCustomAttributes(DataAnnotationMappingAdapter.KeyAttributeType, false).FirstOrDefault();
                        isPrimaryKey = attr != null;
            #endif
                        attr = member.GetCustomAttributes(DataAnnotationMappingAdapter.RequiredAttributeType, false).FirstOrDefault();
                        required = attr != null;

                        attr = member.GetCustomAttributes(DataAnnotationMappingAdapter.StringLengthAttributeType, false).FirstOrDefault();
                        if (attr != null)
                            length =(int) DataAnnotationMappingAdapter.Instance.StringLength.Length(attr);
                    }

                    sqlType = SqlType.Get(underlyingType, new ColumnAttribute { IsNullable = !required, Length = length });

                }
                else//manyToOne
                {
                    isRelationship = true;
                    relatedEntityType = underlyingType;
                    isManyToOne = isRelationship && !isEnumerableType;

                    if (relatedEntityType == entity.entityType)
                        relatedEntity = entity;

                    if (DataAnnotationMappingAdapter.Instance != null)
                    {
            #if !SDK35
                        var assAtt = member.GetCustomAttributes(DataAnnotationMappingAdapter.AssociationAttributeType, false).FirstOrDefault();
                        if (assAtt != null)
                        {
                            thisKey = DataAnnotationMappingAdapter.Instance.Association.ThisKey(assAtt) as string;
                            otherKey = DataAnnotationMappingAdapter.Instance.Association.OtherKey(assAtt) as string;
                        }
            #endif
                    }
                }

            }
            else
            {
                isRelationship = true;
                relatedEntityType = ReflectionHelper.GetElementType(memberType);
                isManyToOne = isRelationship && !isEnumerableType;

                if (relatedEntityType == entity.entityType)
                    relatedEntity = entity;
                if (DataAnnotationMappingAdapter.Instance != null)
                {
            #if !SDK35
                    var assAtt = member.GetCustomAttributes(DataAnnotationMappingAdapter.AssociationAttributeType, false).FirstOrDefault();
                    if (assAtt != null)
                    {
                        thisKey = DataAnnotationMappingAdapter.Instance.Association.ThisKey(assAtt) as string;
                        otherKey = DataAnnotationMappingAdapter.Instance.Association.OtherKey(assAtt) as string;
                    }
            #endif
                }
            }
        }
예제 #3
0
 private void InitializeAssociationAttribute(bool isEnumerableType, AbstractAssociationAttribute association)
 {
     isManyToOne = association.isForeignKey;
     if (isEnumerableType)
     {
         if (isManyToOne)
             throw new MappingException("Foreign Key member type should be entity class not collection type.");
         relatedEntityType = ReflectionHelper.GetElementType(memberType);
     }
     else
     {
         relatedEntityType = memberType;
     }
     thisKey = association.ThisKey;
     otherKey = association.OtherKey;
     if (relatedEntityType == entity.entityType)
         relatedEntity = entity;
 }
예제 #4
0
 private static IMemberMapping[] GetReferencedMembers(string names, string sourceName, EntityMapping entity)
 {
     return names.Split(separators).Select(n => GetReferencedMember(n, sourceName, entity)).ToArray();
 }
예제 #5
0
 private static IMemberMapping GetReferencedMember(string name, string sourceName, EntityMapping entity)
 {
     var mm = entity.GetMappingMember(name);
     if (mm == null)
         throw new InvalidOperationException(string.Format(Res.AttrbuteMappingError, entity.entityType.Name, name, sourceName, entity.entityType.Name));
     return mm;
 }
예제 #6
0
        internal void OnNotify(EntityMapping entityModel)
        {
            if (isRelationship)
            {
                if (entityModel == entity)
                {
                    thisKeyMembers = GetReferencedMembers(thisKey, "Association.ThisKey", entity);
                }
                else if (relatedEntity == null)
                {
                    if (relatedEntityType == entityModel.entityType)
                        relatedEntity = entityModel;
                    else if (relatedEntityType.FullName.Contains(DLinq.StrEntityRefType)
                        && relatedEntityType.GetGenericArguments()[0] == entityModel.entityType)
                        relatedEntity = entityModel;
                }

                if (relatedEntity != null)
                {
                    otherKeyMembers = GetReferencedMembers(otherKey, "Association.OtherKey", relatedEntity);
                }
            }
        }
예제 #7
0
        private static IMemberMapping GetReferencedMember(string name, string sourceName, EntityMapping entity)
        {
            var mm = entity.GetMappingMember(name);

            if (mm == null)
            {
                throw new InvalidOperationException(string.Format(Res.AttrbuteMappingError, entity.entityType.Name, name, sourceName, entity.entityType.Name));
            }
            return(mm);
        }
예제 #8
0
 private static IMemberMapping[] GetReferencedMembers(string names, string sourceName, EntityMapping entity)
 {
     return(names.Split(separators).Select(n => GetReferencedMember(n, sourceName, entity)).ToArray());
 }
예제 #9
0
        /// <summary>
        /// 初始化约定映射
        /// </summary>
        /// <param name="isEnumerableType"></param>
        private void InitializeConversionMapping(bool isEnumerableType)
        {
            if (!isEnumerableType)
            {
                var underlyingType = memberType;
                if (underlyingType.IsNullable())
                {
                    underlyingType = Nullable.GetUnderlyingType(underlyingType);
                }
                if (underlyingType.IsEnum)
                {
                    underlyingType = Enum.GetUnderlyingType(underlyingType);
                }
                if (Converter.IsPrimitiveType(underlyingType) ||
                    memberType == typeof(byte[]))
                {
                    isColumn   = true;
                    columnName = MappingConversion.Current.ColumnName(member.Name);
                    bool required = false;
                    int  length   = 0;
                    sqlType     = SqlType.Get(underlyingType);
                    isUpdatable = true;

                    if (EFDataAnnotiationAdapter.Instance != null)
                    {
                        PopulateEFDataAnnotitions();
                    }
                    if (DataAnnotationMappingAdapter.Instance != null)
                    {
                        object attr = null;
#if !SDK35
                        attr         = member.GetCustomAttributes(DataAnnotationMappingAdapter.KeyAttributeType, false).FirstOrDefault();
                        isPrimaryKey = attr != null;
#endif
                        attr     = member.GetCustomAttributes(DataAnnotationMappingAdapter.RequiredAttributeType, false).FirstOrDefault();
                        required = attr != null;

                        attr = member.GetCustomAttributes(DataAnnotationMappingAdapter.StringLengthAttributeType, false).FirstOrDefault();
                        if (attr != null)
                        {
                            length = (int)DataAnnotationMappingAdapter.Instance.StringLength.Length(attr);
                        }
                    }

                    sqlType = SqlType.Get(underlyingType, new ColumnAttribute {
                        IsNullable = !required, Length = length
                    });
                }
                else//manyToOne
                {
                    isRelationship    = true;
                    relatedEntityType = underlyingType;
                    isManyToOne       = isRelationship && !isEnumerableType;

                    if (relatedEntityType == entity.entityType)
                    {
                        relatedEntity = entity;
                    }

                    if (DataAnnotationMappingAdapter.Instance != null)
                    {
#if !SDK35
                        var assAtt = member.GetCustomAttributes(DataAnnotationMappingAdapter.AssociationAttributeType, false).FirstOrDefault();
                        if (assAtt != null)
                        {
                            thisKey  = DataAnnotationMappingAdapter.Instance.Association.ThisKey(assAtt) as string;
                            otherKey = DataAnnotationMappingAdapter.Instance.Association.OtherKey(assAtt) as string;
                        }
#endif
                    }
                }
            }
            else
            {
                isRelationship    = true;
                relatedEntityType = ReflectionHelper.GetElementType(memberType);
                isManyToOne       = isRelationship && !isEnumerableType;

                if (relatedEntityType == entity.entityType)
                {
                    relatedEntity = entity;
                }
                if (DataAnnotationMappingAdapter.Instance != null)
                {
#if !SDK35
                    var assAtt = member.GetCustomAttributes(DataAnnotationMappingAdapter.AssociationAttributeType, false).FirstOrDefault();
                    if (assAtt != null)
                    {
                        thisKey  = DataAnnotationMappingAdapter.Instance.Association.ThisKey(assAtt) as string;
                        otherKey = DataAnnotationMappingAdapter.Instance.Association.OtherKey(assAtt) as string;
                    }
#endif
                }
            }
        }