Exemplo n.º 1
0
        private bool TryFindAndCreateEnumProperties(
            Type type, StructuralType cspaceType, StructuralType ospaceType, IEnumerable <PropertyInfo> clrProperties,
            List <Action> referenceResolutionListForCurrentType)
        {
            var typeClosureToTrack = new List <KeyValuePair <EdmProperty, PropertyInfo> >();

            foreach (
                var cspaceProperty in cspaceType.GetDeclaredOnlyMembers <EdmProperty>().Where(p => Helper.IsEnumType(p.TypeUsage.EdmType)))
            {
                var clrProperty = clrProperties.FirstOrDefault(p => MemberMatchesByConvention(p, cspaceProperty));
                if (clrProperty != null)
                {
                    typeClosureToTrack.Add(new KeyValuePair <EdmProperty, PropertyInfo>(cspaceProperty, clrProperty));
                }
                else
                {
                    var message = Strings.Validator_OSpace_Convention_MissingRequiredProperty(cspaceProperty.Name, type.FullName);
                    LogLoadMessage(message, cspaceType);
                    return(false);
                }
            }

            foreach (var typeToTrack in typeClosureToTrack)
            {
                TrackClosure(typeToTrack.Value.PropertyType);
                // prevent the lifting of these closure variables
                var ot   = ospaceType;
                var cp   = typeToTrack.Key;
                var clrp = typeToTrack.Value;
                referenceResolutionListForCurrentType.Add(() => CreateAndAddEnumProperty(type, ot, cp, clrp));
            }

            return(true);
        }
