Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Remove Key from ModelBindingResult
Browse files Browse the repository at this point in the history
  • Loading branch information
rynowak committed May 23, 2016
1 parent ef29749 commit 88c9ae6
Show file tree
Hide file tree
Showing 38 changed files with 136 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Internal;

namespace Microsoft.AspNetCore.Mvc.ModelBinding
Expand All @@ -15,39 +14,24 @@ public struct ModelBindingResult : IEquatable<ModelBindingResult>
/// <summary>
/// Creates a <see cref="ModelBindingResult"/> representing a failed model binding operation.
/// </summary>
/// <param name="key">The key of the current model binding operation.</param>
/// <returns>A <see cref="ModelBindingResult"/> representing a failed model binding operation.</returns>
public static ModelBindingResult Failed(string key)
public static ModelBindingResult Failed()
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}

return new ModelBindingResult(key, model: null, isModelSet: false);
return new ModelBindingResult(model: null, isModelSet: false);
}

/// <summary>
/// Creates a <see cref="ModelBindingResult"/> representing a successful model binding operation.
/// </summary>
/// <param name="key">The key of the current model binding operation.</param>
/// <param name="model">The model value. May be <c>null.</c></param>
/// <returns>A <see cref="ModelBindingResult"/> representing a successful model bind.</returns>
public static ModelBindingResult Success(
string key,
object model)
public static ModelBindingResult Success(object model)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}

return new ModelBindingResult(key, model, isModelSet: true);
return new ModelBindingResult( model, isModelSet: true);
}

private ModelBindingResult(string key, object model, bool isModelSet)
private ModelBindingResult(object model, bool isModelSet)
{
Key = key;
Model = model;
IsModelSet = isModelSet;
}
Expand All @@ -57,16 +41,6 @@ private ModelBindingResult(string key, object model, bool isModelSet)
/// </summary>
public object Model { get; }

/// <summary>
/// <para>
/// Gets the model name which was used to bind the model.
/// </para>
/// <para>
/// This property can be used during validation to add model state for a bound model.
/// </para>
/// </summary>
public string Key { get; }

/// <summary>
/// <para>
/// Gets a value indicating whether or not the <see cref="Model"/> value has been set.
Expand Down Expand Up @@ -96,7 +70,6 @@ public override bool Equals(object obj)
public override int GetHashCode()
{
var hashCodeCombiner = HashCodeCombiner.Start();
hashCodeCombiner.Add(Key, StringComparer.OrdinalIgnoreCase);
hashCodeCombiner.Add(IsModelSet);
hashCodeCombiner.Add(Model);

Expand All @@ -107,25 +80,20 @@ public override int GetHashCode()
public bool Equals(ModelBindingResult other)
{
return
string.Equals(Key, other.Key, StringComparison.OrdinalIgnoreCase) &&
IsModelSet == other.IsModelSet &&
object.Equals(Model, other.Model);
}

/// <inheritdoc />
public override string ToString()
{
if (Key == null)
{
return "No Result";
}
else if (IsModelSet)
if (IsModelSet)
{
return $"Success {Key} -> '{Model}'";
return $"Success '{Model}'";
}
else
{
return $"Failed {Key}";
return $"Failed";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public class ControllerArgumentBinder : IControllerArgumentBinder
_validator.Validate(
controllerContext,
modelBindingContext.ValidationState,
modelBindingResult.Value.Key,
modelBindingContext.ModelName,
modelBindingResult.Value.Model);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,6 @@ public override ValidationStateDictionary ValidationState
}
set
{
if (value.HasValue && value.Value == default(ModelBindingResult))
{
throw new ArgumentException(nameof(ModelBindingResult));
}

_state.Result = value;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.Mvc.Core/Internal/NoOpBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class NoOpBinder : IModelBinder

public Task BindModelAsync(ModelBindingContext bindingContext)
{
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName);
bindingContext.Result = ModelBindingResult.Failed();
return TaskCache.CompletedTask;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task BindModelAsync(ModelBindingContext bindingContext)

if (bindingContext.Result == null)
{
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName);
bindingContext.Result = ModelBindingResult.Failed();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public async Task BindModelAsync(ModelBindingContext bindingContext)
// This model binder is the only handler for the Body binding source and it cannot run twice. Always
// tell the model binding system to skip other model binders and never to fall back i.e. indicate a
// fatal error.
bindingContext.Result = ModelBindingResult.Failed(modelBindingKey);
bindingContext.Result = ModelBindingResult.Failed();
return;
}

Expand All @@ -109,11 +109,11 @@ public async Task BindModelAsync(ModelBindingContext bindingContext)
{
// Formatter encountered an error. Do not use the model it returned. As above, tell the model
// binding system to skip other model binders and never to fall back.
bindingContext.Result = ModelBindingResult.Failed(modelBindingKey);
bindingContext.Result = ModelBindingResult.Failed();
return;
}

bindingContext.Result = ModelBindingResult.Success(modelBindingKey, model);
bindingContext.Result = ModelBindingResult.Success(model);
return;
}
catch (Exception ex)
Expand All @@ -123,7 +123,7 @@ public async Task BindModelAsync(ModelBindingContext bindingContext)
// This model binder is the only handler for the Body binding source and it cannot run twice. Always
// tell the model binding system to skip other model binders and never to fall back i.e. indicate a
// fatal error.
bindingContext.Result = ModelBindingResult.Failed(modelBindingKey);
bindingContext.Result = ModelBindingResult.Failed();
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (valueProviderResult == ValueProviderResult.None)
{
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName);
bindingContext.Result = ModelBindingResult.Failed();
return TaskCache.CompletedTask;
}

