-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
- add single-parameter `ValueProviderResult` constructor - use in most of the greedy and type-matching model binders - revert changes to `FormCollectionModelBinder` and related tests - do _not_ add a `ModelState` entry in this binder - add doc comments in `ValueProviderResult` class - simplify logic in `FormFileModelBinder` - exit early instead of using `typeMatched` variable nits: - use new `ValueProviderResult` constructor in many existing tests - `""` -> `string.Empty` and `vpr` -> `valueProviderResult` in `ValueProviderResultTest` - improve some test names in `BodyValidationIntegrationTests` - do not check `Message` of a Json.NET `Exception`
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,25 +12,59 @@ | |
|
||
namespace Microsoft.AspNet.Mvc.ModelBinding | ||
{ | ||
/// <summary> | ||
/// Result of an <see cref="IValueProvider.GetValueAsync"/> operation. | ||
/// </summary> | ||
public class ValueProviderResult | ||
{ | ||
private static readonly CultureInfo _staticCulture = CultureInfo.InvariantCulture; | ||
private CultureInfo _instanceCulture; | ||
|
||
// default constructor so that subclassed types can set the properties themselves | ||
/// <summary> | ||
/// Instantiates a new instance of the <see cref="ValueProviderResult"/> class. Subclass must at least set | ||
/// <see cref="Culture"/> to avoid <see cref="Exception"/>s in <see cref="ConvertTo(Type, CultureInfo)"/>. | ||
/// </summary> | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dougbu
Author
Member
|
||
protected ValueProviderResult() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Instantiates a new instance of the <see cref="ValueProviderResult"/> class with given | ||
/// <paramref name="rawValue"/>. Initializes <see cref="Culture"/> to | ||
/// <see cref="CultureInfo.InvariantCulture"/>. | ||
This comment has been minimized.
Sorry, something went wrong.
dougbu
Author
Member
|
||
/// </summary> | ||
/// <param name="rawValue">The <see cref="RawValue"/> value of the new instance.</param> | ||
public ValueProviderResult(object rawValue) | ||
: this(rawValue, attemptedValue: null, culture: _staticCulture) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Instantiates a new instance of the <see cref="ValueProviderResult"/> class with given | ||
/// <paramref name="rawValue"/>, <paramref name="attemptedValue"/>, and <paramref name="culture"/>. | ||
/// </summary> | ||
/// <param name="rawValue">The <see cref="RawValue"/> value of the new instance.</param> | ||
/// <param name="attemptedValue">The <see cref="AttemptedValue"/> value of the new instance.</param> | ||
/// <param name="culture">The <see cref="Culture"/> value of the new instance.</param> | ||
public ValueProviderResult(object rawValue, string attemptedValue, CultureInfo culture) | ||
{ | ||
RawValue = rawValue; | ||
AttemptedValue = attemptedValue; | ||
Culture = culture; | ||
} | ||
|
||
/// <summary> | ||
/// <see cref="string"/> conversion of <see cref="RawValue"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// Used in helpers that generate <c><textarea></c> elements as well as some error messages. | ||
/// </remarks> | ||
public string AttemptedValue { get; protected set; } | ||
|
||
/// <summary> | ||
/// <see cref="CultureInfo"/> to use in <see cref="ConvertTo(Type)"/> or | ||
/// <see cref="ConvertTo(Type, CultureInfo)"/> if passed <see cref="CultureInfo"/> is <c>null</c>. | ||
/// </summary> | ||
public CultureInfo Culture | ||
{ | ||
get | ||
|
@@ -44,13 +78,37 @@ public CultureInfo Culture | |
protected set { _instanceCulture = value; } | ||
} | ||
|
||
/// <summary> | ||
/// The provided <see cref="object"/>. | ||
/// </summary> | ||
public object RawValue { get; protected set; } | ||
|
||
/// <summary> | ||
/// Converts <see cref="RawValue"/> to the given <paramref name="type"/>. Uses <see cref="Culture"/> for | ||
/// <see cref="TypeConverter"/> operations. | ||
/// </summary> | ||
/// <param name="type">The target <see cref="Type"/> of the conversion.</param> | ||
/// <returns> | ||
/// <see cref="RawValue"/> converted to the given <paramref name="type"/>. <c>null</c> if the conversion fails. | ||
/// </returns> | ||
public object ConvertTo(Type type) | ||
{ | ||
return ConvertTo(type, culture: null); | ||
} | ||
|
||
/// <summary> | ||
/// Converts <see cref="RawValue"/> to the given <paramref name="type"/> using the given | ||
/// <paramref name="culture"/>. | ||
/// </summary> | ||
/// <param name="type">The target <see cref="Type"/> of the conversion.</param> | ||
/// <param name="culture"> | ||
/// The <see cref="CultureInfo"/> to use for <see cref="TypeConverter"/> operations. Uses | ||
/// <see cref="Culture"/> if this parameter is <c>null</c>. | ||
/// </param> | ||
/// <returns> | ||
/// <see cref="RawValue"/> converted to the given <paramref name="type"/> using the given | ||
/// <paramref name="culture"/>. <c>null</c> if the conversion fails. | ||
/// </returns> | ||
public virtual object ConvertTo([NotNull] Type type, CultureInfo culture) | ||
{ | ||
var value = RawValue; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Globalization; | ||
#if DNXCORE50 | ||
using System.Reflection; | ||
#endif | ||
|
@@ -61,7 +62,7 @@ protected override Task<ModelBindingResult> BindModelCoreAsync([NotNull] ModelBi | |
model); | ||
|
||
var attemptedValue = (model as string) ?? request.Headers.Get(headerName); | ||
var valueProviderResult = new ValueProviderResult(model, attemptedValue, culture: null); | ||
var valueProviderResult = new ValueProviderResult(model, attemptedValue, CultureInfo.InvariantCulture); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dougbu
Author
Member
|
||
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult); | ||
} | ||
|
||
|
This seems like over-sharing. Why do we even have this
protected
constructor?