diff --git a/src/Microsoft.AspNetCore.OData/Edm/ClrTypeCache.cs b/src/Microsoft.AspNetCore.OData/Edm/ClrTypeCache.cs deleted file mode 100644 index 8b254ed44..000000000 --- a/src/Microsoft.AspNetCore.OData/Edm/ClrTypeCache.cs +++ /dev/null @@ -1,82 +0,0 @@ -//----------------------------------------------------------------------------- -// -// Copyright (c) .NET Foundation and Contributors. All rights reserved. -// See License.txt in the project root for license information. -// -//------------------------------------------------------------------------------ - -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using Microsoft.OData.Edm; - -namespace Microsoft.AspNetCore.OData.Edm -{ - /// - /// CLR type cache - /// - internal class ClrTypeCache - { - private ConcurrentDictionary _clrToEdmTypeCache = new ConcurrentDictionary(); - - private ConcurrentDictionary _edmToClrTypeCache - = new ConcurrentDictionary(new EdmTypeCacheItemComparer()); - - // for unit test only - internal ConcurrentDictionary ClrToEdmTypeCache => _clrToEdmTypeCache; - internal ConcurrentDictionary EdmToClrTypeCache => _edmToClrTypeCache; - - public IEdmTypeReference GetEdmType(Type clrType, IEdmModel model) - { - IEdmTypeReference edmType; - if (!_clrToEdmTypeCache.TryGetValue(clrType, out edmType)) - { - edmType = model.GetEdmTypeReference(clrType); - _clrToEdmTypeCache[clrType] = edmType; - } - - return edmType; - } - - public Type GetClrType(IEdmTypeReference edmType, IEdmModel edmModel) - { - Type clrType; - - EdmTypeCacheItem item = new EdmTypeCacheItem(edmType.Definition, edmType.IsNullable); - if (!_edmToClrTypeCache.TryGetValue(item, out clrType)) - { - clrType = edmModel.GetClrType(edmType); - _edmToClrTypeCache[item] = clrType; - } - - return clrType; - } - - internal struct EdmTypeCacheItem - { - public IEdmType EdmType { get; } - - public bool Nullable { get; } - - public EdmTypeCacheItem(IEdmType edmType, bool nullable) - { - EdmType = edmType; - Nullable = nullable; - } - } - - internal class EdmTypeCacheItemComparer : IEqualityComparer - { - public bool Equals(EdmTypeCacheItem x, EdmTypeCacheItem y) - { - return (x.EdmType == y.EdmType) && (x.Nullable == y.Nullable); - } - - public int GetHashCode(EdmTypeCacheItem obj) - { - string combined = $"{obj.EdmType.FullTypeName()}~{obj.Nullable}"; - return combined.GetHashCode(StringComparison.Ordinal); - } - } - } -} diff --git a/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml b/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml index dd6357a7c..47202d3a0 100644 --- a/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml +++ b/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml @@ -1916,11 +1916,6 @@ The EDM entity type. A collection of operations bound to the feed. - - - CLR type cache - - Allows client to tell OData which are the custom aggregation methods defined. diff --git a/test/Microsoft.AspNetCore.OData.Tests/Edm/ClrTypeCacheTest.cs b/test/Microsoft.AspNetCore.OData.Tests/Edm/ClrTypeCacheTest.cs deleted file mode 100644 index 0f44fa79f..000000000 --- a/test/Microsoft.AspNetCore.OData.Tests/Edm/ClrTypeCacheTest.cs +++ /dev/null @@ -1,161 +0,0 @@ -//----------------------------------------------------------------------------- -// -// Copyright (c) .NET Foundation and Contributors. All rights reserved. -// See License.txt in the project root for license information. -// -//------------------------------------------------------------------------------ - -using System; -using System.Linq; -using Microsoft.AspNetCore.OData.Edm; -using Microsoft.OData.Edm; -using Microsoft.OData.ModelBuilder; -using Xunit; - -namespace Microsoft.AspNetCore.OData.Tests.Edm -{ - public class ClrTypeCacheTest - { - private static IEdmModel _model = GetEdmModel(); - - #region GetEdmType - [Fact] - public void GetEdmType_Returns_CachedInstance() - { - // Arrange - ClrTypeCache cache = new ClrTypeCache(); - IEdmModel model = EdmCoreModel.Instance; - - // Act - IEdmTypeReference edmType1 = cache.GetEdmType(typeof(int), model); - IEdmTypeReference edmType2 = cache.GetEdmType(typeof(int), model); - - // Assert - Assert.NotNull(edmType1); - Assert.Same(edmType1, edmType2); - } - - [Fact] - 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); - } - - [Fact] - 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(); - } - - #endregion - - #region GetClrType - - [Fact] - public void GetClrType_Returns_CorrectType() - { - // Arrange - ClrTypeCache cache = new ClrTypeCache(); - Type clrType = typeof(CacheAddress); - IEdmComplexType address = _model.SchemaElements.OfType().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); - } - - [Fact] - public void GetClrType_Cached_OnlyOneInstance() - { - // Arrange - ClrTypeCache cache = new ClrTypeCache(); - Type clrType = typeof(CacheAddress); - IEdmComplexType address = _model.SchemaElements.OfType().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(); - } - #endregion - - private static IEdmModel GetEdmModel() - { - EdmModel model = new EdmModel(); - - // Address is an open complex type - var addressType = new EdmComplexType("NS", "Address", null, false, true); - addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String); - addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); - model.AddElement(addressType); - - IEdmComplexType edmType = model.FindDeclaredType("NS.Address") as IEdmComplexType; - Type cacheAddressType = typeof(CacheAddress); - model.SetAnnotationValue(edmType, new ClrTypeAnnotation(cacheAddressType)); - return model; - } - - private class CacheAddress - { - public string Street { get; set; } - public string City { get; set; } - } - } -} diff --git a/test/Microsoft.AspNetCore.OData.Tests/Edm/TypeCacheItemTests.cs b/test/Microsoft.AspNetCore.OData.Tests/Edm/TypeCacheItemTests.cs index 61599ee69..241330f82 100644 --- a/test/Microsoft.AspNetCore.OData.Tests/Edm/TypeCacheItemTests.cs +++ b/test/Microsoft.AspNetCore.OData.Tests/Edm/TypeCacheItemTests.cs @@ -7,10 +7,8 @@ using System; using System.Collections.Generic; -using System.Linq; using Microsoft.AspNetCore.OData.Edm; using Microsoft.OData.Edm; -using Microsoft.OData.ModelBuilder; using Moq; using Xunit;