Beispiel #1
0
        /// <summary>
        /// Validate that the expected and actual type names and type kinds are compatible.
        /// </summary>
        /// <param name="collectionItemTypeName">The actual type name.</param>
        /// <param name="collectionItemTypeKind">The actual type kind.</param>
        private void ValidateCollectionItemTypeNameAndKind(string collectionItemTypeName, EdmTypeKind collectionItemTypeKind)
        {
            // Compare the item type kinds.
            if (this.itemTypeKind != collectionItemTypeKind)
            {
                throw new ODataException(Strings.CollectionWithoutExpectedTypeValidator_IncompatibleItemTypeKind(collectionItemTypeKind, this.itemTypeKind));
            }

            if (this.itemTypeKind == EdmTypeKind.Primitive)
            {
                Debug.Assert(this.primitiveItemType != null, "this.primitiveItemType != null");
                Debug.Assert(collectionItemTypeName != null, "collectionItemTypeName != null");

                // NOTE: we do support type inheritance for spatial primitive types; otherwise the type names have to match.
                if (!string.IsNullOrEmpty(this.itemTypeName) && this.itemTypeName.Equals(collectionItemTypeName, System.StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }

                if (this.primitiveItemType.IsSpatial())
                {
                    EdmPrimitiveTypeKind collectionItemPrimitiveKind = EdmCoreModel.Instance.GetPrimitiveTypeKind(collectionItemTypeName);
                    IEdmPrimitiveType    collectionItemPrimitiveType = EdmCoreModel.Instance.GetPrimitiveType(collectionItemPrimitiveKind);

                    if (this.itemTypeDerivedFromCollectionValue)
                    {
                        // If the collection defines an item type, the collection item type has to be assignable to it.
                        if (this.primitiveItemType.IsAssignableFrom(collectionItemPrimitiveType))
                        {
                            return;
                        }
                    }
                    else
                    {
                        // If the collection does not define an item type, the collection items must have a common base type.
                        IEdmPrimitiveType commonBaseType = EdmLibraryExtensions.GetCommonBaseType(this.primitiveItemType, collectionItemPrimitiveType);
                        if (commonBaseType != null)
                        {
                            this.primitiveItemType = commonBaseType;
                            this.itemTypeName      = commonBaseType.FullTypeName();
                            return;
                        }
                    }
                }

                throw new ODataException(Strings.CollectionWithoutExpectedTypeValidator_IncompatibleItemTypeName(collectionItemTypeName, this.itemTypeName));
            }
            else
            {
                // Since we do not support type inheritance for complex types, comparison of the type names is sufficient
                if (string.CompareOrdinal(this.itemTypeName, collectionItemTypeName) != 0)
                {
                    throw new ODataException(Strings.CollectionWithoutExpectedTypeValidator_IncompatibleItemTypeName(collectionItemTypeName, this.itemTypeName));
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Determines the kind of type, then returns appropriate concrete type reference to that primitive type.
        /// </summary>
        /// <param name="type">the abstract primitive type</param>
        /// <param name="isNullable">a boolean to indicate whether the reference can take on a null value</param>
        /// <returns>IEdmPrimitiveTypeReference to type, or null if type is of unknown type</returns>
        private static IEdmPrimitiveTypeReference GetPrimitiveTypeReference(this IEdmPrimitiveType type, bool isNullable)
        {
            switch (type.PrimitiveKind)
            {
            case EdmPrimitiveTypeKind.Boolean:
            case EdmPrimitiveTypeKind.Byte:
            case EdmPrimitiveTypeKind.Date:
            case EdmPrimitiveTypeKind.Double:
            case EdmPrimitiveTypeKind.Guid:
            case EdmPrimitiveTypeKind.Int16:
            case EdmPrimitiveTypeKind.Int32:
            case EdmPrimitiveTypeKind.Int64:
            case EdmPrimitiveTypeKind.SByte:
            case EdmPrimitiveTypeKind.Single:
            case EdmPrimitiveTypeKind.Stream:
            case EdmPrimitiveTypeKind.PrimitiveType:
                return(new EdmPrimitiveTypeReference(type, isNullable));

            case EdmPrimitiveTypeKind.Binary:
                return(new EdmBinaryTypeReference(type, isNullable));

            case EdmPrimitiveTypeKind.String:
                return(new EdmStringTypeReference(type, isNullable));

            case EdmPrimitiveTypeKind.Decimal:
                return(new EdmDecimalTypeReference(type, isNullable));

            case EdmPrimitiveTypeKind.DateTimeOffset:
            case EdmPrimitiveTypeKind.Duration:
            case EdmPrimitiveTypeKind.TimeOfDay:
                return(new EdmTemporalTypeReference(type, isNullable));

            case EdmPrimitiveTypeKind.Geography:
            case EdmPrimitiveTypeKind.GeographyPoint:
            case EdmPrimitiveTypeKind.GeographyLineString:
            case EdmPrimitiveTypeKind.GeographyPolygon:
            case EdmPrimitiveTypeKind.GeographyCollection:
            case EdmPrimitiveTypeKind.GeographyMultiPolygon:
            case EdmPrimitiveTypeKind.GeographyMultiLineString:
            case EdmPrimitiveTypeKind.GeographyMultiPoint:
            case EdmPrimitiveTypeKind.Geometry:
            case EdmPrimitiveTypeKind.GeometryPoint:
            case EdmPrimitiveTypeKind.GeometryLineString:
            case EdmPrimitiveTypeKind.GeometryPolygon:
            case EdmPrimitiveTypeKind.GeometryCollection:
            case EdmPrimitiveTypeKind.GeometryMultiPolygon:
            case EdmPrimitiveTypeKind.GeometryMultiLineString:
            case EdmPrimitiveTypeKind.GeometryMultiPoint:
                return(new EdmSpatialTypeReference(type, isNullable));

            default:
                throw new InvalidOperationException("Unable to obtain a reference to type with name: " + type.FullTypeName());
            }
        }