Put `ModelBindingHelper` on a diet

- remove extra argument checks
- remove two test-only `ConvertTo()` overloads
 - this relates to #4521
This commit is contained in:
Doug Bunting 2016-07-14 12:48:02 -07:00
parent b7a0393311
commit 52e4ca7232
2 changed files with 39 additions and 222 deletions

View File

@ -44,42 +44,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Internal
IObjectModelValidator objectModelValidator) IObjectModelValidator objectModelValidator)
where TModel : class where TModel : class
{ {
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (prefix == null)
{
throw new ArgumentNullException(nameof(prefix));
}
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
if (metadataProvider == null)
{
throw new ArgumentNullException(nameof(metadataProvider));
}
if (modelBinderFactory == null)
{
throw new ArgumentNullException(nameof(modelBinderFactory));
}
if (valueProvider == null)
{
throw new ArgumentNullException(nameof(valueProvider));
}
if (objectModelValidator == null)
{
throw new ArgumentNullException(nameof(objectModelValidator));
}
// Includes everything by default.
return TryUpdateModelAsync( return TryUpdateModelAsync(
model, model,
prefix, prefix,
@ -88,6 +52,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Internal
modelBinderFactory, modelBinderFactory,
valueProvider, valueProvider,
objectModelValidator, objectModelValidator,
// Includes everything by default.
propertyFilter: (m) => true); propertyFilter: (m) => true);
} }
@ -120,41 +85,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Internal
params Expression<Func<TModel, object>>[] includeExpressions) params Expression<Func<TModel, object>>[] includeExpressions)
where TModel : class where TModel : class
{ {
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (prefix == null)
{
throw new ArgumentNullException(nameof(prefix));
}
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
if (metadataProvider == null)
{
throw new ArgumentNullException(nameof(metadataProvider));
}
if (modelBinderFactory == null)
{
throw new ArgumentNullException(nameof(modelBinderFactory));
}
if (valueProvider == null)
{
throw new ArgumentNullException(nameof(valueProvider));
}
if (objectModelValidator == null)
{
throw new ArgumentNullException(nameof(objectModelValidator));
}
if (includeExpressions == null) if (includeExpressions == null)
{ {
throw new ArgumentNullException(nameof(includeExpressions)); throw new ArgumentNullException(nameof(includeExpressions));
@ -204,46 +134,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Internal
Func<ModelMetadata, bool> propertyFilter) Func<ModelMetadata, bool> propertyFilter)
where TModel : class where TModel : class
{ {
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (prefix == null)
{
throw new ArgumentNullException(nameof(prefix));
}
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
if (metadataProvider == null)
{
throw new ArgumentNullException(nameof(metadataProvider));
}
if (modelBinderFactory == null)
{
throw new ArgumentNullException(nameof(modelBinderFactory));
}
if (valueProvider == null)
{
throw new ArgumentNullException(nameof(valueProvider));
}
if (objectModelValidator == null)
{
throw new ArgumentNullException(nameof(objectModelValidator));
}
if (propertyFilter == null)
{
throw new ArgumentNullException(nameof(propertyFilter));
}
return TryUpdateModelAsync( return TryUpdateModelAsync(
model, model,
typeof(TModel), typeof(TModel),
@ -282,47 +172,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Internal
IValueProvider valueProvider, IValueProvider valueProvider,
IObjectModelValidator objectModelValidator) IObjectModelValidator objectModelValidator)
{ {
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (modelType == null)
{
throw new ArgumentNullException(nameof(modelType));
}
if (prefix == null)
{
throw new ArgumentNullException(nameof(prefix));
}
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
if (metadataProvider == null)
{
throw new ArgumentNullException(nameof(metadataProvider));
}
if (modelBinderFactory == null)
{
throw new ArgumentNullException(nameof(modelBinderFactory));
}
if (valueProvider == null)
{
throw new ArgumentNullException(nameof(valueProvider));
}
if (objectModelValidator == null)
{
throw new ArgumentNullException(nameof(objectModelValidator));
}
// Includes everything by default.
return TryUpdateModelAsync( return TryUpdateModelAsync(
model, model,
modelType, modelType,
@ -332,6 +181,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Internal
modelBinderFactory, modelBinderFactory,
valueProvider, valueProvider,
objectModelValidator, objectModelValidator,
// Includes everything by default.
propertyFilter: (m) => true); propertyFilter: (m) => true);
} }
@ -786,20 +636,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Internal
return capacity.HasValue ? new List<T>(capacity.Value) : new List<T>(); return capacity.HasValue ? new List<T>(capacity.Value) : new List<T>();
} }
/// <summary>
/// Converts the provided <paramref name="value"/> to a value of <see cref="Type"/> <typeparamref name="T"/>
/// using the <see cref="CultureInfo.InvariantCulture"/>.
/// </summary>
/// <typeparam name="T">The <see cref="Type"/> for conversion.</typeparam>
/// <param name="value">The value to convert."/></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>(object value)
{
return ConvertTo<T>(value, culture: null);
}
/// <summary> /// <summary>
/// Converts the provided <paramref name="value"/> to a value of <see cref="Type"/> <typeparamref name="T"/>. /// Converts the provided <paramref name="value"/> to a value of <see cref="Type"/> <typeparamref name="T"/>.
/// </summary> /// </summary>
@ -815,25 +651,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Internal
return converted == null ? default(T) : (T)converted; return converted == null ? default(T) : (T)converted;
} }
/// <summary>
/// Converts the provided <paramref name="value"/> to a value of <see cref="Type"/> <paramref name="type"/>
/// using the <see cref="CultureInfo.InvariantCulture"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <param name="type">The <see cref="Type"/> for conversion.</param>
/// <returns>
/// The converted value or <c>null</c> if the value could not be converted.
/// </returns>
public static object ConvertTo(object value, Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
return ConvertTo(value, type, culture: null);
}
/// <summary> /// <summary>
/// Converts the provided <paramref name="value"/> to a value of <see cref="Type"/> <paramref name="type"/>. /// Converts the provided <paramref name="value"/> to a value of <see cref="Type"/> <paramref name="type"/>.
/// </summary> /// </summary>

