示例#1
0
        /// <summary>
        /// Add the Edm. prefix to the primitive type if there isn't.
        /// </summary>
        /// <param name="typeName">The type name which may be not prefixed (Edm.).</param>
        /// <returns>The type name with Edm. prefix</returns>
        internal static string AddEdmPrefixOfTypeName(string typeName)
        {
            if (!string.IsNullOrEmpty(typeName))
            {
                string itemTypeName = EdmLibraryExtensions.GetCollectionItemTypeName(typeName);
                if (itemTypeName == null)
                {
                    // This is the primitive type
                    IEdmSchemaType primitiveType = EdmLibraryExtensions.ResolvePrimitiveTypeName(typeName);
                    if (primitiveType != null)
                    {
                        return(primitiveType.FullName());
                    }
                }
                else
                {
                    // This is the collection type
                    IEdmSchemaType primitiveType = EdmLibraryExtensions.ResolvePrimitiveTypeName(itemTypeName);
                    if (primitiveType != null)
                    {
                        return(EdmLibraryExtensions.GetCollectionTypeName(primitiveType.FullName()));
                    }
                }
            }

            // Return the origin type name
            return(typeName);
        }
示例#2
0
        public static Type GetClrType(IEdmType edmType, IEdmModel edmModel, IAssembliesResolver assembliesResolver)
        {
            IEdmSchemaType edmSchemaType = edmType as IEdmSchemaType;

            Contract.Assert(edmSchemaType != null);

            ClrTypeAnnotation annotation = edmModel.GetAnnotationValue <ClrTypeAnnotation>(edmSchemaType);

            if (annotation != null)
            {
                return(annotation.ClrType);
            }

            string             typeName      = edmSchemaType.FullName();
            IEnumerable <Type> matchingTypes = GetMatchingTypes(typeName, assembliesResolver);

            if (matchingTypes.Count() > 1)
            {
                throw Error.Argument("edmTypeReference", SRResources.MultipleMatchingClrTypesForEdmType,
                                     typeName, String.Join(",", matchingTypes.Select(type => type.AssemblyQualifiedName)));
            }

            edmModel.SetAnnotationValue <ClrTypeAnnotation>(edmSchemaType, new ClrTypeAnnotation(matchingTypes.SingleOrDefault()));

            return(matchingTypes.SingleOrDefault());
        }
示例#3
0
        private string GetEntitySetName(IEdmSchemaType ct)
        {
            if (ct.TypeKind != EdmTypeKind.Entity)
            {
                return(string.Empty);
            }
#if odataV3
            var entitySet = EntitySets
                            .Where(m => m != null && m.ElementType.FullName() == ct.FullName())
                            .DefaultIfEmpty().First();
#else
            var entitySet = EntitySets
                            .Where(m => m != null && m.EntityType().FullName() == ct.FullName())
                            .DefaultIfEmpty().First();
#endif
            return(entitySet != null ? entitySet.Name : string.Empty);
        }
示例#4
0
        public static Type GetClrType(IEdmType edmType, IEdmModel edmModel, IAssemblyProvider assemblyProvider)
        {
            IEdmSchemaType edmSchemaType = edmType as IEdmSchemaType;

            Contract.Assert(edmSchemaType != null);

            string             typeName      = edmSchemaType.FullName();
            IEnumerable <Type> matchingTypes = GetMatchingTypes(typeName, assemblyProvider);

            return(matchingTypes.FirstOrDefault());
        }
示例#5
0
        /// <summary>
        /// Get the item type full name if it's the primitive type
        /// </summary>
        /// <param name="typeName">the original type name</param>
        /// <returns>the item type full name if it's the primitive type</returns>
        private static string GetItemTypeFullName(string typeName)
        {
            IEdmSchemaType knownType = EdmCoreModel.Instance.FindDeclaredType(typeName);

            if (knownType != null)
            {
                return(knownType.FullName());
            }

            return(typeName);
        }
示例#6
0
        protected string GetCyclicBaseTypeName(string baseTypeName)
        {
            IEdmSchemaType edmSchemaType = this.context.FindType(baseTypeName);

            if (edmSchemaType != null)
            {
                return(edmSchemaType.FullName());
            }
            else
            {
                return(baseTypeName);
            }
        }