Exemplo n.º 2
0
        // <summary>
        // Creates an Enum property based on <paramref name="clrProperty" /> and adds it to the parent structural type.
        // </summary>
        // <param name="type">
        // CLR type owning <paramref name="clrProperty" /> .
        // </param>
        // <param name="ospaceType"> OSpace type the created property will be added to. </param>
        // <param name="cspaceProperty"> Corresponding property from CSpace. </param>
        // <param name="clrProperty"> CLR property used to build an Enum property. </param>
        private void CreateAndAddEnumProperty(Type type, StructuralType ospaceType, EdmProperty cspaceProperty, PropertyInfo clrProperty)
        {
            EdmType propertyType;

            if (CspaceToOspace.TryGetValue(cspaceProperty.TypeUsage.EdmType, out propertyType))
            {
                if (clrProperty.CanRead &&
                    clrProperty.CanWriteExtended())
                {
                    AddScalarMember(type, clrProperty, ospaceType, cspaceProperty, propertyType);
                }
                else
                {
                    LogError(
                        Strings.Validator_OSpace_Convention_ScalarPropertyMissginGetterOrSetter(
                            clrProperty.Name, type.FullName, type.Assembly().FullName),
                        cspaceProperty.TypeUsage.EdmType);
                }
            }
            else
            {
                LogError(
                    Strings.Validator_OSpace_Convention_MissingOSpaceType(cspaceProperty.TypeUsage.EdmType.FullName),
                    cspaceProperty.TypeUsage.EdmType);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 ///     Given the type in the target space and the member name in the source space,
 ///     get the corresponding member in the target space
 ///     For e.g.  consider a Conceptual Type 'Foo' with a member 'Bar' and a CLR type
 ///     'XFoo' with a member 'YBar'. If one has a reference to Foo one can
 ///     invoke GetMember(Foo,"YBar") to retrieve the member metadata for bar
 /// </summary>
 /// <param name="type"> The type in the target perspective </param>
 /// <param name="memberName"> the name of the member in the source perspective </param>
 /// <param name="ignoreCase"> Whether to do case-sensitive member look up or not </param>
 /// <param name="outMember"> returns the member in target space, if a match is found </param>
 internal virtual bool TryGetMember(StructuralType type, String memberName, bool ignoreCase, out EdmMember outMember)
 {
     DebugCheck.NotNull(type);
     Check.NotEmpty(memberName, "memberName");
     outMember = null;
     return(type.Members.TryGetValue(memberName, ignoreCase, out outMember));
 }
Exemplo n.º 4
0
        private bool TryCreateMembers(
            Type type, StructuralType cspaceType, StructuralType ospaceType, List <Action> referenceResolutionListForCurrentType)
        {
            var clrProperties = (cspaceType.BaseType == null
                                     ? type.GetRuntimeProperties()
                                     : type.GetDeclaredProperties()).Where(p => !p.IsStatic());

            // required properties scalar properties first
            if (!TryFindAndCreatePrimitiveProperties(type, cspaceType, ospaceType, clrProperties))
            {
                return(false);
            }

            if (!TryFindAndCreateEnumProperties(type, cspaceType, ospaceType, clrProperties, referenceResolutionListForCurrentType))
            {
                return(false);
            }

            if (!TryFindComplexProperties(type, cspaceType, ospaceType, clrProperties, referenceResolutionListForCurrentType))
            {
                return(false);
            }

            if (!TryFindNavigationProperties(type, cspaceType, ospaceType, clrProperties, referenceResolutionListForCurrentType))
            {
                return(false);
            }

            return(true);
        }
        private bool TryCreateStructuralType(
            Type type,
            StructuralType cspaceType,
            out EdmType newOSpaceType)
        {
            List <Action> referenceResolutionListForCurrentType = new List <Action>();

            newOSpaceType = (EdmType)null;
            StructuralType ospaceType = !Helper.IsEntityType((EdmType)cspaceType) ? (StructuralType) new ClrComplexType(type, cspaceType.NamespaceName, cspaceType.Name) : (StructuralType) new ClrEntityType(type, cspaceType.NamespaceName, cspaceType.Name);

            if (cspaceType.BaseType != null)
            {
                if (OSpaceTypeFactory.TypesMatchByConvention(type.BaseType(), cspaceType.BaseType))
                {
                    this.TrackClosure(type.BaseType());
                    referenceResolutionListForCurrentType.Add((Action)(() => ospaceType.BaseType = this.ResolveBaseType((StructuralType)cspaceType.BaseType, type)));
                }
                else
                {
                    this.LogLoadMessage(Strings.Validator_OSpace_Convention_BaseTypeIncompatible((object)type.BaseType().FullName, (object)type.FullName, (object)cspaceType.BaseType.FullName), (EdmType)cspaceType);
                    return(false);
                }
            }
            if (!this.TryCreateMembers(type, cspaceType, ospaceType, referenceResolutionListForCurrentType))
            {
                return(false);
            }
            this.LoadedTypes.Add(type.FullName, (EdmType)ospaceType);
            foreach (Action action in referenceResolutionListForCurrentType)
            {
                this.ReferenceResolutions.Add(action);
            }
            newOSpaceType = (EdmType)ospaceType;
            return(true);
        }
        private bool TryFindComplexProperties(
            Type type,
            StructuralType cspaceType,
            StructuralType ospaceType,
            IEnumerable <PropertyInfo> clrProperties,
            List <Action> referenceResolutionListForCurrentType)
        {
            List <KeyValuePair <EdmProperty, PropertyInfo> > keyValuePairList = new List <KeyValuePair <EdmProperty, PropertyInfo> >();

            foreach (EdmProperty edmProperty in cspaceType.GetDeclaredOnlyMembers <EdmProperty>().Where <EdmProperty>((Func <EdmProperty, bool>)(m => Helper.IsComplexType(m.TypeUsage.EdmType))))
            {
                EdmProperty  cspaceProperty = edmProperty;
                PropertyInfo propertyInfo   = clrProperties.FirstOrDefault <PropertyInfo>((Func <PropertyInfo, bool>)(p => OSpaceTypeFactory.MemberMatchesByConvention(p, (EdmMember)cspaceProperty)));
                if (propertyInfo != (PropertyInfo)null)
                {
                    keyValuePairList.Add(new KeyValuePair <EdmProperty, PropertyInfo>(cspaceProperty, propertyInfo));
                }
                else
                {
                    this.LogLoadMessage(Strings.Validator_OSpace_Convention_MissingRequiredProperty((object)cspaceProperty.Name, (object)type.FullName), (EdmType)cspaceType);
                    return(false);
                }
            }
            foreach (KeyValuePair <EdmProperty, PropertyInfo> keyValuePair in keyValuePairList)
            {
                this.TrackClosure(keyValuePair.Value.PropertyType);
                StructuralType ot   = ospaceType;
                EdmProperty    cp   = keyValuePair.Key;
                PropertyInfo   clrp = keyValuePair.Value;
                referenceResolutionListForCurrentType.Add((Action)(() => this.CreateAndAddComplexType(type, ot, cp, clrp)));
            }
            return(true);
        }
        private void ResolveComplexTypeProperty(StructuralType type, PropertyInfo clrProperty)
        {
            // Load the property type and create a new property object
            EdmType propertyType;

            // If the type could not be loaded it's definitely not a complex type, so that's an error
            // If it could be loaded but is not a complex type that's an error as well
            if (!TryGetLoadedType(clrProperty.PropertyType, out propertyType) ||
                propertyType.BuiltInTypeKind != BuiltInTypeKind.ComplexType)
            {
                // This property does not need to be validated further, just add to the errors collection and continue with the next property
                // This failure will cause an exception to be thrown later during validation of all of the types
                SessionData.EdmItemErrors.Add(
                    new EdmItemError(
                        Strings.Validator_OSpace_ComplexPropertyNotComplex(
                            clrProperty.Name, clrProperty.DeclaringType.FullName, clrProperty.PropertyType.FullName)));
            }
            else
            {
                var newProperty = new EdmProperty(
                    clrProperty.Name,
                    TypeUsage.Create(
                        propertyType, new FacetValues
                {
                    Nullable = false
                }),
                    clrProperty, type.ClrType);

                type.AddMember(newProperty);
            }
        }
        /// <summary>
        ///     Creates a structural OSpace type based on CLR type and CSpace type.
        /// </summary>
        /// <param name="type"> CLR type. </param>
        /// <param name="cspaceType"> CSpace Type </param>
        /// <param name="newOSpaceType">
        ///     OSpace type created based on CLR <paramref name="type" /> and <paramref name="cspaceType" />
        /// </param>
        /// <returns>
        ///     <c>true</c> if the type was created successfully. Otherwise <c>false</c> .
        /// </returns>
        private bool TryCreateStructuralType(Type type, StructuralType cspaceType, out EdmType newOSpaceType)
        {
            DebugCheck.NotNull(type);
            DebugCheck.NotNull(cspaceType);

            var referenceResolutionListForCurrentType = new List <Action>();

            newOSpaceType = null;
            Debug.Assert(TypesMatchByConvention(type, cspaceType), "The types passed as parameters don't match by convention.");

            StructuralType ospaceType;

            if (Helper.IsEntityType(cspaceType))
            {
                ospaceType = new ClrEntityType(type, cspaceType.NamespaceName, cspaceType.Name);
            }
            else
            {
                Debug.Assert(Helper.IsComplexType(cspaceType), "Invalid type attribute encountered");
                ospaceType = new ClrComplexType(type, cspaceType.NamespaceName, cspaceType.Name);
            }

            if (cspaceType.BaseType != null)
            {
                if (TypesMatchByConvention(type.BaseType, cspaceType.BaseType))
                {
                    TrackClosure(type.BaseType);
                    referenceResolutionListForCurrentType.Add(
                        () => ospaceType.BaseType = ResolveBaseType((StructuralType)cspaceType.BaseType, type));
                }
                else
                {
                    var message = Strings.Validator_OSpace_Convention_BaseTypeIncompatible(
                        type.BaseType.FullName, type.FullName, cspaceType.BaseType.FullName);
                    SessionData.LoadMessageLogger.LogLoadMessage(message, cspaceType);
                    return(false);
                }
            }

            // Load the properties for this type
            if (!TryCreateMembers(type, cspaceType, ospaceType, referenceResolutionListForCurrentType))
            {
                return(false);
            }

            // Add this to the known type map so we won't try to load it again
            SessionData.TypesInLoading.Add(type.FullName, ospaceType);

            // we only add the referenceResolution to the list unless we structrually matched this type
            foreach (var referenceResolution in referenceResolutionListForCurrentType)
            {
                _referenceResolutions.Add(referenceResolution);
            }

            newOSpaceType = ospaceType;
            return(true);
        }
        private EdmType ResolveBaseType(StructuralType baseCSpaceType, Type type)
        {
            EdmType edmType;

            if (!this.CspaceToOspace.TryGetValue((EdmType)baseCSpaceType, out edmType))
            {
                this.LogError(Strings.Validator_OSpace_Convention_BaseTypeNotLoaded((object)type, (object)baseCSpaceType), (EdmType)baseCSpaceType);
            }
            return(edmType);
        }
Exemplo n.º 10
0
 internal virtual bool TryGetMember(
     StructuralType type,
     string memberName,
     bool ignoreCase,
     out EdmMember outMember)
 {
     Check.NotEmpty(memberName, nameof(memberName));
     outMember = (EdmMember)null;
     return(type.Members.TryGetValue(memberName, ignoreCase, out outMember));
 }
        private int GetBaseTypeMemberCount()
        {
            StructuralType baseType = this._declaringType.BaseType as StructuralType;

            if (baseType != null)
            {
                return(baseType.Members.Count);
            }
            return(0);
        }
        private bool TryCreateMembers(
            Type type,
            StructuralType cspaceType,
            StructuralType ospaceType,
            List <Action> referenceResolutionListForCurrentType)
        {
            IEnumerable <PropertyInfo> clrProperties = (cspaceType.BaseType == null ? type.GetRuntimeProperties() : type.GetDeclaredProperties()).Where <PropertyInfo>((Func <PropertyInfo, bool>)(p => !p.IsStatic()));

            return(this.TryFindAndCreatePrimitiveProperties(type, cspaceType, ospaceType, clrProperties) && this.TryFindAndCreateEnumProperties(type, cspaceType, ospaceType, clrProperties, referenceResolutionListForCurrentType) && (this.TryFindComplexProperties(type, cspaceType, ospaceType, clrProperties, referenceResolutionListForCurrentType) && this.TryFindNavigationProperties(type, cspaceType, ospaceType, clrProperties, referenceResolutionListForCurrentType)));
        }
Exemplo n.º 13
0
        internal void ResolveNavigationProperty(StructuralType declaringType, PropertyInfo propertyInfo)
        {
            IEnumerable <EdmRelationshipNavigationPropertyAttribute> customAttributes = propertyInfo.GetCustomAttributes <EdmRelationshipNavigationPropertyAttribute>(false);
            EdmType edmType;

            if (!this.TryGetLoadedType(propertyInfo.PropertyType, out edmType) || edmType.BuiltInTypeKind != BuiltInTypeKind.EntityType && edmType.BuiltInTypeKind != BuiltInTypeKind.CollectionType)
            {
                this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_OSpace_InvalidNavPropReturnType((object)propertyInfo.Name, (object)propertyInfo.DeclaringType.FullName, (object)propertyInfo.PropertyType.FullName)));
            }
            else
            {
                EdmRelationshipNavigationPropertyAttribute propertyAttribute = customAttributes.First <EdmRelationshipNavigationPropertyAttribute>();
                EdmMember member = (EdmMember)null;
                EdmType   type;
                if (this.SessionData.TypesInLoading.TryGetValue(propertyAttribute.RelationshipNamespaceName + "." + propertyAttribute.RelationshipName, out type) && Helper.IsAssociationType(type))
                {
                    AssociationType associationType = (AssociationType)type;
                    if (associationType != null)
                    {
                        NavigationProperty navigationProperty = new NavigationProperty(propertyInfo.Name, TypeUsage.Create(edmType));
                        navigationProperty.RelationshipType = (RelationshipType)associationType;
                        member = (EdmMember)navigationProperty;
                        if (associationType.Members[0].Name == propertyAttribute.TargetRoleName)
                        {
                            navigationProperty.ToEndMember   = (RelationshipEndMember)associationType.Members[0];
                            navigationProperty.FromEndMember = (RelationshipEndMember)associationType.Members[1];
                        }
                        else if (associationType.Members[1].Name == propertyAttribute.TargetRoleName)
                        {
                            navigationProperty.ToEndMember   = (RelationshipEndMember)associationType.Members[1];
                            navigationProperty.FromEndMember = (RelationshipEndMember)associationType.Members[0];
                        }
                        else
                        {
                            this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.TargetRoleNameInNavigationPropertyNotValid((object)propertyInfo.Name, (object)propertyInfo.DeclaringType.FullName, (object)propertyAttribute.TargetRoleName, (object)propertyAttribute.RelationshipName)));
                            member = (EdmMember)null;
                        }
                        if (member != null && ((RefType)navigationProperty.FromEndMember.TypeUsage.EdmType).ElementType.ClrType != declaringType.ClrType)
                        {
                            this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.NavigationPropertyRelationshipEndTypeMismatch((object)declaringType.FullName, (object)navigationProperty.Name, (object)associationType.FullName, (object)navigationProperty.FromEndMember.Name, (object)((RefType)navigationProperty.FromEndMember.TypeUsage.EdmType).ElementType.ClrType)));
                            member = (EdmMember)null;
                        }
                    }
                }
                else
                {
                    this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.RelationshipNameInNavigationPropertyNotValid((object)propertyInfo.Name, (object)propertyInfo.DeclaringType.FullName, (object)propertyAttribute.RelationshipName)));
                }
                if (member == null)
                {
                    return;
                }
                declaringType.AddMember(member);
            }
        }
