Esempio n. 1
0
        /// <summary>
        /// Resolves the specified entity model schema type and returns the Edm model type for it.
        /// </summary>
        /// <param name="model">The model to get the type from.</param>
        /// <param name="schemaType">The entity model schema type to resolve.</param>
        /// <returns>The resolved type for the specified <paramref name="schemaType"/>.</returns>
        public static IEdmTypeReference ResolveEntityModelSchemaType(IEdmModel model, DataType schemaType)
        {
            if (schemaType == null)
            {
                return(null);
            }

            PrimitiveDataType primitiveDataType = schemaType as PrimitiveDataType;

            if (primitiveDataType != null)
            {
                return(GetPrimitiveTypeReference(primitiveDataType));
            }

            if (model == null)
            {
                return(null);
            }

            EntityDataType entityDataType = schemaType as EntityDataType;

            if (entityDataType != null)
            {
                IEdmNamedElement edmType = model.FindType(entityDataType.Definition.FullName);
                ExceptionUtilities.Assert(
                    edmType != null,
                    "The expected entity type '{0}' was not found in the entity model for this test.",
                    entityDataType.Definition.FullName);

                IEdmEntityType entityType = edmType as IEdmEntityType;
                ExceptionUtilities.Assert(
                    entityType != null,
                    "The expected entity type '{0}' is not defined as entity type in the test's metadata.",
                    entityDataType.Definition.FullName);
                return(entityType.ToTypeReference());
            }

            ComplexDataType complexDataType = schemaType as ComplexDataType;

            if (complexDataType != null)
            {
                return(GetComplexType(model, complexDataType));
            }

            CollectionDataType collectionDataType = schemaType as CollectionDataType;

            if (collectionDataType != null)
            {
                DataType          collectionElementType = collectionDataType.ElementDataType;
                PrimitiveDataType primitiveElementType  = collectionElementType as PrimitiveDataType;
                if (primitiveElementType != null)
                {
                    IEdmPrimitiveTypeReference primitiveElementTypeReference = GetPrimitiveTypeReference(primitiveElementType);
                    return(primitiveElementTypeReference.ToCollectionTypeReference());
                }

                ComplexDataType complexElementType = collectionElementType as ComplexDataType;
                if (complexElementType != null)
                {
                    IEdmComplexTypeReference complexElementTypeReference = GetComplexType(model, complexElementType);
                    return(complexElementTypeReference.ToCollectionTypeReference());
                }

                EntityDataType entityElementType = collectionElementType as EntityDataType;
                if (entityElementType != null)
                {
                    IEdmEntityTypeReference entityElementTypeReference = GetEntityType(model, entityElementType);
                    return(entityElementTypeReference.ToCollectionTypeReference());
                }

                throw new NotSupportedException("Collection types only support primitive, complex, and entity element types.");
            }

            StreamDataType streamType = schemaType as StreamDataType;

            if (streamType != null)
            {
                Type systemType = streamType.GetFacet <PrimitiveClrTypeFacet>().Value;
                ExceptionUtilities.Assert(systemType == typeof(Stream), "Expected the system type 'System.IO.Stream' for a stream reference property.");
                return(MetadataUtils.GetPrimitiveTypeReference(systemType));
            }

            throw new NotImplementedException("Unrecognized schema type " + schemaType.GetType().Name + ".");
        }