示例#7
0
        private void CacheSchemaType(IEdmSchemaType schemaType)
        {
            string key = schemaType.FullName();

            this.schemaTypeCache.Add(key, schemaType);
            IEdmStructuredType item = schemaType as IEdmStructuredType;

            if ((item != null) && (item.BaseType != null))
            {
                List <IEdmStructuredType> list;
                if (!this.derivedTypeMappings.TryGetValue(item.BaseType, out list))
                {
                    list = new List <IEdmStructuredType>();
                    this.derivedTypeMappings[item.BaseType] = list;
                }
                list.Add(item);
            }
        }
        /// <summary>
        /// Adds a schema type to the internal caches of the model.
        /// </summary>
        /// <param name="schemaType">The <see cref="IEdmSchemaType"/> to cache.</param>
        /// <remarks>
        /// Materialization state: none required. No change in materialization state.
        /// Cache state: none required. No change in cache state.
        /// </remarks>
        private void CacheSchemaType(IEdmSchemaType schemaType)
        {
            Debug.Assert(schemaType != null, "schemaType != null");

            // first add the schema element to the schema element cache
            string fullName = schemaType.FullName();
            Debug.Assert(!this.schemaTypeCache.ContainsKey(fullName), "Schema type cache already contains an element with name " + fullName + ".");
            this.schemaTypeCache.Add(fullName, schemaType);
            IEdmStructuredType structuredType = schemaType as IEdmStructuredType;
            if (structuredType != null && structuredType.BaseType != null)
            {
                List<IEdmStructuredType> derivedTypes;
                if (!this.derivedTypeMappings.TryGetValue(structuredType.BaseType, out derivedTypes))
                {
                    derivedTypes = new List<IEdmStructuredType>();
                    this.derivedTypeMappings[structuredType.BaseType] = derivedTypes;
                }

                derivedTypes.Add(structuredType);
            }
        }
示例#9
0
 private void CacheSchemaType(IEdmSchemaType schemaType)
 {
     string key = schemaType.FullName();
     this.schemaTypeCache.Add(key, schemaType);
     IEdmStructuredType item = schemaType as IEdmStructuredType;
     if ((item != null) && (item.BaseType != null))
     {
         List<IEdmStructuredType> list;
         if (!this.derivedTypeMappings.TryGetValue(item.BaseType, out list))
         {
             list = new List<IEdmStructuredType>();
             this.derivedTypeMappings[item.BaseType] = list;
         }
         list.Add(item);
     }
 }
示例#10
0
        private List <PropertyTemplate> GetClassProperties(IEdmSchemaType ent)
        {
            //stop here if enum
            if (ent is IEdmEnumType)
            {
                return(null);
            }
            var structuredType = ent as IEdmStructuredType;
            var properties     = structuredType.Properties();

            if (_setting.UseInheritance)
            {
#if odataV3
                properties = properties.Where(x => ((IEdmSchemaType)x.DeclaringType).FullName() == ent.FullName());
#else
                properties = properties.Where(x => x.DeclaringType.FullTypeName() == ent.FullTypeName());
#endif
            }

            //add serial for properties to support protbuf v3.0
            var serial = 1;
            var list   = properties.Select(property => new PropertyTemplate
            {
                IsNullable = property.Type.IsNullable,
                PropName   = property.Name,
                PropType   = GetClrTypeName(property.Type),
                Serial     = serial++
            }).ToList();

            return(list);
        }
        // Resolves the real name of the base type, in case it was using an alias before.
        protected string GetCyclicBaseTypeName(string baseTypeName)
        {
            IEdmSchemaType schemaBaseType = this.context.FindType(baseTypeName);

            return((schemaBaseType != null) ? schemaBaseType.FullName() : baseTypeName);
        }
示例#12
0
        private List <PropertyTemplate> GetClassProperties(IEdmSchemaType ent)
        {
            //stop here for enum
            var enumType = ent as IEdmEnumType;

            if (enumType != null)
            {
                return(null);
            }

            var structuredType = ent as IEdmStructuredType;
            var properties     = structuredType.Properties();

            if (_setting.UseInheritance)
            {
                properties = properties.Where(x => ((IEdmSchemaType)x.DeclaringType).FullName() == ent.FullName());
            }

            var list = properties.Select(property => new PropertyTemplate
            {
                //ToTrace = property.ToTraceString(),
                IsNullable = property.Type.IsNullable,
                PropName   = property.Name,
                PropType   = GetClrTypeName(property.Type),
                //ToDebugString = Helper.Dump(property)
            }).ToList();

            return(list);
        }