Exemplo n.º 14
0
        private EdmType ResolveBaseType(StructuralType baseCSpaceType, Type type)
        {
            EdmType ospaceType;
            var     foundValue = CspaceToOspace.TryGetValue(baseCSpaceType, out ospaceType);

            if (!foundValue)
            {
                LogError(Strings.Validator_OSpace_Convention_BaseTypeNotLoaded(type, baseCSpaceType), baseCSpaceType);
            }

            Debug.Assert(!foundValue || ospaceType is StructuralType, "Structural type expected (if found).");

            return(ospaceType);
        }
        public override int IndexOf(EdmMember item)
        {
            int num = base.IndexOf(item);

            if (num != -1)
            {
                return(num + this.GetBaseTypeMemberCount());
            }
            StructuralType baseType = this._declaringType.BaseType as StructuralType;

            if (baseType != null)
            {
                return(baseType.Members.IndexOf(item));
            }
            return(-1);
        }
Exemplo n.º 16
0
        private void ResolveComplexTypeProperty(StructuralType type, PropertyInfo clrProperty)
        {
            EdmType edmType;

            if (!this.TryGetLoadedType(clrProperty.PropertyType, out edmType) || edmType.BuiltInTypeKind != BuiltInTypeKind.ComplexType)
            {
                this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_OSpace_ComplexPropertyNotComplex((object)clrProperty.Name, (object)clrProperty.DeclaringType.FullName, (object)clrProperty.PropertyType.FullName)));
            }
            else
            {
                EdmProperty edmProperty = new EdmProperty(clrProperty.Name, TypeUsage.Create(edmType, new FacetValues()
                {
                    Nullable = (FacetValueContainer <bool?>) new bool?(false)
                }), clrProperty, type.ClrType);
                type.AddMember((EdmMember)edmProperty);
            }
        }