View File

@ -843,14 +843,14 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
[Fact] [Fact]
public void ConvertTo_ReturnsNullForReferenceTypes_WhenValueIsNull() public void ConvertTo_ReturnsNullForReferenceTypes_WhenValueIsNull()
{ {
var convertedValue = ModelBindingHelper.ConvertTo(null, typeof(string)); var convertedValue = ModelBindingHelper.ConvertTo(value: null, type: typeof(string), culture: null);
Assert.Null(convertedValue); Assert.Null(convertedValue);
} }
[Fact] [Fact]
public void ConvertTo_ReturnsDefaultForValueTypes_WhenValueIsNull() public void ConvertTo_ReturnsDefaultForValueTypes_WhenValueIsNull()
{ {
var convertedValue = ModelBindingHelper.ConvertTo(null, typeof(int)); var convertedValue = ModelBindingHelper.ConvertTo(value: null, type: typeof(int), culture: null);
Assert.Equal(0, convertedValue); Assert.Equal(0, convertedValue);
} }
@ -861,7 +861,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
var value = new int[] { 1, 20, 42 }; var value = new int[] { 1, 20, 42 };
// Act // Act
var converted = ModelBindingHelper.ConvertTo(value, typeof(string)); var converted = ModelBindingHelper.ConvertTo(value, typeof(string), culture: null);
// Assert // Assert
Assert.Equal("1", converted); Assert.Equal("1", converted);
@ -874,7 +874,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
var value = 42; var value = 42;
// Act // Act
var converted = ModelBindingHelper.ConvertTo<string[]>(value); var converted = ModelBindingHelper.ConvertTo<string[]>(value, culture: null);
// Assert // Assert
Assert.NotNull(converted); Assert.NotNull(converted);
@ -888,7 +888,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var converted = ModelBindingHelper.ConvertTo<string>(42); var converted = ModelBindingHelper.ConvertTo<string>(42, culture: null);
// Assert // Assert
Assert.NotNull(converted); Assert.NotNull(converted);
@ -901,10 +901,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var returned = ModelBindingHelper.ConvertTo<int?>(null); var returned = ModelBindingHelper.ConvertTo<int?>(value: null, culture: null);
// Assert // Assert
Assert.Equal(returned, null); Assert.Null(returned);
} }
[Fact] [Fact]
@ -914,10 +914,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
var original = " "; var original = " ";
// Act // Act
var returned = ModelBindingHelper.ConvertTo<int?>(original); var returned = ModelBindingHelper.ConvertTo<int?>(original, culture: null);
// Assert // Assert
Assert.Equal(returned, null); Assert.Null(returned);
} }
[Fact] [Fact]
@ -926,7 +926,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(new string[] { null }, typeof(int)); var outValue = ModelBindingHelper.ConvertTo(new string[] { null }, typeof(int), culture: null);
// Assert // Assert
Assert.Null(outValue); Assert.Null(outValue);
@ -938,7 +938,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(new int[0], typeof(int)); var outValue = ModelBindingHelper.ConvertTo(new int[0], typeof(int), culture: null);
// Assert // Assert
Assert.Null(outValue); Assert.Null(outValue);
@ -951,7 +951,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
{ {
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(value, typeof(int)); var outValue = ModelBindingHelper.ConvertTo(value, typeof(int), culture: null);
// Assert // Assert
Assert.Null(outValue); Assert.Null(outValue);
@ -963,7 +963,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(null, typeof(int[])); var outValue = ModelBindingHelper.ConvertTo(value: null, type: typeof(int[]), culture: null);
// Assert // Assert
Assert.Null(outValue); Assert.Null(outValue);
@ -975,10 +975,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(new object[] { 1 }, typeof(IntEnum)); var outValue = ModelBindingHelper.ConvertTo(new object[] { 1 }, typeof(IntEnum), culture: null);
// Assert // Assert
Assert.Equal(outValue, IntEnum.Value1); Assert.Equal(IntEnum.Value1, outValue);
} }
[Theory] [Theory]
@ -1008,7 +1008,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(new object[] { input }, enumType); var outValue = ModelBindingHelper.ConvertTo(new object[] { input }, enumType, culture: null);
// Assert // Assert
Assert.Equal(expected, outValue); Assert.Equal(expected, outValue);
@ -1020,10 +1020,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(new object[] { "1" }, typeof(IntEnum)); var outValue = ModelBindingHelper.ConvertTo(new object[] { "1" }, typeof(IntEnum), culture: null);
// Assert // Assert
Assert.Equal(outValue, IntEnum.Value1); Assert.Equal(IntEnum.Value1, outValue);
} }
[Fact] [Fact]
@ -1032,10 +1032,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(new object[] { "Value1" }, typeof(IntEnum)); var outValue = ModelBindingHelper.ConvertTo(new object[] { "Value1" }, typeof(IntEnum), culture: null);
// Assert // Assert
Assert.Equal(outValue, IntEnum.Value1); Assert.Equal(IntEnum.Value1, outValue);
} }
[Fact] [Fact]
@ -1044,7 +1044,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo("12", typeof(int?)); var outValue = ModelBindingHelper.ConvertTo("12", typeof(int?), culture: null);
// Assert // Assert
Assert.Equal(12, outValue); Assert.Equal(12, outValue);
@ -1056,7 +1056,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo("12.5", typeof(double?)); var outValue = ModelBindingHelper.ConvertTo("12.5", typeof(double?), culture: null);
// Assert // Assert
Assert.Equal(12.5, outValue); Assert.Equal(12.5, outValue);
@ -1068,7 +1068,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(12M, typeof(int?)); var outValue = ModelBindingHelper.ConvertTo(12M, typeof(int?), culture: null);
// Assert // Assert
Assert.Equal(12, outValue); Assert.Equal(12, outValue);
@ -1080,7 +1080,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(12.5M, typeof(double?)); var outValue = ModelBindingHelper.ConvertTo(12.5M, typeof(double?), culture: null);
// Assert // Assert
Assert.Equal(12.5, outValue); Assert.Equal(12.5, outValue);
@ -1092,7 +1092,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(12M, typeof(int?)); var outValue = ModelBindingHelper.ConvertTo(12M, typeof(int?), culture: null);
// Assert // Assert
Assert.Equal(12, outValue); Assert.Equal(12, outValue);
@ -1104,7 +1104,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(12M, typeof(long?)); var outValue = ModelBindingHelper.ConvertTo(12M, typeof(long?), culture: null);
// Assert // Assert
Assert.Equal(12L, outValue); Assert.Equal(12L, outValue);
@ -1116,7 +1116,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(new object[] { "some string" }, typeof(string)); var outValue = ModelBindingHelper.ConvertTo(new object[] { "some string" }, typeof(string), culture: null);
// Assert // Assert
Assert.Equal("some string", outValue); Assert.Equal("some string", outValue);
@ -1131,7 +1131,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(value, typeof(IntEnum[])); var outValue = ModelBindingHelper.ConvertTo(value, typeof(IntEnum[]), culture: null);
// Assert // Assert
var result = Assert.IsType<IntEnum[]>(outValue); var result = Assert.IsType<IntEnum[]>(outValue);
@ -1149,7 +1149,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(value, typeof(FlagsEnum[])); var outValue = ModelBindingHelper.ConvertTo(value, typeof(FlagsEnum[]), culture: null);
// Assert // Assert
var result = Assert.IsType<FlagsEnum[]>(outValue); var result = Assert.IsType<FlagsEnum[]>(outValue);
@ -1165,7 +1165,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
var original = new[] { "some string" }; var original = new[] { "some string" };
// Act // Act
var outValue = ModelBindingHelper.ConvertTo(original, typeof(string[])); var outValue = ModelBindingHelper.ConvertTo(original, typeof(string[]), culture: null);
// Assert // Assert
Assert.Same(original, outValue); Assert.Same(original, outValue);
@ -1181,7 +1181,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Act & Assert // Act & Assert
var ex = Assert.Throws<FormatException>( var ex = Assert.Throws<FormatException>(
() => ModelBindingHelper.ConvertTo("this-is-not-a-valid-value", destinationType)); () => ModelBindingHelper.ConvertTo("this-is-not-a-valid-value", destinationType, culture: null));
} }
[Fact] [Fact]
@ -1205,7 +1205,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act & Assert // Act & Assert
Assert.Equal(expectedValue, ModelBindingHelper.ConvertTo(initialValue, typeof(T))); Assert.Equal(expectedValue, ModelBindingHelper.ConvertTo(initialValue, typeof(T), culture: null));
} }
public static IEnumerable<object[]> IntrinsicConversionData public static IEnumerable<object[]> IntrinsicConversionData
@ -1248,7 +1248,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Act & Assert // Act & Assert
var ex = Assert.Throws<InvalidOperationException>( var ex = Assert.Throws<InvalidOperationException>(
() => ModelBindingHelper.ConvertTo(new MyClassWithoutConverter(), destinationType)); () => ModelBindingHelper.ConvertTo(new MyClassWithoutConverter(), destinationType, culture: null));
Assert.Equal(expectedMessage, ex.Message); Assert.Equal(expectedMessage, ex.Message);
} }
@ -1264,7 +1264,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Act & Assert // Act & Assert
var ex = Assert.Throws<InvalidOperationException>( var ex = Assert.Throws<InvalidOperationException>(
() => ModelBindingHelper.ConvertTo(value, destinationType)); () => ModelBindingHelper.ConvertTo(value, destinationType, culture: null));
Assert.Equal(expectedMessage, ex.Message); Assert.Equal(expectedMessage, ex.Message);
} }
@ -1278,7 +1278,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
var value = new MySubClassWithoutConverter(); var value = new MySubClassWithoutConverter();
// Act // Act
var result = ModelBindingHelper.ConvertTo(value, destinationType); var result = ModelBindingHelper.ConvertTo(value, destinationType, culture: null);
// Assert // Assert
Assert.Same(value, result); Assert.Same(value, result);
@ -1298,7 +1298,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
}; };
// Act // Act
var result = ModelBindingHelper.ConvertTo(value, destinationType); var result = ModelBindingHelper.ConvertTo(value, destinationType, culture: null);
// Assert // Assert
Assert.IsType(destinationType, result); Assert.IsType(destinationType, result);
@ -1323,7 +1323,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// Arrange // Arrange
// Act // Act
var outValue = ModelBindingHelper.ConvertTo<FlagsEnum>(value); var outValue = ModelBindingHelper.ConvertTo<FlagsEnum>(value, culture: null);
// Assert // Assert
Assert.Equal(expected, outValue); Assert.Equal(expected, outValue);