Move `ModelStateDictionaryExtensions` into `Microsoft.AspNet.Mvc.ModelBinding` namespace

- missed the inconsistency when reviewing
- kept class in ViewFeatures assembly because it depends on `ExpressionHelper`

nit: wrap some long lines
This commit is contained in:
Doug Bunting 2015-09-26 17:11:29 -07:00
parent b28debf442
commit eff10cdd66
2 changed files with 21 additions and 10 deletions

View File

@ -4,10 +4,9 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.ViewFeatures; using Microsoft.AspNet.Mvc.ViewFeatures;
namespace Microsoft.AspNet.Mvc namespace Microsoft.AspNet.Mvc.ModelBinding
{ {
/// <summary> /// <summary>
/// Extensions methods for <see cref="ModelStateDictionary"/>. /// Extensions methods for <see cref="ModelStateDictionary"/>.
@ -22,7 +21,10 @@ namespace Microsoft.AspNet.Mvc
/// <param name="modelState">The <see cref="ModelStateDictionary"/> instance this method extends.</param> /// <param name="modelState">The <see cref="ModelStateDictionary"/> instance this method extends.</param>
/// <param name="expression">An expression to be evaluated against an item in the current model.</param> /// <param name="expression">An expression to be evaluated against an item in the current model.</param>
/// <param name="errorMessage">The error message to add.</param> /// <param name="errorMessage">The error message to add.</param>
public static void AddModelError<TModel>(this ModelStateDictionary modelState, Expression<Func<TModel, object>> expression, string errorMessage) public static void AddModelError<TModel>(
this ModelStateDictionary modelState,
Expression<Func<TModel, object>> expression,
string errorMessage)
{ {
modelState.AddModelError(GetExpressionText(expression), errorMessage); modelState.AddModelError(GetExpressionText(expression), errorMessage);
} }
@ -35,7 +37,10 @@ namespace Microsoft.AspNet.Mvc
/// <param name="modelState">The <see cref="ModelStateDictionary"/> instance this method extends.</param> /// <param name="modelState">The <see cref="ModelStateDictionary"/> instance this method extends.</param>
/// <param name="expression">An expression to be evaluated against an item in the current model.</param> /// <param name="expression">An expression to be evaluated against an item in the current model.</param>
/// <param name="exception">The <see cref="Exception"/> to add.</param> /// <param name="exception">The <see cref="Exception"/> to add.</param>
public static void AddModelError<TModel>(this ModelStateDictionary modelState, Expression<Func<TModel, object>> expression, Exception exception) public static void AddModelError<TModel>(
this ModelStateDictionary modelState,
Expression<Func<TModel, object>> expression,
Exception exception)
{ {
modelState.AddModelError(GetExpressionText(expression), exception); modelState.AddModelError(GetExpressionText(expression), exception);
} }
@ -50,18 +55,23 @@ namespace Microsoft.AspNet.Mvc
/// true if the element is successfully removed; otherwise, false. /// true if the element is successfully removed; otherwise, false.
/// This method also returns false if <paramref name="expression"/> was not found in the model-state dictionary. /// This method also returns false if <paramref name="expression"/> was not found in the model-state dictionary.
/// </returns> /// </returns>
public static bool Remove<TModel>(this ModelStateDictionary modelState, Expression<Func<TModel, object>> expression) public static bool Remove<TModel>(
this ModelStateDictionary modelState,
Expression<Func<TModel, object>> expression)
{ {
return modelState.Remove(GetExpressionText(expression)); return modelState.Remove(GetExpressionText(expression));
} }
/// <summary> /// <summary>
/// Removes all the entries for the specified <paramref name="expression"/> from the <see cref="ModelStateDictionary"/>. /// Removes all the entries for the specified <paramref name="expression"/> from the
/// <see cref="ModelStateDictionary"/>.
/// </summary> /// </summary>
/// <typeparam name="TModel">The type of the model.</typeparam> /// <typeparam name="TModel">The type of the model.</typeparam>
/// <param name="modelState">The <see cref="ModelStateDictionary"/> instance this method extends.</param> /// <param name="modelState">The <see cref="ModelStateDictionary"/> instance this method extends.</param>
/// <param name="expression">An expression to be evaluated against an item in the current model.</param> /// <param name="expression">An expression to be evaluated against an item in the current model.</param>
public static void RemoveAll<TModel>(this ModelStateDictionary modelState, Expression<Func<TModel, object>> expression) public static void RemoveAll<TModel>(
this ModelStateDictionary modelState,
Expression<Func<TModel, object>> expression)
{ {
string modelKey = GetExpressionText(expression); string modelKey = GetExpressionText(expression);
if (string.IsNullOrEmpty(modelKey)) if (string.IsNullOrEmpty(modelKey))
@ -98,7 +108,9 @@ namespace Microsoft.AspNet.Mvc
if (IsConversionToObject(unaryExpression)) if (IsConversionToObject(unaryExpression))
{ {
return ExpressionHelper.GetExpressionText(Expression.Lambda(unaryExpression.Operand, expression.Parameters[0])); return ExpressionHelper.GetExpressionText(Expression.Lambda(
unaryExpression.Operand,
expression.Parameters[0]));
} }
return ExpressionHelper.GetExpressionText(expression); return ExpressionHelper.GetExpressionText(expression);

View File

@ -2,10 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using Microsoft.AspNet.Mvc.ModelBinding;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Mvc namespace Microsoft.AspNet.Mvc.ModelBinding
{ {
public class ModelStateDictionaryExtensionsTest public class ModelStateDictionaryExtensionsTest
{ {