Exemplo n.º 17
0
 private void LoadPropertiesFromType(StructuralType structuralType)
 {
     foreach (PropertyInfo propertyInfo in structuralType.ClrType.GetDeclaredProperties().Where <PropertyInfo>((Func <PropertyInfo, bool>)(p => !p.IsStatic())))
     {
         EdmMember member = (EdmMember)null;
         bool      isEntityKeyProperty = false;
         if (propertyInfo.GetCustomAttributes <EdmRelationshipNavigationPropertyAttribute>(false).Any <EdmRelationshipNavigationPropertyAttribute>())
         {
             PropertyInfo pi = propertyInfo;
             this._unresolvedNavigationProperties.Add((Action)(() => this.ResolveNavigationProperty(structuralType, pi)));
         }
         else if (propertyInfo.GetCustomAttributes <EdmScalarPropertyAttribute>(false).Any <EdmScalarPropertyAttribute>())
         {
             Type type = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
             if ((object)type == null)
             {
                 type = propertyInfo.PropertyType;
             }
             if (type.IsEnum())
             {
                 this.TrackClosure(propertyInfo.PropertyType);
                 PropertyInfo local = propertyInfo;
                 this.AddTypeResolver((Action)(() => this.ResolveEnumTypeProperty(structuralType, local)));
             }
             else
             {
                 member = this.LoadScalarProperty(structuralType.ClrType, propertyInfo, out isEntityKeyProperty);
             }
         }
         else if (propertyInfo.GetCustomAttributes <EdmComplexPropertyAttribute>(false).Any <EdmComplexPropertyAttribute>())
         {
             this.TrackClosure(propertyInfo.PropertyType);
             PropertyInfo local = propertyInfo;
             this.AddTypeResolver((Action)(() => this.ResolveComplexTypeProperty(structuralType, local)));
         }
         if (member != null)
         {
             structuralType.AddMember(member);
             if (Helper.IsEntityType((EdmType)structuralType) && isEntityKeyProperty)
             {
                 ((EntityTypeBase)structuralType).AddKeyMember(member);
             }
         }
     }
 }
        private EdmType ResolveBaseType(StructuralType baseCSpaceType, Type type)
        {
            EdmType ospaceType;
            var     foundValue = SessionData.CspaceToOspace.TryGetValue(baseCSpaceType, out ospaceType);

            if (!foundValue)
            {
                var message =
                    SessionData.LoadMessageLogger.CreateErrorMessageWithTypeSpecificLoadLogs(
                        Strings.Validator_OSpace_Convention_BaseTypeNotLoaded(type, baseCSpaceType),
                        baseCSpaceType);
                SessionData.EdmItemErrors.Add(new EdmItemError(message));
            }

            Debug.Assert(!foundValue || ospaceType is StructuralType, "Structural type expected (if found).");

            return(ospaceType);
        }
