From 174c5eaa6c8370a4d3c4ba898aee9e7a5904720c Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 2 Jul 2018 11:20:29 -0700 Subject: [PATCH] Make fewer public API changes --- .../Metadata/ModelMetadataIdentity.cs | 2 +- .../ModelBinding/ModelMetadataProvider.cs | 22 -------------- .../ControllerBoundPropertyDescriptor.cs | 2 +- .../ControllerParameterDescriptor.cs | 2 +- .../IParameterInfoParameterDescriptor.cs | 2 +- .../IPropertyInfoParameterDescriptor.cs | 2 +- .../Metadata/DefaultModelMetadataProvider.cs | 30 ++++++++++++++++--- .../Metadata/IModelMetadataProvider2.cs | 27 +++++++++++++++++ .../ModelBinding/Metadata/ModelAttributes.cs | 4 +-- .../ModelBinding/ParameterBinder.cs | 2 +- .../HandlerParameterDescriptor.cs | 2 +- .../PageBoundPropertyDescriptor.cs | 2 +- 12 files changed, 63 insertions(+), 36 deletions(-) rename src/Microsoft.AspNetCore.Mvc.Core/{Infrastructure => Internal}/IParameterInfoParameterDescriptor.cs (92%) rename src/Microsoft.AspNetCore.Mvc.Core/{Infrastructure => Internal}/IPropertyInfoParameterDescriptor.cs (91%) create mode 100644 src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/IModelMetadataProvider2.cs diff --git a/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/Metadata/ModelMetadataIdentity.cs b/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/Metadata/ModelMetadataIdentity.cs index af97d9a043..913576e6a8 100644 --- a/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/Metadata/ModelMetadataIdentity.cs +++ b/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/Metadata/ModelMetadataIdentity.cs @@ -81,7 +81,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata /// The . /// The model type. /// A . - public static ModelMetadataIdentity ForParameter(ParameterInfo parameter, Type modelType) + private static ModelMetadataIdentity ForParameter(ParameterInfo parameter, Type modelType) { if (parameter == null) { diff --git a/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/ModelMetadataProvider.cs b/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/ModelMetadataProvider.cs index 147ccc45f5..1b4f01cd6d 100644 --- a/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/ModelMetadataProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/ModelMetadataProvider.cs @@ -32,27 +32,5 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding /// The . /// A instance describing the . public abstract ModelMetadata GetMetadataForParameter(ParameterInfo parameter); - - /// - /// Supplies metadata describing a parameter. - /// - /// The - /// The actual model type. - /// A instance describing the . - public virtual ModelMetadata GetMetadataForParameter(ParameterInfo parameter, Type modelType) - { - throw new NotSupportedException(); - } - - /// - /// Supplies metadata describing a property. - /// - /// The . - /// The actual model type. - /// A instance describing the . - public virtual ModelMetadata GetMetadataForProperty(PropertyInfo propertyInfo, Type modelType) - { - throw new NotSupportedException(); - } } } diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Controllers/ControllerBoundPropertyDescriptor.cs b/src/Microsoft.AspNetCore.Mvc.Core/Controllers/ControllerBoundPropertyDescriptor.cs index 709dffcf5c..f150169fd3 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Controllers/ControllerBoundPropertyDescriptor.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Controllers/ControllerBoundPropertyDescriptor.cs @@ -3,7 +3,7 @@ using System.Reflection; using Microsoft.AspNetCore.Mvc.Abstractions; -using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.AspNetCore.Mvc.Internal; namespace Microsoft.AspNetCore.Mvc.Controllers { diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Controllers/ControllerParameterDescriptor.cs b/src/Microsoft.AspNetCore.Mvc.Core/Controllers/ControllerParameterDescriptor.cs index 593a40d0fc..e52cf741fb 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Controllers/ControllerParameterDescriptor.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Controllers/ControllerParameterDescriptor.cs @@ -3,7 +3,7 @@ using System.Reflection; using Microsoft.AspNetCore.Mvc.Abstractions; -using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.AspNetCore.Mvc.Internal; namespace Microsoft.AspNetCore.Mvc.Controllers { diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IParameterInfoParameterDescriptor.cs b/src/Microsoft.AspNetCore.Mvc.Core/Internal/IParameterInfoParameterDescriptor.cs similarity index 92% rename from src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IParameterInfoParameterDescriptor.cs rename to src/Microsoft.AspNetCore.Mvc.Core/Internal/IParameterInfoParameterDescriptor.cs index 93e6a09b28..6f260b5750 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IParameterInfoParameterDescriptor.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Internal/IParameterInfoParameterDescriptor.cs @@ -4,7 +4,7 @@ using System.Reflection; using Microsoft.AspNetCore.Mvc.Abstractions; -namespace Microsoft.AspNetCore.Mvc.Infrastructure +namespace Microsoft.AspNetCore.Mvc.Internal { /// /// A for action parameters. diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IPropertyInfoParameterDescriptor.cs b/src/Microsoft.AspNetCore.Mvc.Core/Internal/IPropertyInfoParameterDescriptor.cs similarity index 91% rename from src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IPropertyInfoParameterDescriptor.cs rename to src/Microsoft.AspNetCore.Mvc.Core/Internal/IPropertyInfoParameterDescriptor.cs index 5a9a8682d1..ccbe5de73c 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IPropertyInfoParameterDescriptor.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Internal/IPropertyInfoParameterDescriptor.cs @@ -4,7 +4,7 @@ using System.Reflection; using Microsoft.AspNetCore.Mvc.Abstractions; -namespace Microsoft.AspNetCore.Mvc.Infrastructure +namespace Microsoft.AspNetCore.Mvc.Internal { /// /// A for bound properties. diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/DefaultModelMetadataProvider.cs b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/DefaultModelMetadataProvider.cs index b9e374701e..e15c0c3532 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/DefaultModelMetadataProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/DefaultModelMetadataProvider.cs @@ -14,12 +14,27 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata /// /// A default implementation of based on reflection. /// - public class DefaultModelMetadataProvider : ModelMetadataProvider + public class DefaultModelMetadataProvider : ModelMetadataProvider, IModelMetadataProvider2 { + private static readonly Func _modelMetadataIdentityForParameter; + private readonly TypeCache _typeCache = new TypeCache(); private readonly Func _cacheEntryFactory; private readonly ModelMetadataCacheEntry _metadataCacheEntryForObjectType; + static DefaultModelMetadataProvider() + { + var forParameterMethod = typeof(ModelMetadataIdentity).GetMethod( + nameof(ModelMetadataIdentity.ForParameter), + BindingFlags.Static | BindingFlags.NonPublic, + binder: null, + types: new[] { typeof(ParameterInfo), typeof(Type) }, + modifiers: null); + + _modelMetadataIdentityForParameter = (Func) + forParameterMethod.CreateDelegate(typeof(Func)); + } + /// /// Creates a new . /// @@ -103,7 +118,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata => GetMetadataForParameter(parameter, parameter?.ParameterType); /// - public override ModelMetadata GetMetadataForParameter(ParameterInfo parameter, Type modelType) + ModelMetadata IModelMetadataProvider2.GetMetadataForParameter(ParameterInfo parameter, Type modelType) + => GetMetadataForParameter(parameter, modelType); + + internal ModelMetadata GetMetadataForParameter(ParameterInfo parameter, Type modelType) { if (parameter == null) { @@ -133,8 +151,12 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata return cacheEntry.Metadata; } + /// - public override ModelMetadata GetMetadataForProperty(PropertyInfo propertyInfo, Type modelType) + ModelMetadata IModelMetadataProvider2.GetMetadataForProperty(PropertyInfo parameter, Type modelType) + => GetMetadataForProperty(parameter, modelType); + + internal ModelMetadata GetMetadataForProperty(PropertyInfo propertyInfo, Type modelType) { if (propertyInfo == null) { @@ -183,7 +205,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata private ModelMetadataCacheEntry GetCacheEntry(ParameterInfo parameter, Type modelType) { return _typeCache.GetOrAdd( - ModelMetadataIdentity.ForParameter(parameter, modelType), + _modelMetadataIdentityForParameter(parameter, modelType), _cacheEntryFactory); } diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/IModelMetadataProvider2.cs b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/IModelMetadataProvider2.cs new file mode 100644 index 0000000000..b9d8d809f4 --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/IModelMetadataProvider2.cs @@ -0,0 +1,27 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Reflection; + +namespace Microsoft.AspNetCore.Mvc.ModelBinding +{ + internal interface IModelMetadataProvider2 + { + /// + /// Supplies metadata describing a parameter. + /// + /// The + /// The actual model type. + /// A instance describing the . + ModelMetadata GetMetadataForParameter(ParameterInfo parameter, Type modelType); + + /// + /// Supplies metadata describing a property. + /// + /// The . + /// The actual model type. + /// A instance describing the . + ModelMetadata GetMetadataForProperty(PropertyInfo propertyInfo, Type modelType); + } +} diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/ModelAttributes.cs b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/ModelAttributes.cs index 0f9a5bf103..c5d48ed8a9 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/ModelAttributes.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/ModelAttributes.cs @@ -149,7 +149,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding /// /// A instance with the attributes of the property and its . /// - public static ModelAttributes GetAttributesForProperty(Type containerType, PropertyInfo property, Type modelType) + internal static ModelAttributes GetAttributesForProperty(Type containerType, PropertyInfo property, Type modelType) { if (containerType == null) { @@ -231,7 +231,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding /// /// A instance with the attributes of the parameter and its . /// - public static ModelAttributes GetAttributesForParameter(ParameterInfo parameterInfo, Type modelType) + internal static ModelAttributes GetAttributesForParameter(ParameterInfo parameterInfo, Type modelType) { if (parameterInfo == null) { diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/ParameterBinder.cs b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/ParameterBinder.cs index a7f1b8e5ba..7a04fe8be3 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/ParameterBinder.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/ParameterBinder.cs @@ -344,7 +344,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding if (!modelBindingResult.IsModelSet || modelBindingResult.Model == null || - !(_modelMetadataProvider is ModelMetadataProvider modelMetadataProvider)) + !(_modelMetadataProvider is IModelMetadataProvider2 modelMetadataProvider)) { return; } diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/HandlerParameterDescriptor.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/HandlerParameterDescriptor.cs index 099b86192c..cc1be6d7b8 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/HandlerParameterDescriptor.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/HandlerParameterDescriptor.cs @@ -3,7 +3,7 @@ using System.Reflection; using Microsoft.AspNetCore.Mvc.Abstractions; -using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.AspNetCore.Mvc.Internal; namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure { diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageBoundPropertyDescriptor.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageBoundPropertyDescriptor.cs index cb02222300..4bb1f43ca9 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageBoundPropertyDescriptor.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageBoundPropertyDescriptor.cs @@ -3,7 +3,7 @@ using System.Reflection; using Microsoft.AspNetCore.Mvc.Abstractions; -using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.AspNetCore.Mvc.Internal; namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure {