public void GetClrType_Cached_OnlyOneInstance() { // Arrange ClrTypeCache cache = new ClrTypeCache(); Type clrType = typeof(CacheAddress); IEdmComplexType address = _model.SchemaElements.OfType <IEdmComplexType>().FirstOrDefault(c => c.Name == "Address"); Action cacheCallAndVerify = () => { IEdmComplexTypeReference addressTypeTrue = new EdmComplexTypeReference(address, true); Type actual = cache.GetClrType(addressTypeTrue, _model); Assert.Same(clrType, actual); IEdmComplexTypeReference addressTypeFalse = new EdmComplexTypeReference(address, false); actual = cache.GetClrType(addressTypeFalse, _model); Assert.Same(clrType, actual); Assert.Equal(2, cache.EdmToClrTypeCache.Count); }; // Act & Assert cacheCallAndVerify(); // 5 is a magic number, it doesn't matter, just want to call it mulitple times. for (int i = 0; i < 5; i++) { cacheCallAndVerify(); } cacheCallAndVerify(); }
/// <inheritdoc /> internal ODataSerializer GetODataPayloadSerializerImpl(Type type, Func <IEdmModel> modelFunction, ODataPath path, Type errorType) { if (type == null) { throw Error.ArgumentNull("type"); } if (modelFunction == null) { throw Error.ArgumentNull("modelFunction"); } // handle the special types. if (type == typeof(ODataServiceDocument)) { return(_rootContainer.GetRequiredService <ODataServiceDocumentSerializer>()); } else if (type == typeof(Uri) || type == typeof(ODataEntityReferenceLink)) { return(_rootContainer.GetRequiredService <ODataEntityReferenceLinkSerializer>()); } else if (TypeHelper.IsTypeAssignableFrom(typeof(IEnumerable <Uri>), type) || type == typeof(ODataEntityReferenceLinks)) { return(_rootContainer.GetRequiredService <ODataEntityReferenceLinksSerializer>()); } else if (type == typeof(ODataError) || type == errorType) { return(_rootContainer.GetRequiredService <ODataErrorSerializer>()); } else if (TypeHelper.IsTypeAssignableFrom(typeof(IEdmModel), type)) { return(_rootContainer.GetRequiredService <ODataMetadataSerializer>()); } // Get the model. Using a Func<IEdmModel> to delay evaluation of the model // until after the above checks have passed. IEdmModel model = modelFunction(); // if it is not a special type, assume it has a corresponding EdmType. ClrTypeCache typeMappingCache = model.GetTypeMappingCache(); IEdmTypeReference edmType = typeMappingCache.GetEdmType(type, model); if (edmType != null) { bool isCountRequest = path != null && path.Segments.LastOrDefault() is CountSegment; bool isRawValueRequest = path != null && path.Segments.LastOrDefault() is ValueSegment; if (((edmType.IsPrimitive() || edmType.IsEnum()) && isRawValueRequest) || isCountRequest) { return(_rootContainer.GetRequiredService <ODataRawValueSerializer>()); } else { return(GetEdmTypeSerializer(edmType)); } } else { return(null); } }
/// <inheritdoc /> public override ODataDeserializer GetODataDeserializer(IEdmModel model, Type type, HttpRequestMessage request) { if (type == null) { throw Error.ArgumentNull("type"); } if (model == null) { throw Error.ArgumentNull("model"); } if (type == typeof(Uri)) { return(_entityReferenceLinkDeserializer); } if (type == typeof(ODataActionParameters) || type == typeof(ODataUntypedActionParameters)) { return(_actionPayloadDeserializer); } ClrTypeCache typeMappingCache = model.GetTypeMappingCache(); IEdmTypeReference edmType = typeMappingCache.GetEdmType(type, model); if (edmType == null) { return(null); } else { return(GetEdmTypeDeserializer(edmType)); } }
internal IEdmTypeReference GetEdmType(object instance, Type type) { IEdmTypeReference edmType; IEdmObject edmObject = instance as IEdmObject; if (edmObject != null) { edmType = edmObject.GetEdmType(); if (edmType == null) { throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName, typeof(IEdmObject).Name); } } else { if (Model == null) { throw Error.InvalidOperation(SRResources.RequestMustHaveModel); } _typeMappingCache = _typeMappingCache ?? Model.GetTypeMappingCache(); edmType = _typeMappingCache.GetEdmType(type, Model); if (edmType == null) { throw Error.InvalidOperation(SRResources.ClrTypeNotInModel, type); } } return(edmType); }
public void GetEdmType_Cached_OnlyOneInstance() { // Arrange ClrTypeCache cache = new ClrTypeCache(); Type clrType = typeof(CacheAddress); Action cacheCallAndVerify = () => { IEdmTypeReference edmType = cache.GetEdmType(clrType, _model); Assert.NotNull(edmType); Assert.Equal("NS.Address", edmType.FullName()); Assert.Single(cache.ClrToEdmTypeCache); }; // Act & Assert cacheCallAndVerify(); // 5 is a magic number, it doesn't matter, just want to call it mulitple times. for (int i = 0; i < 5; i++) { cacheCallAndVerify(); } cacheCallAndVerify(); }
/// <inheritdoc /> public override ODataDeserializer GetODataDeserializer(Type type, HttpRequestMessage request) { if (type == null) { throw Error.ArgumentNull("type"); } if (type == typeof(Uri)) { return(_rootContainer.GetRequiredService <ODataEntityReferenceLinkDeserializer>()); } if (type == typeof(ODataActionParameters) || type == typeof(ODataUntypedActionParameters)) { return(_rootContainer.GetRequiredService <ODataActionPayloadDeserializer>()); } IEdmModel model = request.GetModel(); ClrTypeCache typeMappingCache = model.GetTypeMappingCache(); IEdmTypeReference edmType = typeMappingCache.GetEdmType(type, model); if (edmType == null) { return(null); } else { return(GetEdmTypeDeserializer(edmType)); } }
/// <inheritdoc /> public ODataDeserializer GetODataDeserializer(IEdmModel model, Type type, HttpRequest request) { if (type == null) { throw Error.ArgumentNull("type"); } /* * if (type == typeof(Uri)) * { * return _serviceProvider.GetRequiredService<ODataEntityReferenceLinkDeserializer>(); * } * * if (type == typeof(ODataActionParameters) || type == typeof(ODataUntypedActionParameters)) * { * return _serviceProvider.GetRequiredService<ODataActionPayloadDeserializer>(); * }*/ //IEdmModel model = request.GetModel(); ClrTypeCache typeMappingCache = model.GetTypeMappingCache(); IEdmTypeReference edmType = typeMappingCache.GetEdmType(type, model); if (edmType == null) { return(null); } else { return(GetEdmTypeDeserializer(edmType)); } }
/// <inheritdoc /> public virtual ODataSerializer GetODataPayloadSerializer(HttpContext context, Type type) { if (context == null) { throw Error.ArgumentNull("context"); } if (type == null) { throw Error.ArgumentNull("type"); } IServiceProvider provider = context.RequestServices; var x = context.Response.StatusCode; // handle the special types. if (type == typeof(ODataServiceDocument)) { return(provider.GetRequiredService <ODataServiceDocumentSerializer>()); } else if (type == typeof(Uri) || type == typeof(ODataEntityReferenceLink)) { return(provider.GetRequiredService <ODataEntityReferenceLinkSerializer>()); } else if (typeof(IEnumerable <Uri>).IsAssignableFrom(type) || type == typeof(ODataEntityReferenceLinks)) { return(provider.GetRequiredService <ODataEntityReferenceLinksSerializer>()); } else if (type == typeof(ODataError) || type == typeof(SerializableError)) { return(provider.GetRequiredService <ODataErrorSerializer>()); } else if (typeof(IEdmModel).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) { return(provider.GetRequiredService <ODataMetadataSerializer>()); } // if it is not a special type, assume it has a corresponding EdmType. IEdmModel model = context.ODataFeature().Model; ClrTypeCache typeMappingCache = model.GetTypeMappingCache(); IEdmTypeReference edmType = typeMappingCache.GetEdmType(type, model); if (edmType != null) { if (((edmType.IsPrimitive() || edmType.IsEnum()) && ODataRawValueMediaTypeMapping.IsRawValueRequest(context)) || ODataCountMediaTypeMapping.IsCountRequest(context)) { return(provider.GetRequiredService <ODataRawValueSerializer>()); } else { return(GetEdmTypeSerializer(context, edmType)); } } else { return(null); } }
/// <inheritdoc /> public override ODataSerializer GetODataPayloadSerializer(IEdmModel model, Type type, HttpRequestMessage request) { if (model == null) { throw Error.ArgumentNull("model"); } if (type == null) { throw Error.ArgumentNull("type"); } if (request == null) { throw Error.ArgumentNull("request"); } // handle the special types. if (type == typeof(ODataServiceDocument)) { return(_workspaceSerializer); } else if (type == typeof(Uri) || type == typeof(ODataEntityReferenceLink)) { return(_entityReferenceLinkSerializer); } else if (typeof(IEnumerable <Uri>).IsAssignableFrom(type) || type == typeof(ODataEntityReferenceLinks)) { return(_entityReferenceLinksSerializer); } else if (type == typeof(ODataError) || type == typeof(HttpError)) { return(_errorSerializer); } else if (typeof(IEdmModel).IsAssignableFrom(type)) { return(_metadataSerializer); } // if it is not a special type, assume it has a corresponding EdmType. ClrTypeCache typeMappingCache = model.GetTypeMappingCache(); IEdmTypeReference edmType = typeMappingCache.GetEdmType(type, model); if (edmType != null) { if (((edmType.IsPrimitive() || edmType.IsEnum()) && ODataRawValueMediaTypeMapping.IsRawValueRequest(request)) || ODataCountMediaTypeMapping.IsCountRequest(request)) { return(_rawValueSerializer); } else { return(GetEdmTypeSerializer(edmType)); } } else { return(null); } }
public void GetTypeMappingCache_ReturnsCachedInstance_IfCalledMultipleTimes() { IEdmModel model = new EdmModel(); ClrTypeCache cache1 = model.GetTypeMappingCache(); ClrTypeCache cache2 = model.GetTypeMappingCache(); Assert.Same(cache1, cache2); }
internal IEdmTypeReference GetEdmType(object instance, Type type) { IEdmTypeReference edmType = null; IEdmObject edmObject = instance as IEdmObject; if (edmObject != null) { edmType = edmObject.GetEdmType(); if (edmType == null) { throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName, typeof(IEdmObject).Name); } } else { if (Model == null) { throw Error.InvalidOperation(SRResources.RequestMustHaveModel); } IEdmModelClrTypeMappingHandler typeMappingHandler = Model.GetAnnotationValue <IEdmModelClrTypeMappingHandler>(Model); if (typeMappingHandler != null) { edmType = typeMappingHandler.MapClrInstanceToEdmTypeReference(Model, instance); } if (edmType == null) { _typeMappingCache = _typeMappingCache ?? Model.GetTypeMappingCache(); edmType = _typeMappingCache.GetEdmType(type, Model); if (edmType == null) { if (instance != null) { edmType = _typeMappingCache.GetEdmType(instance.GetType(), Model); } if (edmType == null) { throw Error.InvalidOperation(SRResources.ClrTypeNotInModel, type); } } else if (instance != null) { IEdmTypeReference actualType = _typeMappingCache.GetEdmType(instance.GetType(), Model); if (actualType != null && actualType != edmType) { edmType = actualType; } } } } return(edmType); }
public void GetEdmType_Returns_CachedInstance() { ClrTypeCache cache = new ClrTypeCache(); IEdmModel model = EdmCoreModel.Instance; IEdmTypeReference edmType1 = cache.GetEdmType(typeof(int), model); IEdmTypeReference edmType2 = cache.GetEdmType(typeof(int), model); Assert.NotNull(edmType1); Assert.Same(edmType1, edmType2); }
internal IEdmTypeReference GetEdmType(object instance, Type type) { IEdmTypeReference edmType; IEdmObject edmObject = instance as IEdmObject; if (edmObject != null && instance.GetType().GetProperty("ModelID").GetValue(instance) != null) { edmType = edmObject.GetEdmType(); if (edmType == null) { throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName, typeof(IEdmObject).Name); } } else { if (Model == null) { throw Error.InvalidOperation(SRResources.RequestMustHaveModel); } var cacheType = instance.GetType().GetProperty("Items"); type = cacheType != null?cacheType.GetValue(instance).GetType() : type; _typeMappingCache = _typeMappingCache ?? Model.GetTypeMappingCache(); edmType = _typeMappingCache.GetEdmType(type, Model); if (edmType == null) { if (instance != null) { edmType = _typeMappingCache.GetEdmType(instance.GetType(), Model); } if (edmType == null) { throw Error.InvalidOperation(SRResources.ClrTypeNotInModel, type); } } else if (instance != null) { IEdmTypeReference actualType = _typeMappingCache.GetEdmType(instance.GetType(), Model); if (actualType != null && actualType != edmType) { edmType = actualType; } } } return(edmType); }
public void GetEdmType_Returns_CachedInstance(Type clrType) { // Arrange ClrTypeCache cache = new ClrTypeCache(); IEdmModel model = EdmCoreModel.Instance; // Act IEdmTypeReference edmType1 = cache.GetEdmType(clrType, model); IEdmTypeReference edmType2 = cache.GetEdmType(clrType, model); // Assert Assert.NotNull(edmType1); Assert.Same(edmType1, edmType2); }
internal static ClrTypeCache GetTypeMappingCache(this IEdmModel model) { Contract.Assert(model != null); ClrTypeCache typeMappingCache = model.GetAnnotationValue <ClrTypeCache>(model); if (typeMappingCache == null) { typeMappingCache = new ClrTypeCache(); model.SetAnnotationValue(model, typeMappingCache); } return(typeMappingCache); }
public void GetEdmType_Returns_CachedInstance_ForClrType() { // Arrange ClrTypeCache cache = new ClrTypeCache(); Type clrType = typeof(CacheAddress); // Act IEdmTypeReference edmType1 = cache.GetEdmType(clrType, _model); IEdmTypeReference edmType2 = cache.GetEdmType(clrType, _model); // Assert Assert.NotNull(edmType1); Assert.Same(edmType1, edmType2); Assert.Equal("NS.Address", edmType1.FullName()); Assert.True(edmType1.IsNullable); }
public void GetClrType_Returns_CorrectType() { // Arrange ClrTypeCache cache = new ClrTypeCache(); Type clrType = typeof(CacheAddress); IEdmComplexType address = _model.SchemaElements.OfType <IEdmComplexType>().FirstOrDefault(c => c.Name == "Address"); // Act & Assert IEdmComplexTypeReference addressType = new EdmComplexTypeReference(address, true); Type actual = cache.GetClrType(addressType, _model); Assert.Same(clrType, actual); addressType = new EdmComplexTypeReference(address, false); actual = cache.GetClrType(addressType, _model); Assert.Same(clrType, actual); }
/// <inheritdoc /> public override ODataDeserializer GetODataDeserializer(Type type, HttpRequest request) { if (type == null) { throw Error.ArgumentNull(nameof(type)); } if (request == null) { throw Error.ArgumentNull(nameof(request)); } if (type == typeof(Uri)) { return(_serviceProvider.GetRequiredService <ODataEntityReferenceLinkDeserializer>()); } if (type == typeof(ODataActionParameters) || type == typeof(ODataUntypedActionParameters)) { return(_serviceProvider.GetRequiredService <ODataActionPayloadDeserializer>()); } if (type == typeof(EdmChangedObjectCollection) || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(DeltaSet <>))) { return(_serviceProvider.GetRequiredService <ODataDeltaResourceSetDeserializer>()); } IEdmModel model = request.GetModel(); //IODataTypeMappingProvider typeMappingProvider = _serviceProvider.GetRequiredService<IODataTypeMappingProvider>(); ClrTypeCache typeMappingCache = model.GetTypeMappingCache(); IEdmTypeReference edmType = typeMappingCache.GetEdmType(type, model); //IEdmTypeReference edmType = typeMappingProvider.GetEdmType(model, type); if (edmType == null) { return(null); } else { return(GetEdmTypeDeserializer(edmType)); } }
/// <inheritdoc /> public override ODataDeserializer GetODataDeserializer(Type type, HttpRequest request) { if (type == null) { throw Error.ArgumentNull(nameof(type)); } if (request == null) { throw Error.ArgumentNull(nameof(request)); } if (type == typeof(Uri)) { return(_serviceProvider.GetRequiredService <ODataEntityReferenceLinkDeserializer>()); } if (type == typeof(ODataActionParameters) || type == typeof(ODataUntypedActionParameters)) { return(_serviceProvider.GetRequiredService <ODataActionPayloadDeserializer>()); } IEdmModel model = request.GetModel(); //IODataTypeMappingProvider typeMappingProvider = _serviceProvider.GetRequiredService<IODataTypeMappingProvider>(); ClrTypeCache typeMappingCache = model.GetTypeMappingCache(); IEdmTypeReference edmType = typeMappingCache.GetEdmType(type, model); //IEdmTypeReference edmType = typeMappingProvider.GetEdmType(model, type); if (edmType == null) { return(null); } else { bool isDelta = IsDelta(type); return(GetEdmTypeDeserializer(edmType, isDelta)); } }
/// <inheritdoc /> internal ODataDeserializer GetODataDeserializerImpl(Type type, Func <IEdmModel> modelFunction) { if (type == null) { throw Error.ArgumentNull("type"); } if (modelFunction == null) { throw Error.ArgumentNull("modelFunction"); } if (type == typeof(Uri)) { return(_rootContainer.GetRequiredService <ODataEntityReferenceLinkDeserializer>()); } if (type == typeof(ODataActionParameters) || type == typeof(ODataUntypedActionParameters)) { return(_rootContainer.GetRequiredService <ODataActionPayloadDeserializer>()); } // Get the model. Using a Func<IEdmModel> to delay evaluation of the model // until after the above checks have passed. IEdmModel model = modelFunction(); ClrTypeCache typeMappingCache = model.GetTypeMappingCache(); IEdmTypeReference edmType = typeMappingCache.GetEdmType(type, model); if (edmType == null) { return(null); } else { return(GetEdmTypeDeserializer(edmType)); } }
/// <summary> /// Gets the appropriate ODataDeserializer for a Type, converting it into an Edm type if necessary. /// /// This method was copied from DefaultODataDeserializerProvider, except instead of returning deserializers via DI, /// it directly returns deserializers so that the ActionPayloadDeserializer could be replaced with the ODataMigrationActionPayloadDeserializer, /// which is used if the incoming type is a set of parameters for an OData action. /// </summary> /// <param name="type">Type to get deserializer for.</param> /// <param name="modelFunction">Function to get IEdmModel (to delay evaluation of model)</param> /// <returns>Appropriate ODataDeserializer for this type.</returns> private ODataDeserializer GetODataDeserializerImpl(Type type, Func <IEdmModel> modelFunction) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (modelFunction == null) { throw new ArgumentNullException(nameof(modelFunction)); } if (type == typeof(Uri)) { return(new ODataEntityReferenceLinkDeserializer()); } if (type == typeof(ODataActionParameters) || type == typeof(ODataUntypedActionParameters)) { return(new ODataMigrationActionPayloadDeserializer(this)); } // Get the model. Using a Func<IEdmModel> to delay evaluation of the model // until after the above checks have passed. IEdmModel model = modelFunction(); ClrTypeCache typeMappingCache = model.GetTypeMappingCache(); IEdmTypeReference edmType = typeMappingCache.GetEdmType(type, model); if (edmType == null) { return(null); } else { return(GetEdmTypeDeserializer(edmType)); } }
internal IEdmTypeReference GetEdmType(object instance, Type type) { IEdmTypeReference edmType; IEdmObject edmObject = instance as IEdmObject; if (edmObject != null) { edmType = edmObject.GetEdmType(); if (edmType == null) { throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName, typeof(IEdmObject).Name); } } else { if (Model == null) { throw Error.InvalidOperation(SRResources.RequestMustHaveModel); } _typeMappingCache = _typeMappingCache ?? Model.GetTypeMappingCache(); edmType = _typeMappingCache.GetEdmType(type, Model); if (edmType == null) { if (instance != null) { edmType = _typeMappingCache.GetEdmType(instance.GetType(), Model); } if (edmType == null) { throw Error.InvalidOperation(SRResources.ClrTypeNotInModel, type); } } } return edmType; }
/// <inheritdoc /> public override ODataSerializer GetODataPayloadSerializer(Type type, HttpRequest request) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (request == null) { throw new ArgumentNullException(nameof(request)); } ODataPath path = request.ODataFeature().Path; Type errorType = typeof(SerializableError); // handle the special types. if (type == typeof(ODataServiceDocument)) { return(_serviceProvider.GetRequiredService <ODataServiceDocumentSerializer>()); } else if (type == typeof(Uri) || type == typeof(ODataEntityReferenceLink)) { return(_serviceProvider.GetRequiredService <ODataEntityReferenceLinkSerializer>()); } else if (TypeHelper.IsTypeAssignableFrom(typeof(IEnumerable <Uri>), type) || type == typeof(ODataEntityReferenceLinks)) { return(_serviceProvider.GetRequiredService <ODataEntityReferenceLinksSerializer>()); } else if (type == typeof(ODataError) || type == errorType) { return(_serviceProvider.GetRequiredService <ODataErrorSerializer>()); } else if (TypeHelper.IsTypeAssignableFrom(typeof(IEdmModel), type)) { return(_serviceProvider.GetRequiredService <ODataMetadataSerializer>()); } IEdmModel model = request.GetModel(); // if it is not a special type, assume it has a corresponding EdmType. ClrTypeCache typeMappingCache = model.GetTypeMappingCache(); IEdmTypeReference edmType = typeMappingCache.GetEdmType(type, model); if (edmType != null) { bool isCountRequest = path != null && path.LastSegment is CountSegment; bool isRawValueRequest = path != null && path.LastSegment is ValueSegment; if (((edmType.IsPrimitive() || edmType.IsEnum()) && isRawValueRequest) || isCountRequest) { return(_serviceProvider.GetRequiredService <ODataRawValueSerializer>()); } else { return(GetEdmTypeSerializer(edmType)); } } else { return(null); } }