/// <summary> /// Returns a resource type of the property on the specified resource type. /// </summary> /// <param name="resourceType">The resource type to look for the property on.</param> /// <param name="propertyName">The name of the property to look for.</param> /// <param name="isMultiValueProperty">return true if the property was a MultiValue property.</param> /// <returns>The type of the property specified. Note that for MultiValue properties this returns the type of the item of the MultiValue property.</returns> private static ResourceType GetPropertyType(ResourceType resourceType, string propertyName, out bool isMultiValueProperty) { Debug.Assert(propertyName != null, "propertyName != null"); isMultiValueProperty = false; ResourceProperty resourceProperty = resourceType != null?resourceType.TryResolvePropertyName(propertyName) : null; if (resourceProperty != null) { if (resourceProperty.IsOfKind(ResourcePropertyKind.MultiValue)) { isMultiValueProperty = true; Debug.Assert(resourceProperty.ResourceType is MultiValueResourceType, "MultiValue property must be of the MultiValueResourceType."); MultiValueResourceType multiValueResourceType = (MultiValueResourceType)resourceProperty.ResourceType; return(multiValueResourceType.ItemType); } else { return(resourceProperty.ResourceType); } } else { if (resourceType != null && !resourceType.IsOpenType) { // could be a named stream resourceProperty = resourceType.TryResolveNamedStream(propertyName); if (resourceProperty != null) { throw new ODataException(Strings.EpmSourceTree_NamedStreamCannotBeMapped(propertyName, resourceType.FullName)); } else { throw new ODataException(Strings.EpmSourceTree_MissingPropertyOnType(propertyName, resourceType.FullName)); } } return(null); } }
/// <summary> /// Make the resource type readonly from now on. This means that no more changes can be made to the resource type anymore. /// </summary> public void SetReadOnly() { #if DEBUG IList <ResourceProperty> currentPropertyCollection = this.propertiesDeclaredOnThisType; #endif // If this is a multivalue resource type, set its ItemType to ReadOnly. This has to be done first // as MultiValueResourceType instances are readonly to begin with, but their ItemTypes are not. if (this.ResourceTypeKind == Providers.ResourceTypeKind.MultiValue) { MultiValueResourceType multiValueType = this as MultiValueResourceType; Debug.Assert( multiValueType != null, "ResourceTypeKind is MultiValue but the instance is not of MultiValueResourceType"); multiValueType.ItemType.SetReadOnly(); } // if its already sealed, its a no-op if (this.isReadOnly) { return; } // We need to set readonly at the start to avoid any circular loops that may result due to navigation properties. // If there are any exceptions, we need to set readonly to false. this.isReadOnly = true; // There can be properties with the same name in the base class also (using the new construct) // if the base type is not null, then we need to make sure that there is no property with the same name. // Otherwise, we are only populating property declared for this type and clr gaurantees that they are unique if (this.BaseType != null) { this.BaseType.SetReadOnly(); // Mark current type as OpenType if base is an OpenType if (this.BaseType.IsOpenType && this.ResourceTypeKind != ResourceTypeKind.ComplexType) { this.isOpenType = true; } // Mark the current type as being a Media Link Entry if the base type is a Media Link Entry. if (this.BaseType.IsMediaLinkEntry) { this.isMediaLinkEntry = true; } // Make sure current type is not a CLR type if base is not a CLR type. if (!this.BaseType.CanReflectOnInstanceType) { this.canReflectOnInstanceType = false; } } // set all the properties to readonly if (this.propertiesDeclaredOnThisType != null) { foreach (ResourceProperty p in this.propertiesDeclaredOnThisType) { p.SetReadOnly(); } } #if DEBUG // We cannot change the properties collection method. Basically, we should not be calling Properties or PropertiesDeclaredOnThisType properties // since they call the virtual LoadPropertiesDeclaredOnThisType and we want to postpone that virtual call until we actually need to do something // more useful with the properties Debug.Assert( Object.ReferenceEquals(this.propertiesDeclaredOnThisType, currentPropertyCollection), "We should not have modified the properties collection instance"); #endif }
/// <summary> /// Write the items in a MultiValue in ATOM format. /// </summary> /// <param name="writer">The <see cref="XmlWriter"/> to write to.</param> /// <param name="metadata">The metadata provider to use or null if no metadata is available.</param> /// <param name="multiValue">The MultiValue to write.</param> /// <param name="resourcePropertyType">The resource type of the multi value (or null if not metadata is available).</param> /// <param name="isOpenPropertyType">True if the type name belongs to an open property.</param> /// <param name="isWritingCollection">True if we are writing a collection instead of an entry.</param> /// <param name="version">The protocol version used for writing.</param> /// <param name="epmValueCache">Cache of values used in EPM so that we avoid multiple enumerations of properties/items. (can be null)</param> /// <param name="epmSourcePathSegment">The EPM source path segment which points to the multivalue property we're writing. (can be null)</param> private static void WriteMultiValue( XmlWriter writer, DataServiceMetadataProviderWrapper metadata, ODataMultiValue multiValue, ResourceType resourcePropertyType, bool isOpenPropertyType, bool isWritingCollection, ODataVersion version, EpmValueCache epmValueCache, EpmSourcePathSegment epmSourcePathSegment) { Debug.Assert(multiValue != null, "multiValue != null"); string typeName = multiValue.TypeName; // resolve the type name to the resource type; if no type name is specified we will use the // type inferred from metadata MultiValueResourceType multiValueType = (MultiValueResourceType)MetadataUtils.ResolveTypeName(metadata, resourcePropertyType, ref typeName, ResourceTypeKind.MultiValue, isOpenPropertyType); if (typeName != null) { WritePropertyTypeAttribute(writer, typeName); } ResourceType expectedItemType = multiValueType == null ? null : multiValueType.ItemType; IEnumerable items = EpmValueCache.GetMultiValueItems(epmValueCache, epmSourcePathSegment, multiValue, true); if (items != null) { foreach (object itemValue in items) { object item; EpmMultiValueItemCache epmItemCache = itemValue as EpmMultiValueItemCache; if (epmItemCache != null) { item = epmItemCache.ItemValue; } else { item = itemValue; } ValidationUtils.ValidateMultiValueItem(item); writer.WriteStartElement(AtomConstants.ODataNamespacePrefix, AtomConstants.ODataMultiValueItemElementName, AtomConstants.ODataNamespace); ODataComplexValue complexValue = item as ODataComplexValue; if (complexValue != null) { WriteComplexValue(writer, metadata, complexValue, expectedItemType, false, isWritingCollection, version, epmItemCache, epmSourcePathSegment); } else { ODataMultiValue multiValueItem = item as ODataMultiValue; if (multiValueItem != null) { throw new ODataException(Strings.ODataWriter_NestedMultiValuesAreNotSupported); } else { AtomValueUtils.WritePrimitiveValue(writer, item, expectedItemType); } } writer.WriteEndElement(); } } }