Expand All @@ -34,14 +34,14 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
var value = valueProviderResult.FirstValue;
if (string.IsNullOrEmpty(value))
{
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName);
bindingContext.Result = ModelBindingResult.Failed();
return TaskCache.CompletedTask;
}

try
{
var model = Convert.FromBase64String(value);
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model);
bindingContext.Result = ModelBindingResult.Success(model);
return TaskCache.CompletedTask;
}
catch (Exception exception)
Expand All @@ -54,7 +54,7 @@ public Task BindModelAsync(ModelBindingContext bindingContext)

// Matched the type (byte[]) only this binder supports. As in missing data cases, always tell the model
// binding system to skip other model binders i.e. return non-null.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName);
bindingContext.Result = ModelBindingResult.Failed();
return TaskCache.CompletedTask;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
// DO NOT simplify this code by removing the cast.
var model = (object)bindingContext.HttpContext.RequestAborted;
bindingContext.ValidationState.Add(model, new ValidationStateEntry() { SuppressValidation = true });
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model);
bindingContext.Result = ModelBindingResult.Success(model);

return TaskCache.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public virtual async Task BindModelAsync(ModelBindingContext bindingContext)
model = CreateEmptyCollection(bindingContext.ModelType);
}

bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model);
bindingContext.Result = ModelBindingResult.Success(model);
}

return;
Expand Down Expand Up @@ -110,7 +110,7 @@ public virtual async Task BindModelAsync(ModelBindingContext bindingContext)
valueProviderResult);
}

bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model);
bindingContext.Result = ModelBindingResult.Success(model);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ private async Task BindModelCoreAsync(ModelBindingContext bindingContext)
model: propertyModel))
{
await BindProperty(bindingContext);
result = bindingContext.Result ?? ModelBindingResult.Failed(modelName);
result = bindingContext.Result ?? ModelBindingResult.Failed();
}

if (result.IsModelSet)
{
SetProperty(bindingContext, property, result);
SetProperty(bindingContext, modelName, property, result);
}
else if (property.IsBindingRequired)
{
Expand All @@ -104,7 +104,7 @@ private async Task BindModelCoreAsync(ModelBindingContext bindingContext)
}
}

bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, bindingContext.Model);
bindingContext.Result = ModelBindingResult.Success(bindingContext.Model);
}

