private void VerifyInstanceAnnotations(string name, IODataInstanceAnnotationContainer instanceAnnotationContainer) { switch (name) { case "Name1": Assert.Equal(1, instanceAnnotationContainer.GetResourceAnnotation("NS.Test")); break; case "Name2": Assert.Equal(100, instanceAnnotationContainer.GetPropertyAnnotation("Gender", "NS.TestGender")); break; case "Name3": Assert.Equal(1, instanceAnnotationContainer.GetResourceAnnotation("NS.Test")); Assert.Equal(100, instanceAnnotationContainer.GetPropertyAnnotation("Gender", "NS.TestGender")); break; case "Name4": Assert.Equal(100, instanceAnnotationContainer.GetResourceAnnotation("NS.Test1")); Assert.Equal("Testing", instanceAnnotationContainer.GetResourceAnnotation("NS.Test2")); Assert.Equal(500, instanceAnnotationContainer.GetPropertyAnnotation("Gender", "NS.TestGender")); Assert.Equal("TestName1", instanceAnnotationContainer.GetPropertyAnnotation("Name", "NS.TestName")); break; default: break; } }
/// <summary> /// Gets all instance annotations of a property on the resource. /// </summary> /// <param name="container">The instance annotation container.</param> /// <param name="propertyName">The property name.</param> /// <returns>Dictionary of string(annotation name) and object value(annotation value)</returns> public static IDictionary <string, object> GetPropertyAnnotations(this IODataInstanceAnnotationContainer container, string propertyName) { if (container == null) { throw Error.ArgumentNull(nameof(container)); } return(container.InstanceAnnotations.TryGetValue(propertyName, out IDictionary <string, object> annotationDictionary) ? annotationDictionary : null); }
internal static void ApplyInstanceAnnotations(object resource, IEdmStructuredTypeReference structuredType, ODataResource oDataResource, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext) { PropertyInfo propertyInfo = EdmLibHelpers.GetInstanceAnnotationsContainer(structuredType.StructuredDefinition(), readContext.Model); if (propertyInfo == null) { return; } IODataInstanceAnnotationContainer instanceAnnotationContainer = GetAnnotationContainer(propertyInfo, resource); SetInstanceAnnotations(oDataResource, instanceAnnotationContainer, deserializerProvider, readContext); }
private static void AddInstanceAnnotationToContainer(IODataInstanceAnnotationContainer instanceAnnotationContainer, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext, ODataInstanceAnnotation annotation, string propertyName) { IEdmTypeReference propertyType = null; object annotationValue = ConvertAnnotationValue(annotation.Value, ref propertyType, deserializerProvider, readContext); if (string.IsNullOrEmpty(propertyName)) { instanceAnnotationContainer.AddResourceAnnotation(annotation.Name, annotationValue); } else { instanceAnnotationContainer.AddPropertyAnnotation(propertyName, annotation.Name, annotationValue); } }
public static IODataInstanceAnnotationContainer GetAnnotationContainer(PropertyInfo propertyInfo, object resource) { IDelta delta = resource as IDelta; object value; if (delta != null) { delta.TryGetPropertyValue(propertyInfo.Name, out value); } else { value = propertyInfo.GetValue(resource); } IODataInstanceAnnotationContainer instanceAnnotationContainer = value as IODataInstanceAnnotationContainer; if (instanceAnnotationContainer == null) { try { if (propertyInfo.PropertyType == typeof(ODataInstanceAnnotationContainer) || propertyInfo.PropertyType == typeof(IODataInstanceAnnotationContainer)) { instanceAnnotationContainer = new ODataInstanceAnnotationContainer(); } else { instanceAnnotationContainer = Activator.CreateInstance(propertyInfo.PropertyType) as IODataInstanceAnnotationContainer; } if (delta != null) { delta.TrySetPropertyValue(propertyInfo.Name, instanceAnnotationContainer); } else { propertyInfo.SetValue(resource, instanceAnnotationContainer); } } catch (Exception ex) { throw new ODataException(Error.Format(SRResources.CannotCreateInstanceForProperty, propertyInfo.Name), ex); } } return(instanceAnnotationContainer); }
/// <summary> /// Adds an instance annotation to a property. /// </summary> /// <param name="container">The instance annotation container.</param> /// <param name="propertyName">The property name.</param> /// <param name="annotationName">The annotation name.</param> /// <param name="value">The annotation value.</param> public static void AddPropertyAnnotation(this IODataInstanceAnnotationContainer container, string propertyName, string annotationName, object value) { if (container == null) { throw Error.ArgumentNull(nameof(container)); } VerifyInstanceAnnotationName(annotationName); IDictionary <string, object> annotationDictionary; if (!container.InstanceAnnotations.TryGetValue(propertyName, out annotationDictionary)) { annotationDictionary = new Dictionary <string, object>(); container.InstanceAnnotations.Add(propertyName, annotationDictionary); } annotationDictionary[annotationName] = value; }
internal static void SetInstanceAnnotations(ODataResource oDataResource, IODataInstanceAnnotationContainer instanceAnnotationContainer, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext) { if (oDataResource.InstanceAnnotations != null) { foreach (ODataInstanceAnnotation annotation in oDataResource.InstanceAnnotations) { AddInstanceAnnotationToContainer(instanceAnnotationContainer, deserializerProvider, readContext, annotation, string.Empty); } } foreach (ODataProperty property in oDataResource.Properties) { if (property.InstanceAnnotations != null) { foreach (ODataInstanceAnnotation annotation in property.InstanceAnnotations) { AddInstanceAnnotationToContainer(instanceAnnotationContainer, deserializerProvider, readContext, annotation, property.Name); } } } }
/// <summary> /// Gets all instance annotations of a resource. /// </summary> /// <param name="container">The instance annotation container.</param> /// <returns>Dictionary of string(annotation name) and object value(annotation value)</returns> public static IDictionary <string, object> GetResourceAnnotations(this IODataInstanceAnnotationContainer container) { return(container.GetPropertyAnnotations(string.Empty)); }
/// <summary> /// Adds an instance annotation to the resource. /// </summary> /// <param name="container">The instance annotation container.</param> /// <param name="annotationName">The annotation name.</param> /// <param name="value">The annotation value.</param> public static void AddResourceAnnotation(this IODataInstanceAnnotationContainer container, string annotationName, object value) { container.AddPropertyAnnotation(string.Empty, annotationName, value); }