/// <summary> /// Constructs a new instance of Astoria type using the specified clr type. /// </summary> /// <param name="instanceType">CLR type that represents the flow format inside the Astoria runtime.</param> /// <param name="resourceTypeKind">Kind of the resource type.</param> /// <param name="baseType">Base type of the resource type.</param> /// <param name="namespaceName">Namespace name of the given resource type.</param> /// <param name="name">Name of the given resource type.</param> /// <param name="isAbstract">Whether the resource type is an abstract type or not.</param> public ResourceType( Type instanceType, ResourceTypeKind resourceTypeKind, ResourceType baseType, string namespaceName, string name, bool isAbstract) : this(instanceType, baseType, namespaceName, name, isAbstract) { ExceptionUtils.CheckArgumentNotNull(instanceType, "instanceType"); ExceptionUtils.CheckArgumentStringNotNullOrEmpty(name, "name"); CheckResourceTypeKind(resourceTypeKind, "resourceTypeKind"); if (resourceTypeKind == ResourceTypeKind.Primitive || resourceTypeKind == ResourceTypeKind.MultiValue) { throw new ArgumentException(Strings.ResourceType_InvalidValueForResourceTypeKind, "resourceTypeKind"); } if (baseType != null && baseType.ResourceTypeKind != resourceTypeKind) { throw new ArgumentException( Strings.ResourceType_InvalidResourceTypeKindInheritance(resourceTypeKind.ToString(), baseType.ResourceTypeKind.ToString()), "resourceTypeKind"); } if (instanceType.IsValueType) { throw new ArgumentException(Strings.ResourceType_TypeCannotBeValueType, "instanceType"); } this.resourceTypeKind = resourceTypeKind; }
public ResourceType AddResourceType(string name, string typeName, ResourceTypeKind resourceTypeKind, bool isOpenType, string baseType, string namespaceName, bool isAbstract) { //TODO: Add to existing container (possible?) // add to service container ResourceType baseResourceType = null; if (baseType != null) { foreach (ResourceContainer rc in this.ServiceContainer.ResourceContainers) { foreach (ResourceType rt in rc) { if (rt.Name == baseType) { baseResourceType = rt; break; } } } } ResourceType newResourceType = null; if (baseResourceType == null) newResourceType = Resource.ResourceType(name, namespaceName); else newResourceType = Resource.ResourceType(name, namespaceName, baseResourceType); // call service op to add to metadata string url = this.ServiceUri + String.Format("/AddResourceType?name='{0}'&typeName='{1}'&resourceTypeKind='{2}'&isOpenType={3}" + "{4}{5}&isAbstract=false", name, typeName, resourceTypeKind.ToString(), isOpenType.ToString().ToLowerInvariant(), baseType == null ? "" : "&baseType='" + baseType + "'", namespaceName == null ? "&namespaceName='" + this.ContextNamespace + "'" : "&namespaceName='" + namespaceName + "'"); this.ExecuteServiceOp(url); return newResourceType; }
/// <summary>Creates an instance of a data service <see cref="T:Microsoft.OData.Service.Providers.ResourceType" />.</summary> /// <param name="instanceType">CLR type that represents the format inside the WCF Data Services?runtime.</param> /// <param name="resourceTypeKind"> /// <see cref="T:Microsoft.OData.Service.Providers.ResourceTypeKind" /> of the resource type.</param> /// <param name="baseType">Base type of the resource type as string.</param> /// <param name="namespaceName">Namespace name of the resource type as string.</param> /// <param name="name">Name of the given resource type as string.</param> /// <param name="isAbstract">Boolean value that indicates whether the resource type is an abstract type.</param> public ResourceType( Type instanceType, ResourceTypeKind resourceTypeKind, ResourceType baseType, string namespaceName, string name, bool isAbstract) : this(instanceType, baseType, namespaceName, name, isAbstract) { WebUtil.CheckArgumentNull(instanceType, "instanceType"); WebUtil.CheckStringArgumentNullOrEmpty(name, "name"); WebUtil.CheckResourceTypeKind(resourceTypeKind, "resourceTypeKind"); if (resourceTypeKind == ResourceTypeKind.Primitive || resourceTypeKind == ResourceTypeKind.Collection || resourceTypeKind == Providers.ResourceTypeKind.EntityCollection) { throw new ArgumentException(Strings.ResourceType_InvalidValueForResourceTypeKind("resourceTypeKind"), "resourceTypeKind"); } if (baseType != null && baseType.ResourceTypeKind != resourceTypeKind) { throw new ArgumentException( Strings.ResourceType_InvalidResourceTypeKindInheritance(resourceTypeKind.ToString(), baseType.ResourceTypeKind.ToString()), "resourceTypeKind"); } if (instanceType.IsValueType) { throw new ArgumentException(Strings.ResourceType_TypeCannotBeValueType, "instanceType"); } this.resourceTypeKind = resourceTypeKind; }
/// <summary> /// Validates that <paramref name="typeName"/> is a valid type name of the specified kind (<paramref name="typeKind"/>). /// </summary> /// <param name="metadata">The metadata against which to validate the type name.</param> /// <param name="typeName">The type name to validate.</param> /// <param name="typeKind">The expected <see cref="ResourceTypeKind"/> of the type.</param> /// <returns>A <see cref="ResourceType"/> for the <paramref name="typeName"/>.</returns> internal static ResourceType ValidateNonMultiValueTypeName(DataServiceMetadataProviderWrapper metadata, string typeName, ResourceTypeKind typeKind) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(metadata != null, "metadata != null"); ResourceType resourceType = metadata.TryResolveResourceType(typeName); if (resourceType == null) { throw new ODataException(Strings.ODataWriterCore_UnrecognizedTypeName(typeName, typeKind)); } if (resourceType.ResourceTypeKind != typeKind) { throw new ODataException(Strings.ODataWriterCore_IncorrectTypeKind(typeName, typeKind.ToString(), resourceType.ResourceTypeKind.ToString())); } return(resourceType); }
/// <summary> /// Validates that the (optional) <paramref name="typeFromMetadata"/> is compatible with the (optional) <paramref name="typeFromValue"/>. /// </summary> /// <param name="typeFromMetadata">The (optional) type from the metadata definition.</param> /// <param name="typeFromValue">The (optional) type derived from the value.</param> /// <param name="typeKind">The expected type kind.</param> /// <returns>The type of the property as derived from the <paramref name="typeFromMetadata"/> and/or <paramref name="typeFromValue"/>.</returns> internal static ResourceType ValidateMetadataType(ResourceType typeFromMetadata, ResourceType typeFromValue, ResourceTypeKind typeKind) { DebugUtils.CheckNoExternalCallers(); if (typeFromMetadata == null) { // if we have no metadata information there is nothing to validate return(typeFromValue); } if (typeFromValue == null) { // derive the property type from the metadata if (typeFromMetadata.ResourceTypeKind != typeKind) { throw new ODataException(Strings.ODataWriter_IncompatibleTypeKind(typeKind.ToString(), typeFromMetadata.Name, typeFromMetadata.ResourceTypeKind.ToString())); } return(typeFromMetadata); } else { // Make sure the types are consistent if (typeFromMetadata.FullName != typeFromValue.FullName) { throw new ODataException(Strings.ODataWriter_IncompatibleType(typeFromValue.FullName, typeFromMetadata.FullName)); } return(typeFromValue); } }
internal IEdmSchemaType EnsureSchemaType(ResourceType resourceType) { IEdmSchemaType type; ResourceTypeKind resourceTypeKind = resourceType.ResourceTypeKind; if (resourceTypeKind == ResourceTypeKind.Primitive) { return(MetadataProviderUtils.CreatePrimitiveTypeReference(resourceType).PrimitiveDefinition()); } string typeNamespace = this.GetTypeNamespace(resourceType); string key = ComputeSchemaTypeCacheKey(typeNamespace, resourceType); if (this.schemaTypeCache.TryGetValue(key, out type)) { return(type); } switch (resourceTypeKind) { case ResourceTypeKind.EntityType: return(this.AddEntityType(resourceType, typeNamespace)); case ResourceTypeKind.ComplexType: return(this.AddComplexType(resourceType, typeNamespace)); } throw new InvalidOperationException(System.Data.Services.Strings.MetadataProviderEdmModel_UnsupportedSchemaTypeKind(resourceTypeKind.ToString())); }
/// <summary> /// Validates that <paramref name="typeName"/> is a valid type name of the specified kind (<paramref name="typeKind"/>). /// </summary> /// <param name="metadata">The metadata against which to validate the type name.</param> /// <param name="typeName">The type name to validate.</param> /// <param name="typeKind">The expected <see cref="ResourceTypeKind"/> of the type.</param> /// <returns>A <see cref="ResourceType"/> for the <paramref name="typeName"/>.</returns> internal static ResourceType ValidateNonMultiValueTypeName(DataServiceMetadataProviderWrapper metadata, string typeName, ResourceTypeKind typeKind) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(metadata != null, "metadata != null"); ResourceType resourceType = metadata.TryResolveResourceType(typeName); if (resourceType == null) { throw new ODataException(Strings.ODataWriterCore_UnrecognizedTypeName(typeName, typeKind)); } if (resourceType.ResourceTypeKind != typeKind) { throw new ODataException(Strings.ODataWriterCore_IncorrectTypeKind(typeName, typeKind.ToString(), resourceType.ResourceTypeKind.ToString())); } return resourceType; }
/// <summary> /// Validates that the (optional) <paramref name="typeFromMetadata"/> is compatible with the (optional) <paramref name="typeFromValue"/>. /// </summary> /// <param name="typeFromMetadata">The (optional) type from the metadata definition.</param> /// <param name="typeFromValue">The (optional) type derived from the value.</param> /// <param name="typeKind">The expected type kind.</param> /// <returns>The type of the property as derived from the <paramref name="typeFromMetadata"/> and/or <paramref name="typeFromValue"/>.</returns> internal static ResourceType ValidateMetadataType(ResourceType typeFromMetadata, ResourceType typeFromValue, ResourceTypeKind typeKind) { DebugUtils.CheckNoExternalCallers(); if (typeFromMetadata == null) { // if we have no metadata information there is nothing to validate return typeFromValue; } if (typeFromValue == null) { // derive the property type from the metadata if (typeFromMetadata.ResourceTypeKind != typeKind) { throw new ODataException(Strings.ODataWriter_IncompatibleTypeKind(typeKind.ToString(), typeFromMetadata.Name, typeFromMetadata.ResourceTypeKind.ToString())); } return typeFromMetadata; } else { // Make sure the types are consistent if (typeFromMetadata.FullName != typeFromValue.FullName) { throw new ODataException(Strings.ODataWriter_IncompatibleType(typeFromValue.FullName, typeFromMetadata.FullName)); } return typeFromValue; } }