/// <summary>
Expand Down Expand Up @@ -335,10 +335,12 @@ protected virtual object CreateModel(ModelBindingContext bindingContext)
/// Updates a property in the current <see cref="ModelBindingContext.Model"/>.
/// </summary>
/// <param name="bindingContext">The <see cref="ModelBindingContext"/>.</param>
/// <param name="modelName">The model name.</param>
/// <param name="propertyMetadata">The <see cref="ModelMetadata"/> for the property to set.</param>
/// <param name="result">The <see cref="ModelBindingResult"/> for the property's new value.</param>
protected virtual void SetProperty(
ModelBindingContext bindingContext,
string modelName,
ModelMetadata propertyMetadata,
ModelBindingResult result)
{
Expand All @@ -347,6 +349,11 @@ protected virtual object CreateModel(ModelBindingContext bindingContext)
throw new ArgumentNullException(nameof(bindingContext));
}

if (modelName == null)
{
throw new ArgumentNullException(nameof(modelName));
}

if (propertyMetadata == null)
{
throw new ArgumentNullException(nameof(propertyMetadata));
Expand All @@ -372,12 +379,13 @@ protected virtual object CreateModel(ModelBindingContext bindingContext)
}
catch (Exception exception)
{
AddModelError(exception, bindingContext, result);
AddModelError(exception, modelName, bindingContext, result);
}
}

private static void AddModelError(
Exception exception,
string modelName,
ModelBindingContext bindingContext,
ModelBindingResult result)
{
Expand All @@ -389,11 +397,10 @@ protected virtual object CreateModel(ModelBindingContext bindingContext)

// Do not add an error message if a binding error has already occurred for this property.
var modelState = bindingContext.ModelState;
var modelStateKey = result.Key;
var validationState = modelState.GetFieldValidationState(modelStateKey);
var validationState = modelState.GetFieldValidationState(modelName);
if (validationState == ModelValidationState.Unvalidated)
{
modelState.AddModelError(modelStateKey, exception, bindingContext.ModelMetadata);
modelState.AddModelError(modelName, exception, bindingContext.ModelMetadata);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task BindModelAsync(ModelBindingContext bindingContext)
model = new EmptyFormCollection();
}

bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model);
bindingContext.Result = ModelBindingResult.Success(model);
}

private class EmptyFormCollection : IFormCollection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task BindModelAsync(ModelBindingContext bindingContext)
{
// Silently fail and stop other model binders running if unable to create an instance or use the
// current instance.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName);
bindingContext.Result = ModelBindingResult.Failed();
return;
}

Expand Down Expand Up @@ -65,7 +65,7 @@ public async Task BindModelAsync(ModelBindingContext bindingContext)
if (postedFiles.Count == 0)
{
// Silently fail if the named file does not exist in the request.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName);
bindingContext.Result = ModelBindingResult.Failed();
return;
}

Expand All @@ -77,7 +77,7 @@ public async Task BindModelAsync(ModelBindingContext bindingContext)
{
// Silently fail if no files match. Will bind to an empty collection (treat empty as a success
// case and not reach here) if binding to a top-level object.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName);
bindingContext.Result = ModelBindingResult.Failed();
return;
}

Expand Down Expand Up @@ -111,7 +111,7 @@ public async Task BindModelAsync(ModelBindingContext bindingContext)
rawValue: null,
attemptedValue: null);

bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, value);
bindingContext.Result = ModelBindingResult.Success(value);
}

private async Task GetFormFilesAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
// Silently fail if unable to create an instance or use the current instance. Also reach here in the
// typeof(string) case if the header does not exist in the request and in the
// typeof(IEnumerable<string>) case if the header does not exist and this is not a top-level object.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName);
bindingContext.Result = ModelBindingResult.Failed();
}
else
{
Expand All @@ -63,7 +63,7 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
request.Headers.GetCommaSeparatedValues(headerName),
request.Headers[headerName]);

bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model);
bindingContext.Result = ModelBindingResult.Success(model);
}

return TaskCache.CompletedTask;
Expand Down
Loading

0 comments on commit 88c9ae6

Please sign in to comment.