Exemplo n.º 19
0
        private bool TryFindNavigationProperties(
            Type type, StructuralType cspaceType, StructuralType ospaceType, PropertyInfo[] clrProperties,
            List <Action> referenceResolutionListForCurrentType)
        {
            var typeClosureToTrack =
                new List <KeyValuePair <NavigationProperty, PropertyInfo> >();

            foreach (var cspaceProperty in cspaceType.GetDeclaredOnlyMembers <NavigationProperty>())
            {
                var clrProperty = clrProperties.FirstOrDefault(p => NonPrimitiveMemberMatchesByConvention(p, cspaceProperty));
                if (clrProperty != null)
                {
                    var needsSetter = cspaceProperty.ToEndMember.RelationshipMultiplicity != RelationshipMultiplicity.Many;
                    if (clrProperty.CanRead &&
                        (!needsSetter || clrProperty.CanWriteExtended()))
                    {
                        typeClosureToTrack.Add(
                            new KeyValuePair <NavigationProperty, PropertyInfo>(
                                cspaceProperty, clrProperty));
                    }
                }
                else
                {
                    var message = Strings.Validator_OSpace_Convention_MissingRequiredProperty(
                        cspaceProperty.Name, type.FullName);
                    LogLoadMessage(message, cspaceType);
                    return(false);
                }
            }

            foreach (var typeToTrack in typeClosureToTrack)
            {
                TrackClosure(typeToTrack.Value.PropertyType);

                // keep from lifting these closure variables
                var ct = cspaceType;
                var ot = ospaceType;
                var cp = typeToTrack.Key;

                referenceResolutionListForCurrentType.Add(() => CreateAndAddNavigationProperty(ct, ot, cp));
            }

            return(true);
        }
        /// <summary>
        ///     Resolves enum type property.
        /// </summary>
        /// <param name="declaringType"> The type to add the declared property to. </param>
        /// <param name="clrProperty"> Property to resolve. </param>
        private void ResolveEnumTypeProperty(StructuralType declaringType, PropertyInfo clrProperty)
        {
            DebugCheck.NotNull(declaringType);
            DebugCheck.NotNull(clrProperty);
            Debug.Assert(
                (Nullable.GetUnderlyingType(clrProperty.PropertyType) ?? clrProperty.PropertyType).IsEnum,
                "This method should be called for enums only");

            EdmType propertyType;

            if (!TryGetLoadedType(clrProperty.PropertyType, out propertyType) ||
                !Helper.IsEnumType(propertyType))
            {
                SessionData.EdmItemErrors.Add(
                    new EdmItemError(
                        Strings.Validator_OSpace_ScalarPropertyNotPrimitive(
                            clrProperty.Name,
                            clrProperty.DeclaringType.FullName,
                            clrProperty.PropertyType.FullName)));
            }
            else
            {
                var edmScalarPropertyAttribute =
                    (EdmScalarPropertyAttribute)clrProperty.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false).Single();

                var enumProperty = new EdmProperty(
                    clrProperty.Name,
                    TypeUsage.Create(
                        propertyType, new FacetValues
                {
                    Nullable = edmScalarPropertyAttribute.IsNullable
                }),
                    clrProperty,
                    declaringType.ClrType);

                declaringType.AddMember(enumProperty);

                if (declaringType.BuiltInTypeKind == BuiltInTypeKind.EntityType &&
                    edmScalarPropertyAttribute.EntityKeyProperty)
                {
                    ((EntityType)declaringType).AddKeyMember(enumProperty);
                }
            }
        }
Exemplo n.º 21
0
 private bool TryFindAndCreatePrimitiveProperties(
     Type type, StructuralType cspaceType, StructuralType ospaceType, PropertyInfo[] clrProperties)
 {
     foreach (
         var cspaceProperty in
         cspaceType.GetDeclaredOnlyMembers <EdmProperty>().Where(p => Helper.IsPrimitiveType(p.TypeUsage.EdmType)))
     {
         var clrProperty = clrProperties.FirstOrDefault(p => MemberMatchesByConvention(p, cspaceProperty));
         if (clrProperty != null)
         {
             PrimitiveType propertyType;
             if (TryGetPrimitiveType(clrProperty.PropertyType, out propertyType))
             {
                 if (clrProperty.CanRead &&
                     clrProperty.CanWriteExtended())
                 {
                     AddScalarMember(type, clrProperty, ospaceType, cspaceProperty, propertyType);
                 }
                 else
                 {
                     var message = Strings.Validator_OSpace_Convention_ScalarPropertyMissginGetterOrSetter(
                         clrProperty.Name, type.FullName, type.Assembly.FullName);
                     LogLoadMessage(message, cspaceType);
                     return(false);
                 }
             }
             else
             {
                 var message = Strings.Validator_OSpace_Convention_NonPrimitiveTypeProperty(
                     clrProperty.Name, type.FullName, clrProperty.PropertyType.FullName);
                 LogLoadMessage(message, cspaceType);
                 return(false);
             }
         }
         else
         {
             var message = Strings.Validator_OSpace_Convention_MissingRequiredProperty(cspaceProperty.Name, type.FullName);
             LogLoadMessage(message, cspaceType);
             return(false);
         }
     }
     return(true);
 }
        // <summary>
        // Given the type in the target space and the member name in the source space,
        // get the corresponding member in the target space
        // For e.g.  consider a Conceptual Type Foo with a member bar and a CLR type
        // XFoo with a member YBar. If one has a reference to Foo one can
        // invoke GetMember(Foo,"YBar") to retrieve the member metadata for bar
        // </summary>
        // <param name="type"> The type in the target perspective </param>
        // <param name="memberName"> the name of the member in the source perspective </param>
        // <param name="ignoreCase"> true for case-insensitive lookup </param>
        // <param name="outMember"> returns the edmMember if a match is found </param>
        // <returns> true if a match is found, otherwise false </returns>
        internal override bool TryGetMember(StructuralType type, String memberName, bool ignoreCase, out EdmMember outMember)
        {
            outMember = null;
            Map map = null;

            if (MetadataWorkspace.TryGetMap(type, DataSpace.OCSpace, out map))
            {
                var objectTypeMap = map as ObjectTypeMapping;

                if (objectTypeMap != null)
                {
                    var objPropertyMapping = objectTypeMap.GetMemberMapForClrMember(memberName, ignoreCase);
                    if (null != objPropertyMapping)
                    {
                        outMember = objPropertyMapping.EdmMember;
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 23
0
        private static void AddScalarMember(
            Type type, PropertyInfo clrProperty, StructuralType ospaceType, EdmProperty cspaceProperty, EdmType propertyType)
        {
            DebugCheck.NotNull(type);
            DebugCheck.NotNull(clrProperty);
            Debug.Assert(clrProperty.CanRead && clrProperty.CanWriteExtended(), "The clr property has to have a setter and a getter.");
            DebugCheck.NotNull(ospaceType);
            DebugCheck.NotNull(cspaceProperty);
            DebugCheck.NotNull(propertyType);
            Debug.Assert(Helper.IsScalarType(propertyType), "Property has to be primitive or enum.");

            var cspaceType = cspaceProperty.DeclaringType;

            var isKeyMember = Helper.IsEntityType(cspaceType) && ((EntityType)cspaceType).KeyMemberNames.Contains(clrProperty.Name);

            // the property is nullable only if it is not a key and can actually be set to null (i.e. is not a value type or is a nullable value type)
            var nullableFacetValue = !isKeyMember
                                     &&
                                     (!clrProperty.PropertyType.IsValueType || Nullable.GetUnderlyingType(clrProperty.PropertyType) != null);

            var ospaceProperty =
                new EdmProperty(
                    cspaceProperty.Name,
                    TypeUsage.Create(
                        propertyType, new FacetValues
            {
                Nullable = nullableFacetValue
            }),
                    clrProperty,
                    type);

            if (isKeyMember)
            {
                ((EntityType)ospaceType).AddKeyMember(ospaceProperty);
            }
            else
            {
                ospaceType.AddMember(ospaceProperty);
            }
        }
Exemplo n.º 24
0
        private void ValidateStructuralType(
            StructuralType item,
            List <EdmItemError> errors,
            HashSet <MetadataItem> validatedItems)
        {
            this.ValidateEdmType((EdmType)item, errors, validatedItems);
            Dictionary <string, EdmMember> dictionary = new Dictionary <string, EdmMember>();

            foreach (EdmMember member in item.Members)
            {
                EdmMember edmMember = (EdmMember)null;
                if (dictionary.TryGetValue(member.Name, out edmMember))
                {
                    this.AddError(errors, new EdmItemError(Strings.Validator_BaseTypeHasMemberOfSameName));
                }
                else
                {
                    dictionary.Add(member.Name, member);
                }
                this.InternalValidate((MetadataItem)member, errors, validatedItems);
            }
        }
 private bool TryFindAndCreatePrimitiveProperties(
     Type type,
     StructuralType cspaceType,
     StructuralType ospaceType,
     IEnumerable <PropertyInfo> clrProperties)
 {
     foreach (EdmProperty edmProperty in cspaceType.GetDeclaredOnlyMembers <EdmProperty>().Where <EdmProperty>((Func <EdmProperty, bool>)(p => Helper.IsPrimitiveType(p.TypeUsage.EdmType))))
     {
         EdmProperty  cspaceProperty = edmProperty;
         PropertyInfo propertyInfo   = clrProperties.FirstOrDefault <PropertyInfo>((Func <PropertyInfo, bool>)(p => OSpaceTypeFactory.MemberMatchesByConvention(p, (EdmMember)cspaceProperty)));
         if (propertyInfo != (PropertyInfo)null)
         {
             PrimitiveType primitiveType;
             if (OSpaceTypeFactory.TryGetPrimitiveType(propertyInfo.PropertyType, out primitiveType))
             {
                 if (propertyInfo.CanRead && propertyInfo.CanWriteExtended())
                 {
                     OSpaceTypeFactory.AddScalarMember(type, propertyInfo, ospaceType, cspaceProperty, (EdmType)primitiveType);
                 }
                 else
                 {
                     this.LogLoadMessage(Strings.Validator_OSpace_Convention_ScalarPropertyMissginGetterOrSetter((object)propertyInfo.Name, (object)type.FullName, (object)type.Assembly().FullName), (EdmType)cspaceType);
                     return(false);
                 }
             }
             else
             {
                 this.LogLoadMessage(Strings.Validator_OSpace_Convention_NonPrimitiveTypeProperty((object)propertyInfo.Name, (object)type.FullName, (object)propertyInfo.PropertyType.FullName), (EdmType)cspaceType);
                 return(false);
             }
         }
         else
         {
             this.LogLoadMessage(Strings.Validator_OSpace_Convention_MissingRequiredProperty((object)cspaceProperty.Name, (object)type.FullName), (EdmType)cspaceType);
             return(false);
         }
     }
     return(true);
 }
Exemplo n.º 26
0
        private void CreateAndAddComplexType(Type type, StructuralType ospaceType, EdmProperty cspaceProperty, PropertyInfo clrProperty)
        {
            EdmType propertyType;

            if (CspaceToOspace.TryGetValue(cspaceProperty.TypeUsage.EdmType, out propertyType))
            {
                Debug.Assert(propertyType is StructuralType, "Structural type expected.");

                var property = new EdmProperty(
                    cspaceProperty.Name, TypeUsage.Create(
                        propertyType, new FacetValues
                {
                    Nullable = false
                }), clrProperty, type);
                ospaceType.AddMember(property);
            }
            else
            {
                LogError(
                    Strings.Validator_OSpace_Convention_MissingOSpaceType(cspaceProperty.TypeUsage.EdmType.FullName),
                    cspaceProperty.TypeUsage.EdmType);
            }
        }
Exemplo n.º 27
0
        private void ResolveEnumTypeProperty(StructuralType declaringType, PropertyInfo clrProperty)
        {
            EdmType edmType;

            if (!this.TryGetLoadedType(clrProperty.PropertyType, out edmType) || !Helper.IsEnumType(edmType))
            {
                this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_OSpace_ScalarPropertyNotPrimitive((object)clrProperty.Name, (object)clrProperty.DeclaringType.FullName, (object)clrProperty.PropertyType.FullName)));
            }
            else
            {
                EdmScalarPropertyAttribute propertyAttribute = clrProperty.GetCustomAttributes <EdmScalarPropertyAttribute>(false).Single <EdmScalarPropertyAttribute>();
                EdmProperty edmProperty = new EdmProperty(clrProperty.Name, TypeUsage.Create(edmType, new FacetValues()
                {
                    Nullable = (FacetValueContainer <bool?>) new bool?(propertyAttribute.IsNullable)
                }), clrProperty, declaringType.ClrType);
                declaringType.AddMember((EdmMember)edmProperty);
                if (declaringType.BuiltInTypeKind != BuiltInTypeKind.EntityType || !propertyAttribute.EntityKeyProperty)
                {
                    return;
                }
                ((EntityTypeBase)declaringType).AddKeyMember((EdmMember)edmProperty);
            }
        }
Exemplo n.º 28
0
        /// <summary>
        ///     Validate an StructuralType object
        /// </summary>
        /// <param name="item"> The StructuralType object to validate </param>
        /// <param name="errors"> An error collection for adding validation errors </param>
        /// <param name="validatedItems"> A dictionary keeping track of items that have been validated </param>
        private void ValidateStructuralType(StructuralType item, List <EdmItemError> errors, HashSet <MetadataItem> validatedItems)
        {
            ValidateEdmType(item, errors, validatedItems);

            // Just validate each member, the collection already guaranteed that there aren't any nulls in the collection
            var allMembers = new Dictionary <string, EdmMember>();

            foreach (var member in item.Members)
            {
                // Check if the base type already has a member of the same name
                EdmMember baseMember = null;
                if (allMembers.TryGetValue(member.Name, out baseMember))
                {
                    AddError(errors, new EdmItemError(Strings.Validator_BaseTypeHasMemberOfSameName));
                }
                else
                {
                    allMembers.Add(member.Name, member);
                }

                InternalValidate(member, errors, validatedItems);
            }
        }
        private bool TryFindNavigationProperties(
            Type type,
            StructuralType cspaceType,
            StructuralType ospaceType,
            IEnumerable <PropertyInfo> clrProperties,
            List <Action> referenceResolutionListForCurrentType)
        {
            List <KeyValuePair <NavigationProperty, PropertyInfo> > keyValuePairList = new List <KeyValuePair <NavigationProperty, PropertyInfo> >();

            foreach (NavigationProperty declaredOnlyMember in cspaceType.GetDeclaredOnlyMembers <NavigationProperty>())
            {
                NavigationProperty cspaceProperty = declaredOnlyMember;
                PropertyInfo       propertyInfo   = clrProperties.FirstOrDefault <PropertyInfo>((Func <PropertyInfo, bool>)(p => OSpaceTypeFactory.NonPrimitiveMemberMatchesByConvention(p, (EdmMember)cspaceProperty)));
                if (propertyInfo != (PropertyInfo)null)
                {
                    bool flag = cspaceProperty.ToEndMember.RelationshipMultiplicity != RelationshipMultiplicity.Many;
                    if (propertyInfo.CanRead && (!flag || propertyInfo.CanWriteExtended()))
                    {
                        keyValuePairList.Add(new KeyValuePair <NavigationProperty, PropertyInfo>(cspaceProperty, propertyInfo));
                    }
                }
                else
                {
                    this.LogLoadMessage(Strings.Validator_OSpace_Convention_MissingRequiredProperty((object)cspaceProperty.Name, (object)type.FullName), (EdmType)cspaceType);
                    return(false);
                }
            }
            foreach (KeyValuePair <NavigationProperty, PropertyInfo> keyValuePair in keyValuePairList)
            {
                this.TrackClosure(keyValuePair.Value.PropertyType);
                StructuralType     ct = cspaceType;
                StructuralType     ot = ospaceType;
                NavigationProperty cp = keyValuePair.Key;
                referenceResolutionListForCurrentType.Add((Action)(() => this.CreateAndAddNavigationProperty(ct, ot, cp)));
            }
            return(true);
        }
        internal override bool TryGetMember(
            StructuralType type,
            string memberName,
            bool ignoreCase,
            out EdmMember outMember)
        {
            outMember = (EdmMember)null;
            MappingBase map = (MappingBase)null;

            if (this.MetadataWorkspace.TryGetMap((GlobalItem)type, DataSpace.OCSpace, out map))
            {
                ObjectTypeMapping objectTypeMapping = map as ObjectTypeMapping;
                if (objectTypeMapping != null)
                {
                    ObjectMemberMapping memberMapForClrMember = objectTypeMapping.GetMemberMapForClrMember(memberName, ignoreCase);
                    if (memberMapForClrMember != null)
                    {
                        outMember = memberMapForClrMember.EdmMember;
                        return(true);
                    }
                }
            }
            return(false);
        }