示例#13
0
        private List <PropertyTemplate> GetClassProperties(IEdmSchemaType ent)
        {
            //stop here if enum
            if (ent is IEdmEnumType)
            {
                return(Enumerable.Empty <PropertyTemplate>().ToList());                    //null;
            }
            var structuredType = ent as IEdmStructuredType;
            var properties     = structuredType.Properties();

            if (_setting != null && _setting.UseInheritance)
            {
#if odataV3
                properties = properties.Where(x => ((IEdmSchemaType)x.DeclaringType).FullName() == ent.FullName());
#else
                properties = properties.Where(x => x.DeclaringType.FullTypeName() == ent.FullTypeName());
#endif
            }

            //add serial for properties to support protbuf v3.0
            var serial = 1;
            var list   = properties.Select(property => new PropertyTemplate
            {
                IsNullable     = property.Type.IsNullable,
                PropName       = property.Name,
                PropType       = GetClrTypeName(property.Type),
                Serial         = serial++,
                ClassNameSpace = ent.Namespace,
                MaxLength      = GetMaxLength(property),
                IsReadOnly     = Model.IsReadOnly(property),
                //OriginalType = property.VocabularyAnnotations(Model),
            }).ToList();

            return(list);
        }
示例#14
0
 private static void CheckForUnreacheableTypeError(ValidationContext context, IEdmSchemaType type, EdmLocation location)
 {
     IEdmType foundType = context.Model.FindType(type.FullName());
     if (foundType is AmbiguousTypeBinding)
     {
         context.AddError(
             location,
             EdmErrorCode.BadAmbiguousElementBinding,
             Strings.EdmModel_Validator_Semantic_AmbiguousType(type.FullName()));
     }
     else if (!foundType.IsEquivalentTo(type))
     {
         context.AddError(
             location,
             EdmErrorCode.BadUnresolvedType,
             Strings.EdmModel_Validator_Semantic_InaccessibleType(type.FullName()));
     }
 }
 static SerializationValidator()
 {
     SerializationValidator.TypeReferenceTargetMustHaveValidName = new ValidationRule <IEdmTypeReference>((ValidationContext context, IEdmTypeReference typeReference) => {
         IEdmSchemaType definition = typeReference.Definition as IEdmSchemaType;
         if (definition != null && !EdmUtil.IsQualifiedName(definition.FullName()))
         {
             context.AddError(typeReference.Location(), EdmErrorCode.ReferencedTypeMustHaveValidName, Strings.Serializer_ReferencedTypeMustHaveValidName(definition.FullName()));
         }
     }
                                                                                                          );
     SerializationValidator.EntityReferenceTargetMustHaveValidName = new ValidationRule <IEdmEntityReferenceType>((ValidationContext context, IEdmEntityReferenceType entityReference) => {
         if (!EdmUtil.IsQualifiedName(entityReference.EntityType.FullName()))
         {
             context.AddError(entityReference.Location(), EdmErrorCode.ReferencedTypeMustHaveValidName, Strings.Serializer_ReferencedTypeMustHaveValidName(entityReference.EntityType.FullName()));
         }
     }
                                                                                                                  );
     SerializationValidator.EntitySetTypeMustHaveValidName = new ValidationRule <IEdmEntitySet>((ValidationContext context, IEdmEntitySet set) => {
         if (!EdmUtil.IsQualifiedName(set.ElementType.FullName()))
         {
             context.AddError(set.Location(), EdmErrorCode.ReferencedTypeMustHaveValidName, Strings.Serializer_ReferencedTypeMustHaveValidName(set.ElementType.FullName()));
         }
     }
                                                                                                );
     SerializationValidator.StructuredTypeBaseTypeMustHaveValidName = new ValidationRule <IEdmStructuredType>((ValidationContext context, IEdmStructuredType type) => {
         IEdmSchemaType baseType = type.BaseType as IEdmSchemaType;
         if (baseType != null && !EdmUtil.IsQualifiedName(baseType.FullName()))
         {
             context.AddError(type.Location(), EdmErrorCode.ReferencedTypeMustHaveValidName, Strings.Serializer_ReferencedTypeMustHaveValidName(baseType.FullName()));
         }
     }
                                                                                                              );
     SerializationValidator.NavigationPropertyVerifyAssociationName = new ValidationRule <IEdmNavigationProperty>((ValidationContext context, IEdmNavigationProperty property) => {
         if (!EdmUtil.IsQualifiedName(context.Model.GetAssociationFullName(property)))
         {
             context.AddError(property.Location(), EdmErrorCode.ReferencedTypeMustHaveValidName, Strings.Serializer_ReferencedTypeMustHaveValidName(context.Model.GetAssociationFullName(property)));
         }
     }
                                                                                                                  );
     SerializationValidator.VocabularyAnnotationOutOfLineMustHaveValidTargetName = new ValidationRule <IEdmVocabularyAnnotation>((ValidationContext context, IEdmVocabularyAnnotation annotation) => {
         bool hasValue;
         EdmVocabularyAnnotationSerializationLocation?serializationLocation = annotation.GetSerializationLocation(context.Model);
         if (serializationLocation.GetValueOrDefault() != EdmVocabularyAnnotationSerializationLocation.OutOfLine)
         {
             hasValue = false;
         }
         else
         {
             hasValue = serializationLocation.HasValue;
         }
         if (hasValue && !EdmUtil.IsQualifiedName(annotation.TargetString()))
         {
             context.AddError(annotation.Location(), EdmErrorCode.InvalidName, Strings.Serializer_OutOfLineAnnotationTargetMustHaveValidName(EdmUtil.FullyQualifiedName(annotation.Target)));
         }
     }
                                                                                                                                 );
     SerializationValidator.VocabularyAnnotationMustHaveValidTermName = new ValidationRule <IEdmVocabularyAnnotation>((ValidationContext context, IEdmVocabularyAnnotation annotation) => {
         if (!EdmUtil.IsQualifiedName(annotation.Term.FullName()))
         {
             context.AddError(annotation.Location(), EdmErrorCode.InvalidName, Strings.Serializer_OutOfLineAnnotationTargetMustHaveValidName(annotation.Term.FullName()));
         }
     }
                                                                                                                      );
     ValidationRule[] typeReferenceTargetMustHaveValidName = new ValidationRule[18];
     typeReferenceTargetMustHaveValidName[0]     = SerializationValidator.TypeReferenceTargetMustHaveValidName;
     typeReferenceTargetMustHaveValidName[1]     = SerializationValidator.EntityReferenceTargetMustHaveValidName;
     typeReferenceTargetMustHaveValidName[2]     = SerializationValidator.EntitySetTypeMustHaveValidName;
     typeReferenceTargetMustHaveValidName[3]     = SerializationValidator.StructuredTypeBaseTypeMustHaveValidName;
     typeReferenceTargetMustHaveValidName[4]     = SerializationValidator.VocabularyAnnotationOutOfLineMustHaveValidTargetName;
     typeReferenceTargetMustHaveValidName[5]     = SerializationValidator.VocabularyAnnotationMustHaveValidTermName;
     typeReferenceTargetMustHaveValidName[6]     = SerializationValidator.NavigationPropertyVerifyAssociationName;
     typeReferenceTargetMustHaveValidName[7]     = ValidationRules.FunctionImportEntitySetExpressionIsInvalid;
     typeReferenceTargetMustHaveValidName[8]     = ValidationRules.FunctionImportParametersCannotHaveModeOfNone;
     typeReferenceTargetMustHaveValidName[9]     = ValidationRules.FunctionOnlyInputParametersAllowedInFunctions;
     typeReferenceTargetMustHaveValidName[10]    = ValidationRules.TypeMustNotHaveKindOfNone;
     typeReferenceTargetMustHaveValidName[11]    = ValidationRules.PrimitiveTypeMustNotHaveKindOfNone;
     typeReferenceTargetMustHaveValidName[12]    = ValidationRules.PropertyMustNotHaveKindOfNone;
     typeReferenceTargetMustHaveValidName[13]    = ValidationRules.TermMustNotHaveKindOfNone;
     typeReferenceTargetMustHaveValidName[14]    = ValidationRules.SchemaElementMustNotHaveKindOfNone;
     typeReferenceTargetMustHaveValidName[15]    = ValidationRules.EntityContainerElementMustNotHaveKindOfNone;
     typeReferenceTargetMustHaveValidName[16]    = ValidationRules.EnumMustHaveIntegerUnderlyingType;
     typeReferenceTargetMustHaveValidName[17]    = ValidationRules.EnumMemberValueMustHaveSameTypeAsUnderlyingType;
     SerializationValidator.serializationRuleSet = new ValidationRuleSet(typeReferenceTargetMustHaveValidName);
 }