Remove `ValueProviderResultExtensions`

- #5063
- update tests that used one extension method to instead use `ModelBindingHelper` directly

nit: `mbc` -> `context`
This commit is contained in:
Doug Bunting 2017-03-06 14:02:20 -08:00
parent cc6f0f6a26
commit 824d65ca3d
5 changed files with 36 additions and 71 deletions

View File

@ -1,65 +0,0 @@
// 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 Microsoft.AspNetCore.Mvc.ModelBinding.Internal;
namespace Microsoft.AspNetCore.Mvc.ModelBinding
{
/// <summary>
/// Extensions methods for <see cref="ValueProviderResult"/>.
/// </summary>
[Obsolete("This type is obsolete and will be removed in a future version. " +
"The recommended alternative is System.ComponentModel.TypeDescriptor.GetConverter().")]
public static class ValueProviderResultExtensions
{
/// <summary>
/// Attempts to convert the values in <paramref name="result"/> to the specified type.
/// </summary>
/// <typeparam name="T">The <see cref="Type"/> for conversion.</typeparam>
/// <param name="result">The <see cref="ValueProviderResult"/>.</param>
/// <returns>
/// The converted value, or the default value of <typeparamref name="T"/> if the value could not be converted.
/// </returns>
public static T ConvertTo<T>(this ValueProviderResult result)
{
object valueToConvert = null;
if (result.Values.Count == 1)
{
valueToConvert = result.Values[0];
}
else if (result.Values.Count > 1)
{
valueToConvert = result.Values.ToArray();
}
return ModelBindingHelper.ConvertTo<T>(valueToConvert, result.Culture);
}
/// <summary>
/// Attempts to convert the values in <paramref name="result"/> to the specified type.
/// </summary>
/// <param name="result">The <see cref="ValueProviderResult"/>.</param>
/// <param name="type">The <see cref="Type"/> for conversion.</param>
/// <returns>
/// The converted value, or the default value of <paramref name="type"/> if the value could not be converted.
/// </returns>
public static object ConvertTo(this ValueProviderResult result, Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
object valueToConvert = null;
if (result.Values.Count == 1)
{
valueToConvert = result.Values[0];
}
else if (result.Values.Count > 1)
{
valueToConvert = result.Values.ToArray();
}
return ModelBindingHelper.ConvertTo(valueToConvert, type, result.Culture);
}
}
}

View File

@ -2,5 +2,9 @@
{
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ProducesAttribute : Microsoft.AspNetCore.Mvc.Filters.ResultFilterAttribute, Microsoft.AspNetCore.Mvc.ApiExplorer.IApiResponseMetadataProvider",
"Kind": "Removal"
},
{
"OldTypeId": "public static class Microsoft.AspNetCore.Mvc.ModelBinding.ValueProviderResultExtensions",
"Kind": "Removal"
}
]

View File

@ -2,5 +2,9 @@
{
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ProducesAttribute : Microsoft.AspNetCore.Mvc.Filters.ResultFilterAttribute, Microsoft.AspNetCore.Mvc.ApiExplorer.IApiResponseMetadataProvider",
"Kind": "Removal"
},
{
"OldTypeId": "public static class Microsoft.AspNetCore.Mvc.ModelBinding.ValueProviderResultExtensions",
"Kind": "Removal"
}
]

View File

@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding.Internal;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
@ -143,12 +144,22 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
private static IModelBinder CreateIntBinder()
{
return new StubModelBinder(mbc =>
return new StubModelBinder(context =>
{
var value = mbc.ValueProvider.GetValue(mbc.ModelName);
var value = context.ValueProvider.GetValue(context.ModelName);
if (value != ValueProviderResult.None)
{
var model = value.ConvertTo(mbc.ModelType);
object valueToConvert = null;
if (value.Values.Count == 1)
{
valueToConvert = value.Values[0];
}
else if (value.Values.Count > 1)
{
valueToConvert = value.Values.ToArray();
}
var model = ModelBindingHelper.ConvertTo(valueToConvert, context.ModelType, value.Culture);
return ModelBindingResult.Success(model);
}
return ModelBindingResult.Failed();

View File

@ -8,6 +8,7 @@ using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Xunit;
@ -372,15 +373,25 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
private static IModelBinder CreateIntBinder()
{
return new StubModelBinder(mbc =>
return new StubModelBinder(context =>
{
var value = mbc.ValueProvider.GetValue(mbc.ModelName);
var value = context.ValueProvider.GetValue(context.ModelName);
if (value == ValueProviderResult.None)
{
return ModelBindingResult.Failed();
}
var model = value.ConvertTo(mbc.ModelType);
object valueToConvert = null;
if (value.Values.Count == 1)
{
valueToConvert = value.Values[0];
}
else if (value.Values.Count > 1)
{
valueToConvert = value.Values.ToArray();
}
var model = ModelBindingHelper.ConvertTo(valueToConvert, context.ModelType, value.Culture);
if (model == null)
{
return ModelBindingResult.Failed();