Make fewer public API changes

This commit is contained in:
Pranav K 2018-07-02 11:20:29 -07:00
parent 4f1b7ccca6
commit 174c5eaa6c
12 changed files with 63 additions and 36 deletions

View File

@ -81,7 +81,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
/// <param name="parameter">The <see cref="ParameterInfo" />.</param>
/// <param name="modelType">The model type.</param>
/// <returns>A <see cref="ModelMetadataIdentity"/>.</returns>
public static ModelMetadataIdentity ForParameter(ParameterInfo parameter, Type modelType)
private static ModelMetadataIdentity ForParameter(ParameterInfo parameter, Type modelType)
{
if (parameter == null)
{

View File

@ -32,27 +32,5 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// <param name="parameter">The <see cref="ParameterInfo"/>.</param>
/// <returns>A <see cref="ModelMetadata"/> instance describing the <paramref name="parameter"/>.</returns>
public abstract ModelMetadata GetMetadataForParameter(ParameterInfo parameter);
/// <summary>
/// Supplies metadata describing a parameter.
/// </summary>
/// <param name="parameter">The <see cref="ParameterInfo"/></param>
/// <param name="modelType">The actual model type.</param>
/// <returns>A <see cref="ModelMetadata"/> instance describing the <paramref name="parameter"/>.</returns>
public virtual ModelMetadata GetMetadataForParameter(ParameterInfo parameter, Type modelType)
{
throw new NotSupportedException();
}
/// <summary>
/// Supplies metadata describing a property.
/// </summary>
/// <param name="propertyInfo">The <see cref="PropertyInfo"/>.</param>
/// <param name="modelType">The actual model type.</param>
/// <returns>A <see cref="ModelMetadata"/> instance describing the <paramref name="propertyInfo"/>.</returns>
public virtual ModelMetadata GetMetadataForProperty(PropertyInfo propertyInfo, Type modelType)
{
throw new NotSupportedException();
}
}
}

View File

@ -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
{

View File

@ -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
{

View File

@ -4,7 +4,7 @@
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Abstractions;
namespace Microsoft.AspNetCore.Mvc.Infrastructure
namespace Microsoft.AspNetCore.Mvc.Internal
{
/// <summary>
/// A <see cref="ParameterDescriptor"/> for action parameters.

View File

@ -4,7 +4,7 @@
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Abstractions;
namespace Microsoft.AspNetCore.Mvc.Infrastructure
namespace Microsoft.AspNetCore.Mvc.Internal
{
/// <summary>
/// A <see cref="ParameterDescriptor"/> for bound properties.

View File

@ -14,12 +14,27 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
/// <summary>
/// A default implementation of <see cref="IModelMetadataProvider"/> based on reflection.
/// </summary>
public class DefaultModelMetadataProvider : ModelMetadataProvider
public class DefaultModelMetadataProvider : ModelMetadataProvider, IModelMetadataProvider2
{
private static readonly Func<ParameterInfo, Type, ModelMetadataIdentity> _modelMetadataIdentityForParameter;
private readonly TypeCache _typeCache = new TypeCache();
private readonly Func<ModelMetadataIdentity, ModelMetadataCacheEntry> _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<ParameterInfo, Type, ModelMetadataIdentity>)
forParameterMethod.CreateDelegate(typeof(Func<ParameterInfo, Type, ModelMetadataIdentity>));
}
/// <summary>
/// Creates a new <see cref="DefaultModelMetadataProvider"/>.
/// </summary>
@ -103,7 +118,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
=> GetMetadataForParameter(parameter, parameter?.ParameterType);
/// <inheritdoc />
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;
}
/// <inheritdoc />
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);
}

View File

@ -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
{
/// <summary>
/// Supplies metadata describing a parameter.
/// </summary>
/// <param name="parameter">The <see cref="ParameterInfo"/></param>
/// <param name="modelType">The actual model type.</param>
/// <returns>A <see cref="ModelMetadata"/> instance describing the <paramref name="parameter"/>.</returns>
ModelMetadata GetMetadataForParameter(ParameterInfo parameter, Type modelType);
/// <summary>
/// Supplies metadata describing a property.
/// </summary>
/// <param name="propertyInfo">The <see cref="PropertyInfo"/>.</param>
/// <param name="modelType">The actual model type.</param>
/// <returns>A <see cref="ModelMetadata"/> instance describing the <paramref name="propertyInfo"/>.</returns>
ModelMetadata GetMetadataForProperty(PropertyInfo propertyInfo, Type modelType);
}
}

View File

@ -149,7 +149,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// <returns>
/// A <see cref="ModelAttributes"/> instance with the attributes of the property and its <see cref="Type"/>.
/// </returns>
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
/// <returns>
/// A <see cref="ModelAttributes"/> instance with the attributes of the parameter and its <see cref="Type"/>.
/// </returns>
public static ModelAttributes GetAttributesForParameter(ParameterInfo parameterInfo, Type modelType)
internal static ModelAttributes GetAttributesForParameter(ParameterInfo parameterInfo, Type modelType)
{
if (parameterInfo == null)
{

View File

@ -344,7 +344,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
if (!modelBindingResult.IsModelSet ||
modelBindingResult.Model == null ||
!(_modelMetadataProvider is ModelMetadataProvider modelMetadataProvider))
!(_modelMetadataProvider is IModelMetadataProvider2 modelMetadataProvider))
{
return;
}

View File

@ -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
{

View File

@ -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
{