Replacing NotNullAttribute with thrown exceptions

This commit is contained in:
Pranav K 2015-10-01 08:23:21 -07:00
parent 3a876e387f
commit 5a705d820d
56 changed files with 695 additions and 212 deletions

View File

@ -1,7 +1,7 @@
// 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 Microsoft.Framework.Internal;
using System;
namespace Microsoft.AspNet.Mvc.Abstractions
{
@ -17,8 +17,13 @@ namespace Microsoft.AspNet.Mvc.Abstractions
/// <typeparam name="T">The type of the property.</typeparam>
/// <param name="actionDescriptor">The action descriptor.</param>
/// <returns>The property or the default value of <typeparamref name="T"/>.</returns>
public static T GetProperty<T>([NotNull] this ActionDescriptor actionDescriptor)
public static T GetProperty<T>(this ActionDescriptor actionDescriptor)
{
if (actionDescriptor == null)
{
throw new ArgumentNullException(nameof(actionDescriptor));
}
object value;
if (actionDescriptor.Properties.TryGetValue(typeof(T), out value))
{
@ -37,8 +42,18 @@ namespace Microsoft.AspNet.Mvc.Abstractions
/// <typeparam name="T">The type of the property.</typeparam>
/// <param name="actionDescriptor">The action descriptor.</param>
/// <param name="value">The value of the property.</param>
public static void SetProperty<T>([NotNull] this ActionDescriptor actionDescriptor, [NotNull] T value)
public static void SetProperty<T>(this ActionDescriptor actionDescriptor, T value)
{
if (actionDescriptor == null)
{
throw new ArgumentNullException(nameof(actionDescriptor));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
actionDescriptor.Properties[typeof(T)] = value;
}
}

View File

@ -1,14 +1,19 @@
// 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 Microsoft.Framework.Internal;
using System;
namespace Microsoft.AspNet.Mvc.Abstractions
{
public class ActionInvokerProviderContext
{
public ActionInvokerProviderContext([NotNull] ActionContext actionContext)
public ActionInvokerProviderContext(ActionContext actionContext)
{
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
ActionContext = actionContext;
}

View File

@ -1,8 +1,6 @@
// 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 Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Abstractions
{
public interface IActionDescriptorProvider
@ -28,8 +26,8 @@ namespace Microsoft.AspNet.Mvc.Abstractions
/// </remarks>
int Order { get; }
void OnProvidersExecuting([NotNull] ActionDescriptorProviderContext context);
void OnProvidersExecuting(ActionDescriptorProviderContext context);
void OnProvidersExecuted([NotNull] ActionDescriptorProviderContext context);
void OnProvidersExecuted(ActionDescriptorProviderContext context);
}
}

View File

@ -1,8 +1,6 @@
// 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 Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Abstractions
{
public interface IActionInvokerProvider
@ -28,8 +26,8 @@ namespace Microsoft.AspNet.Mvc.Abstractions
/// </remarks>
int Order { get; }
void OnProvidersExecuting([NotNull] ActionInvokerProviderContext context);
void OnProvidersExecuting(ActionInvokerProviderContext context);
void OnProvidersExecuted([NotNull] ActionInvokerProviderContext context);
void OnProvidersExecuted(ActionInvokerProviderContext context);
}
}

View File

@ -1,7 +1,7 @@
// 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 Microsoft.Framework.Internal;
using System;
namespace Microsoft.AspNet.Mvc.ActionConstraints
{
@ -15,8 +15,13 @@ namespace Microsoft.AspNet.Mvc.ActionConstraints
/// Creates a new <see cref="ActionConstraintItem"/>.
/// </summary>
/// <param name="metadata">The <see cref="IActionConstraintMetadata"/> instance.</param>
public ActionConstraintItem([NotNull] IActionConstraintMetadata metadata)
public ActionConstraintItem(IActionConstraintMetadata metadata)
{
if (metadata == null)
{
throw new ArgumentNullException(nameof(metadata));
}
Metadata = metadata;
}

View File

@ -1,10 +1,10 @@
// 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 System.Collections.Generic;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ActionConstraints
{
@ -19,10 +19,25 @@ namespace Microsoft.AspNet.Mvc.ActionConstraints
/// <param name="action">The <see cref="ActionDescriptor"/> for which constraints are being created.</param>
/// <param name="items">The list of <see cref="ActionConstraintItem"/> objects.</param>
public ActionConstraintProviderContext(
[NotNull] HttpContext context,
[NotNull] ActionDescriptor action,
[NotNull] IList<ActionConstraintItem> items)
HttpContext context,
ActionDescriptor action,
IList<ActionConstraintItem> items)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
HttpContext = context;
Action = action;
Results = items;

View File

@ -1,9 +1,9 @@
// 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 System.Collections.Generic;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ActionConstraints
{
@ -19,8 +19,13 @@ namespace Microsoft.AspNet.Mvc.ActionConstraints
/// <param name="constraints">
/// The list of <see cref="IActionConstraint"/> instances associated with <paramref name="action"/>.
/// </param>
public ActionSelectorCandidate([NotNull] ActionDescriptor action, IReadOnlyList<IActionConstraint> constraints)
public ActionSelectorCandidate(ActionDescriptor action, IReadOnlyList<IActionConstraint> constraints)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
Action = action;
Constraints = constraints;
}

View File

@ -1,8 +1,6 @@
// 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 Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ActionConstraints
{
public interface IActionConstraintProvider
@ -28,8 +26,8 @@ namespace Microsoft.AspNet.Mvc.ActionConstraints
/// </remarks>
int Order { get; }
void OnProvidersExecuting([NotNull] ActionConstraintProviderContext context);
void OnProvidersExecuting(ActionConstraintProviderContext context);
void OnProvidersExecuted([NotNull] ActionConstraintProviderContext context);
void OnProvidersExecuted(ActionConstraintProviderContext context);
}
}

View File

@ -1,11 +1,11 @@
// 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.AspNet.Http;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc
{
@ -29,13 +29,17 @@ namespace Microsoft.AspNet.Mvc
/// Creates a new <see cref="ActionContext"/>.
/// </summary>
/// <param name="actionContext">The <see cref="ActionContext"/> to copy.</param>
public ActionContext([NotNull] ActionContext actionContext)
public ActionContext(ActionContext actionContext)
: this(
actionContext.HttpContext,
actionContext.RouteData,
actionContext.ActionDescriptor,
actionContext.HttpContext,
actionContext.RouteData,
actionContext.ActionDescriptor,
actionContext.ModelState)
{
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
}
/// <summary>
@ -45,9 +49,9 @@ namespace Microsoft.AspNet.Mvc
/// <param name="routeData">The <see cref="AspNet.Routing.RouteData"/> for the current request.</param>
/// <param name="actionDescriptor">The <see cref="Abstractions.ActionDescriptor"/> for the selected action.</param>
public ActionContext(
[NotNull] HttpContext httpContext,
[NotNull] RouteData routeData,
[NotNull] ActionDescriptor actionDescriptor)
HttpContext httpContext,
RouteData routeData,
ActionDescriptor actionDescriptor)
: this(httpContext, routeData, actionDescriptor, new ModelStateDictionary())
{
}
@ -60,11 +64,31 @@ namespace Microsoft.AspNet.Mvc
/// <param name="actionDescriptor">The <see cref="Abstractions.ActionDescriptor"/> for the selected action.</param>
/// <param name="modelState">The <see cref="ModelStateDictionary"/>.</param>
public ActionContext(
[NotNull] HttpContext httpContext,
[NotNull] RouteData routeData,
[NotNull] ActionDescriptor actionDescriptor,
[NotNull] ModelStateDictionary modelState)
HttpContext httpContext,
RouteData routeData,
ActionDescriptor actionDescriptor,
ModelStateDictionary modelState)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (routeData == null)
{
throw new ArgumentNullException(nameof(routeData));
}
if (actionDescriptor == null)
{
throw new ArgumentNullException(nameof(actionDescriptor));
}
if (modelState == null)
{
throw new ArgumentNullException(nameof(modelState));
}
HttpContext = httpContext;
RouteData = routeData;
ActionDescriptor = actionDescriptor;

View File

@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
@ -14,8 +13,8 @@ namespace Microsoft.AspNet.Mvc.Filters
private ExceptionDispatchInfo _exceptionDispatchInfo;
public ActionExecutedContext(
[NotNull] ActionContext actionContext,
[NotNull] IList<IFilterMetadata> filters,
ActionContext actionContext,
IList<IFilterMetadata> filters,
object controller)
: base(actionContext, filters)
{

View File

@ -1,20 +1,25 @@
// 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 System.Collections.Generic;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public class ActionExecutingContext : FilterContext
{
public ActionExecutingContext(
[NotNull] ActionContext actionContext,
[NotNull] IList<IFilterMetadata> filters,
[NotNull] IDictionary<string, object> actionArguments,
ActionContext actionContext,
IList<IFilterMetadata> filters,
IDictionary<string, object> actionArguments,
object controller)
: base(actionContext, filters)
{
if (actionArguments == null)
{
throw new ArgumentNullException(nameof(actionArguments));
}
ActionArguments = actionArguments;
Controller = controller;
}

View File

@ -1,16 +1,16 @@
// 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 System.Collections.Generic;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public class AuthorizationContext : FilterContext
{
public AuthorizationContext(
[NotNull] ActionContext actionContext,
[NotNull] IList<IFilterMetadata> filters)
ActionContext actionContext,
IList<IFilterMetadata> filters)
: base(actionContext, filters)
{
}

View File

@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
@ -13,7 +12,7 @@ namespace Microsoft.AspNet.Mvc.Filters
private Exception _exception;
private ExceptionDispatchInfo _exceptionDispatchInfo;
public ExceptionContext([NotNull] ActionContext actionContext, [NotNull] IList<IFilterMetadata> filters)
public ExceptionContext(ActionContext actionContext, IList<IFilterMetadata> filters)
: base(actionContext, filters)
{
}

View File

@ -1,21 +1,26 @@
// 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 System.Collections.Generic;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public abstract class FilterContext : ActionContext
{
public FilterContext(
[NotNull] ActionContext actionContext,
[NotNull] IList<IFilterMetadata> filters)
ActionContext actionContext,
IList<IFilterMetadata> filters)
: base(actionContext)
{
if (filters == null)
{
throw new ArgumentNullException(nameof(filters));
}
Filters = filters;
}
public virtual IList<IFilterMetadata> Filters { get; private set; }
public virtual IList<IFilterMetadata> Filters { get; }
}
}

View File

@ -1,7 +1,7 @@
// 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 Microsoft.Framework.Internal;
using System;
namespace Microsoft.AspNet.Mvc.Filters
{
@ -33,8 +33,13 @@ namespace Microsoft.AspNet.Mvc.Filters
/// <see cref="Order"/> will be taken from <see cref="IOrderedFilter.Order"/>. Otherwise the value
/// of <see cref="Order"/> will default to <c>0</c>.
/// </remarks>
public FilterDescriptor([NotNull] IFilterMetadata filter, int filterScope)
public FilterDescriptor(IFilterMetadata filter, int filterScope)
{
if (filter == null)
{
throw new ArgumentNullException(nameof(filter));
}
Filter = filter;
Scope = filterScope;

View File

@ -1,8 +1,8 @@
// 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 System.Diagnostics;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
@ -10,14 +10,24 @@ namespace Microsoft.AspNet.Mvc.Filters
[DebuggerDisplay("FilterItem: {Filter}")]
public class FilterItem
{
public FilterItem([NotNull] FilterDescriptor descriptor)
public FilterItem(FilterDescriptor descriptor)
{
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
Descriptor = descriptor;
}
public FilterItem([NotNull] FilterDescriptor descriptor, [NotNull] IFilterMetadata filter)
public FilterItem(FilterDescriptor descriptor, IFilterMetadata filter)
: this(descriptor)
{
if (filter == null)
{
throw new ArgumentNullException(nameof(filter));
}
Filter = filter;
}

View File

@ -1,15 +1,25 @@
// 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 System.Collections.Generic;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public class FilterProviderContext
{
public FilterProviderContext([NotNull] ActionContext actionContext, [NotNull] IList<FilterItem> items)
public FilterProviderContext(ActionContext actionContext, IList<FilterItem> items)
{
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
ActionContext = actionContext;
Results = items;
}

View File

@ -1,14 +1,12 @@
// 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 Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public interface IActionFilter : IFilterMetadata
{
void OnActionExecuting([NotNull] ActionExecutingContext context);
void OnActionExecuting(ActionExecutingContext context);
void OnActionExecuted([NotNull] ActionExecutedContext context);
void OnActionExecuted(ActionExecutedContext context);
}
}

View File

@ -2,12 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public interface IAsyncActionFilter : IFilterMetadata
{
Task OnActionExecutionAsync([NotNull] ActionExecutingContext context, [NotNull] ActionExecutionDelegate next);
Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next);
}
}

View File

@ -2,12 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public interface IAsyncAuthorizationFilter : IFilterMetadata
{
Task OnAuthorizationAsync([NotNull] AuthorizationContext context);
Task OnAuthorizationAsync(AuthorizationContext context);
}
}

View File

@ -2,12 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public interface IAsyncExceptionFilter : IFilterMetadata
{
Task OnExceptionAsync([NotNull] ExceptionContext context);
Task OnExceptionAsync(ExceptionContext context);
}
}

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
@ -24,7 +23,7 @@ namespace Microsoft.AspNet.Mvc.Filters
/// A <see cref="Task"/> which will complete when the remainder of the pipeline completes.
/// </returns>
Task OnResourceExecutionAsync(
[NotNull] ResourceExecutingContext context,
[NotNull] ResourceExecutionDelegate next);
ResourceExecutingContext context,
ResourceExecutionDelegate next);
}
}

View File

@ -2,12 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public interface IAsyncResultFilter : IFilterMetadata
{
Task OnResultExecutionAsync([NotNull] ResultExecutingContext context, [NotNull] ResultExecutionDelegate next);
Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next);
}
}

View File

@ -1,12 +1,10 @@
// 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 Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public interface IAuthorizationFilter : IFilterMetadata
{
void OnAuthorization([NotNull] AuthorizationContext context);
void OnAuthorization(AuthorizationContext context);
}
}

View File

@ -1,12 +1,10 @@
// 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 Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public interface IExceptionFilter : IFilterMetadata
{
void OnException([NotNull] ExceptionContext context);
void OnException(ExceptionContext context);
}
}

View File

@ -2,12 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public interface IFilterFactory : IFilterMetadata
{
IFilterMetadata CreateInstance([NotNull] IServiceProvider serviceProvider);
IFilterMetadata CreateInstance(IServiceProvider serviceProvider);
}
}

View File

@ -1,8 +1,6 @@
// 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 Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public interface IFilterProvider
@ -28,8 +26,8 @@ namespace Microsoft.AspNet.Mvc.Filters
/// </remarks>
int Order { get; }
void OnProvidersExecuting([NotNull] FilterProviderContext context);
void OnProvidersExecuting(FilterProviderContext context);
void OnProvidersExecuted([NotNull] FilterProviderContext context);
void OnProvidersExecuted(FilterProviderContext context);
}
}

View File

@ -1,8 +1,6 @@
// 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 Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
/// <summary>
@ -15,12 +13,12 @@ namespace Microsoft.AspNet.Mvc.Filters
/// Executes the resource filter. Called before execution of the remainder of the pipeline.
/// </summary>
/// <param name="context">The <see cref="ResourceExecutingContext"/>.</param>
void OnResourceExecuting([NotNull] ResourceExecutingContext context);
void OnResourceExecuting(ResourceExecutingContext context);
/// <summary>
/// Executes the resource filter. Called after execution of the remainder of the pipeline.
/// </summary>
/// <param name="context">The <see cref="ResourceExecutedContext"/>.</param>
void OnResourceExecuted([NotNull] ResourceExecutedContext context);
void OnResourceExecuted(ResourceExecutedContext context);
}
}

View File

@ -1,14 +1,12 @@
// 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 Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public interface IResultFilter : IFilterMetadata
{
void OnResultExecuting([NotNull] ResultExecutingContext context);
void OnResultExecuting(ResultExecutingContext context);
void OnResultExecuted([NotNull] ResultExecutedContext context);
void OnResultExecuted(ResultExecutedContext context);
}
}

View File

@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
@ -14,12 +13,17 @@ namespace Microsoft.AspNet.Mvc.Filters
private ExceptionDispatchInfo _exceptionDispatchInfo;
public ResultExecutedContext(
[NotNull] ActionContext actionContext,
[NotNull] IList<IFilterMetadata> filters,
[NotNull] IActionResult result,
ActionContext actionContext,
IList<IFilterMetadata> filters,
IActionResult result,
object controller)
: base(actionContext, filters)
{
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
Result = result;
Controller = controller;
}

View File

@ -2,16 +2,15 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Filters
{
public class ResultExecutingContext : FilterContext
{
public ResultExecutingContext(
[NotNull] ActionContext actionContext,
[NotNull] IList<IFilterMetadata> filters,
[NotNull] IActionResult result,
ActionContext actionContext,
IList<IFilterMetadata> filters,
IActionResult result,
object controller)
: base(actionContext, filters)
{

View File

@ -4,7 +4,6 @@
using System;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Formatters
{
@ -27,11 +26,31 @@ namespace Microsoft.AspNet.Mvc.Formatters
/// The <see cref="Type"/> of the model to deserialize.
/// </param>
public InputFormatterContext(
[NotNull] HttpContext httpContext,
[NotNull] string modelName,
[NotNull] ModelStateDictionary modelState,
[NotNull] Type modelType)
HttpContext httpContext,
string modelName,
ModelStateDictionary modelState,
Type modelType)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (modelName == null)
{
throw new ArgumentNullException(nameof(modelName));
}
if (modelState == null)
{
throw new ArgumentNullException(nameof(modelState));
}
if (modelType == null)
{
throw new ArgumentNullException(nameof(modelType));
}
HttpContext = httpContext;
ModelName = modelName;
ModelState = modelState;

View File

@ -4,7 +4,6 @@
using System;
using System.Diagnostics;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
@ -96,8 +95,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <param name="isFromRequest">
/// A value indicating whether or not the data comes from the HTTP request.
/// </param>
public BindingSource([NotNull] string id, string displayName, bool isGreedy, bool isFromRequest)
public BindingSource(string id, string displayName, bool isGreedy, bool isFromRequest)
{
if (id == null)
{
throw new ArgumentNullException(nameof(id));
}
Id = id;
DisplayName = displayName;
IsGreedy = isGreedy;
@ -158,8 +162,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// This distinction is important as the left-hand-side may be a composite, but the right
/// may not.
/// </remarks>
public virtual bool CanAcceptDataFrom([NotNull] BindingSource bindingSource)
public virtual bool CanAcceptDataFrom(BindingSource bindingSource)
{
if (bindingSource == null)
{
throw new ArgumentNullException(nameof(bindingSource));
}
if (bindingSource is CompositeBindingSource)
{
var message = Resources.FormatBindingSource_CannotBeComposite(

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
@ -24,9 +23,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <param name="displayName">The display name for the composite source.</param>
/// <returns>A <see cref="CompositeBindingSource"/>.</returns>
public static CompositeBindingSource Create(
[NotNull] IEnumerable<BindingSource> bindingSources,
IEnumerable<BindingSource> bindingSources,
string displayName)
{
if (bindingSources == null)
{
throw new ArgumentNullException(nameof(bindingSources));
}
foreach (var bindingSource in bindingSources)
{
if (bindingSource.IsGreedy)
@ -59,11 +63,21 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
private CompositeBindingSource(
[NotNull] string id,
string displayName,
[NotNull] IEnumerable<BindingSource> bindingSources)
string id,
string displayName,
IEnumerable<BindingSource> bindingSources)
: base(id, displayName, isGreedy: false, isFromRequest: true)
{
if (id == null)
{
throw new ArgumentNullException(nameof(id));
}
if (bindingSources == null)
{
throw new ArgumentNullException(nameof(bindingSources));
}
BindingSources = bindingSources;
}
@ -73,8 +87,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public IEnumerable<BindingSource> BindingSources { get; }
/// <inheritdoc />
public override bool CanAcceptDataFrom([NotNull] BindingSource bindingSource)
public override bool CanAcceptDataFrom(BindingSource bindingSource)
{
if (bindingSource == null)
{
throw new ArgumentNullException(nameof(bindingSource));
}
if (bindingSource is CompositeBindingSource)
{
var message = Resources.FormatBindingSource_CannotBeComposite(

View File

@ -3,15 +3,13 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public interface IModelMetadataProvider
{
ModelMetadata GetMetadataForType([NotNull] Type modelType);
ModelMetadata GetMetadataForType(Type modelType);
IEnumerable<ModelMetadata> GetMetadataForProperties([NotNull] Type modelType);
IEnumerable<ModelMetadata> GetMetadataForProperties(Type modelType);
}
}

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
@ -15,6 +14,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <returns>
/// A <see cref="Task"/> that when completed will yield a <see cref="IValueProvider"/> instance or <c>null</c>.
/// </returns>
Task<IValueProvider> GetValueProviderAsync([NotNull] ValueProviderFactoryContext context);
Task<IValueProvider> GetValueProviderAsync(ValueProviderFactoryContext context);
}
}

View File

@ -3,7 +3,6 @@
using System;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
{
@ -17,8 +16,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
/// </summary>
/// <param name="modelType">The model <see cref="Type"/>.</param>
/// <returns>A <see cref="ModelMetadataIdentity"/>.</returns>
public static ModelMetadataIdentity ForType([NotNull] Type modelType)
public static ModelMetadataIdentity ForType(Type modelType)
{
if (modelType == null)
{
throw new ArgumentNullException(nameof(modelType));
}
return new ModelMetadataIdentity()
{
ModelType = modelType,
@ -33,10 +37,20 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
/// <param name="containerType">The container type of the model property.</param>
/// <returns>A <see cref="ModelMetadataIdentity"/>.</returns>
public static ModelMetadataIdentity ForProperty(
[NotNull] Type modelType,
Type modelType,
string name,
[NotNull] Type containerType)
Type containerType)
{
if (modelType == null)
{
throw new ArgumentNullException(nameof(modelType));
}
if (containerType == null)
{
throw new ArgumentNullException(nameof(containerType));
}
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(name));

View File

@ -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 Microsoft.Framework.Internal;
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
namespace Microsoft.AspNet.Mvc.ModelBinding
@ -12,6 +11,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </summary>
public class ModelBindingContext
{
private string _fieldName;
private ModelMetadata _modelMetadata;
private string _modelName;
private ModelStateDictionary _modelState;
private OperationBindingContext _operationBindingContext;
private IValueProvider _valueProvider;
/// <summary>
/// Initializes a new instance of the <see cref="ModelBindingContext"/> class.
/// </summary>
@ -30,12 +36,32 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <param name="modelName">The name of the property or parameter being bound.</param>
/// <returns>A new instance of <see cref="ModelBindingContext"/>.</returns>
public static ModelBindingContext CreateBindingContext(
[NotNull] OperationBindingContext operationBindingContext,
[NotNull] ModelStateDictionary modelState,
[NotNull] ModelMetadata metadata,
OperationBindingContext operationBindingContext,
ModelStateDictionary modelState,
ModelMetadata metadata,
BindingInfo bindingInfo,
[NotNull] string modelName)
string modelName)
{
if (operationBindingContext == null)
{
throw new ArgumentNullException(nameof(operationBindingContext));
}
if (modelState == null)
{
throw new ArgumentNullException(nameof(modelState));
}
if (metadata == null)
{
throw new ArgumentNullException(nameof(metadata));
}
if (modelName == null)
{
throw new ArgumentNullException(nameof(modelName));
}
var binderModelName = bindingInfo?.BinderModelName ?? metadata.BinderModelName;
var propertyPredicateProvider =
bindingInfo?.PropertyBindingPredicateProvider ?? metadata.PropertyBindingPredicateProvider;
@ -66,12 +92,32 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
public static ModelBindingContext CreateChildBindingContext(
[NotNull] ModelBindingContext parent,
[NotNull] ModelMetadata modelMetadata,
[NotNull] string fieldName,
[NotNull] string modelName,
ModelBindingContext parent,
ModelMetadata modelMetadata,
string fieldName,
string modelName,
object model)
{
if (parent == null)
{
throw new ArgumentNullException(nameof(parent));
}
if (modelMetadata == null)
{
throw new ArgumentNullException(nameof(modelMetadata));
}
if (fieldName == null)
{
throw new ArgumentNullException(nameof(fieldName));
}
if (modelName == null)
{
throw new ArgumentNullException(nameof(modelName));
}
return new ModelBindingContext()
{
ModelState = parent.ModelState,
@ -93,12 +139,36 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <summary>
/// Represents the <see cref="OperationBindingContext"/> associated with this context.
/// </summary>
public OperationBindingContext OperationBindingContext { get; [param:NotNull] set; }
public OperationBindingContext OperationBindingContext
{
get { return _operationBindingContext; }
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_operationBindingContext = value;
}
}
/// <summary>
/// Gets or sets the name of the current field being bound.
/// </summary>
public string FieldName { get; [param: NotNull] set; }
public string FieldName
{
get { return _fieldName; }
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_fieldName = value;
}
}
/// <summary>
/// Gets or sets the model value for the current operation.
@ -112,19 +182,55 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <summary>
/// Gets or sets the metadata for the model associated with this context.
/// </summary>
public ModelMetadata ModelMetadata { get; [param: NotNull] set; }
public ModelMetadata ModelMetadata
{
get { return _modelMetadata; }
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_modelMetadata = value;
}
}
/// <summary>
/// Gets or sets the name of the model. This property is used as a key for looking up values in
/// <see cref="IValueProvider"/> during model binding.
/// </summary>
public string ModelName { get; [param: NotNull] set; }
public string ModelName
{
get { return _modelName; }
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_modelName = value;
}
}
/// <summary>
/// Gets or sets the <see cref="ModelStateDictionary"/> used to capture <see cref="ModelState"/> values
/// for properties in the object graph of the model when binding.
/// </summary>
public ModelStateDictionary ModelState { get; [param: NotNull] set; }
public ModelStateDictionary ModelState
{
get { return _modelState; }
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_modelState = value;
}
}
/// <summary>
/// Gets the type of the model.
@ -171,7 +277,19 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <summary>
/// Gets or sets the <see cref="IValueProvider"/> associated with this context.
/// </summary>
public IValueProvider ValueProvider { get; [param: NotNull] set; }
public IValueProvider ValueProvider
{
get { return _valueProvider; }
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_valueProvider = value;
}
}
/// <summary>
/// Gets or sets a predicate which will be evaluated for each property to determine if the property

View File

@ -29,8 +29,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </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([NotNull] string key)
public static ModelBindingResult Failed(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return new ModelBindingResult(key, model: null, isModelSet: false);
}
@ -39,8 +44,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </summary>
/// <param name="key">The key of the current model binding operation.</param>
/// <returns>A completed <see cref="Task{ModelBindingResult}"/> representing a failed model binding operation.</returns>
public static Task<ModelBindingResult> FailedAsync([NotNull] string key)
public static Task<ModelBindingResult> FailedAsync(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return Task.FromResult(Failed(key));
}
@ -51,9 +61,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <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(
[NotNull] string key,
string key,
object model)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return new ModelBindingResult(key, model, isModelSet: true);
}
@ -65,9 +80,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <param name="model">The model value. May be <c>null.</c></param>
/// <returns>A completed <see cref="Task{ModelBindingResult}"/> representing a successful model bind.</returns>
public static Task<ModelBindingResult> SuccessAsync(
[NotNull] string key,
string key,
object model)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return Task.FromResult(Success(key, model));
}

View File

@ -2,20 +2,28 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class ModelError
{
public ModelError([NotNull]Exception exception)
public ModelError(Exception exception)
: this(exception, errorMessage: null)
{
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
}
public ModelError([NotNull]Exception exception, string errorMessage)
public ModelError(Exception exception, string errorMessage)
: this(errorMessage)
{
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
Exception = exception;
}

View File

@ -3,19 +3,28 @@
using System;
using System.Collections.ObjectModel;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class ModelErrorCollection : Collection<ModelError>
{
public void Add([NotNull]Exception exception)
public void Add(Exception exception)
{
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
Add(new ModelError(exception));
}
public void Add([NotNull]string errorMessage)
public void Add(string errorMessage)
{
if (errorMessage == null)
{
throw new ArgumentNullException(nameof(errorMessage));
}
Add(new ModelError(errorMessage));
}
}

View File

@ -29,7 +29,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// Creates a new <see cref="ModelMetadata"/>.
/// </summary>
/// <param name="identity">The <see cref="ModelMetadataIdentity"/>.</param>
protected ModelMetadata([NotNull] ModelMetadataIdentity identity)
protected ModelMetadata(ModelMetadataIdentity identity)
{
Identity = identity;
}

View File

@ -4,7 +4,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
@ -19,8 +18,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// Creates a new <see cref="ModelPropertyCollection"/>.
/// </summary>
/// <param name="properties">The properties.</param>
public ModelPropertyCollection([NotNull] IEnumerable<ModelMetadata> properties)
public ModelPropertyCollection(IEnumerable<ModelMetadata> properties)
{
if (properties == null)
{
throw new ArgumentNullException(nameof(properties));
}
_properties = new List<ModelMetadata>(properties);
}
@ -43,11 +47,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// The <see cref="ModelMetadata"/> instance for the property specified by <paramref name="propertyName"/>, or null
/// if no match can be found.
/// </returns>
public ModelMetadata this[[NotNull] string propertyName]
public ModelMetadata this[string propertyName]
{
get
{
if (propertyName == null)
{
throw new ArgumentNullException(nameof(propertyName));
}
foreach (var property in _properties)
{
if (string.Equals(property.PropertyName, propertyName, StringComparison.Ordinal))

View File

@ -4,7 +4,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.Framework.Internal;
@ -49,10 +48,16 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// from the specified <paramref name="dictionary"/>.
/// </summary>
/// <param name="dictionary">The <see cref="ModelStateDictionary"/> to copy values from.</param>
public ModelStateDictionary([NotNull] ModelStateDictionary dictionary)
public ModelStateDictionary(ModelStateDictionary dictionary)
{
_innerDictionary = new CopyOnWriteDictionary<string, ModelState>(dictionary,
StringComparer.OrdinalIgnoreCase);
if (dictionary == null)
{
throw new ArgumentNullException(nameof(dictionary));
}
_innerDictionary = new CopyOnWriteDictionary<string, ModelState>(
dictionary,
StringComparer.OrdinalIgnoreCase);
MaxAllowedErrors = dictionary.MaxAllowedErrors;
ErrorCount = dictionary.ErrorCount;
@ -152,16 +157,26 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <inheritdoc />
public ModelState this[[NotNull] string key]
public ModelState this[string key]
{
get
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ModelState value;
_innerDictionary.TryGetValue(key, out value);
return value;
}
set
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
@ -185,8 +200,18 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </summary>
/// <param name="key">The key of the <see cref="ModelState"/> to add errors to.</param>
/// <param name="exception">The <see cref="Exception"/> to add.</param>
public void AddModelError([NotNull] string key, [NotNull] Exception exception)
public void AddModelError(string key, Exception exception)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
TryAddModelError(key, exception);
}
@ -201,8 +226,18 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <c>True</c> if the given error was added, <c>false</c> if the error was ignored.
/// See <see cref="MaxAllowedErrors"/>.
/// </returns>
public bool TryAddModelError([NotNull] string key, [NotNull] Exception exception)
public bool TryAddModelError(string key, Exception exception)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
if (ErrorCount >= MaxAllowedErrors - 1)
{
EnsureMaxErrorsReachedRecorded();
@ -241,8 +276,18 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </summary>
/// <param name="key">The key of the <see cref="ModelState"/> to add errors to.</param>
/// <param name="errorMessage">The error message to add.</param>
public void AddModelError([NotNull] string key, [NotNull] string errorMessage)
public void AddModelError(string key, string errorMessage)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (errorMessage == null)
{
throw new ArgumentNullException(nameof(errorMessage));
}
TryAddModelError(key, errorMessage);
}
@ -257,8 +302,18 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <c>True</c> if the given error was added, <c>false</c> if the error was ignored.
/// See <see cref="MaxAllowedErrors"/>.
/// </returns>
public bool TryAddModelError([NotNull] string key, [NotNull] string errorMessage)
public bool TryAddModelError(string key, string errorMessage)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (errorMessage == null)
{
throw new ArgumentNullException(nameof(errorMessage));
}
if (ErrorCount >= MaxAllowedErrors - 1)
{
EnsureMaxErrorsReachedRecorded();
@ -281,8 +336,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <returns>Returns <see cref="ModelValidationState.Unvalidated"/> if no entries are found for the specified
/// key, <see cref="ModelValidationState.Invalid"/> if at least one instance is found with one or more model
/// state errors; <see cref="ModelValidationState.Valid"/> otherwise.</returns>
public ModelValidationState GetFieldValidationState([NotNull] string key)
public ModelValidationState GetFieldValidationState(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
var entries = FindKeysWithPrefix(key);
if (!entries.Any())
{
@ -299,8 +359,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <returns>Returns <see cref="ModelValidationState.Unvalidated"/> if no entry is found for the specified
/// key, <see cref="ModelValidationState.Invalid"/> if an instance is found with one or more model
/// state errors; <see cref="ModelValidationState.Valid"/> otherwise.</returns>
public ModelValidationState GetValidationState([NotNull] string key)
public ModelValidationState GetValidationState(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ModelState validationState;
if (TryGetValue(key, out validationState))
{
@ -315,8 +380,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// as <see cref="ModelValidationState.Valid"/>.
/// </summary>
/// <param name="key">The key of the <see cref="ModelState"/> to mark as valid.</param>
public void MarkFieldValid([NotNull] string key)
public void MarkFieldValid(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
var modelState = GetModelStateForKey(key);
if (modelState.ValidationState == ModelValidationState.Invalid)
{
@ -331,8 +401,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// as <see cref="ModelValidationState.Skipped"/>.
/// </summary>
/// <param name="key">The key of the <see cref="ModelState"/> to mark as skipped.</param>
public void MarkFieldSkipped([NotNull] string key)
public void MarkFieldSkipped(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
var modelState = GetModelStateForKey(key);
if (modelState.ValidationState == ModelValidationState.Invalid)
{
@ -369,8 +444,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <param name="attemptedValue">
/// The values of <param name="rawValue"/> in a comma-separated <see cref="string"/>.
/// </param>
public void SetModelValue([NotNull] string key, object rawValue, string attemptedValue)
public void SetModelValue(string key, object rawValue, string attemptedValue)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
var modelState = GetModelStateForKey(key);
modelState.RawValue = rawValue;
modelState.AttemptedValue = attemptedValue;
@ -383,8 +463,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <param name="valueProviderResult">
/// A <see cref="ValueProviderResult"/> with data for the <see cref="ModelState"/> entry.
/// </param>
public void SetModelValue([NotNull] string key, ValueProviderResult valueProviderResult)
public void SetModelValue(string key, ValueProviderResult valueProviderResult)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
// Avoid creating a new array for rawValue if there's only one value.
object rawValue;
if (valueProviderResult == ValueProviderResult.None)
@ -411,7 +496,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
// If key is null or empty, clear all entries in the dictionary
// else just clear the ones that have key as prefix
var entries = (string.IsNullOrEmpty(key)) ?
var entries = (string.IsNullOrEmpty(key)) ?
_innerDictionary : FindKeysWithPrefix(key);
foreach (var entry in entries)
@ -421,8 +506,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
}
private ModelState GetModelStateForKey([NotNull] string key)
private ModelState GetModelStateForKey(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ModelState modelState;
if (!TryGetValue(key, out modelState))
{
@ -477,8 +567,18 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <inheritdoc />
public void Add([NotNull] string key, [NotNull] ModelState value)
public void Add(string key, ModelState value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_innerDictionary.Add(key, value);
}
@ -495,14 +595,24 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <inheritdoc />
public bool ContainsKey([NotNull] string key)
public bool ContainsKey(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _innerDictionary.ContainsKey(key);
}
/// <inheritdoc />
public void CopyTo([NotNull] KeyValuePair<string, ModelState>[] array, int arrayIndex)
public void CopyTo(KeyValuePair<string, ModelState>[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
_innerDictionary.CopyTo(array, arrayIndex);
}
@ -513,14 +623,24 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <inheritdoc />
public bool Remove([NotNull] string key)
public bool Remove(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _innerDictionary.Remove(key);
}
/// <inheritdoc />
public bool TryGetValue([NotNull] string key, out ModelState value)
public bool TryGetValue(string key, out ModelState value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _innerDictionary.TryGetValue(key, out value);
}
@ -536,8 +656,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
return GetEnumerator();
}
public IEnumerable<KeyValuePair<string, ModelState>> FindKeysWithPrefix([NotNull] string prefix)
public IEnumerable<KeyValuePair<string, ModelState>> FindKeysWithPrefix(string prefix)
{
if (prefix == null)
{
throw new ArgumentNullException(nameof(prefix));
}
ModelState exactMatchValue;
if (_innerDictionary.TryGetValue(prefix, out exactMatchValue))
{

View File

@ -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 Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
@ -16,9 +15,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// exception <paramref name="message"/>.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public TooManyModelErrorsException([NotNull] string message)
public TooManyModelErrorsException(string message)
: base(message)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
}
}
}

View File

@ -2,17 +2,31 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
{
public class ClientModelValidationContext
{
public ClientModelValidationContext(
[NotNull] ModelMetadata metadata,
[NotNull] IModelMetadataProvider metadataProvider,
[NotNull] IServiceProvider requestServices)
ModelMetadata metadata,
IModelMetadataProvider metadataProvider,
IServiceProvider requestServices)
{
if (metadata == null)
{
throw new ArgumentNullException(nameof(metadata));
}
if (metadataProvider == null)
{
throw new ArgumentNullException(nameof(metadataProvider));
}
if (requestServices == null)
{
throw new ArgumentNullException(nameof(requestServices));
}
ModelMetadata = metadata;
MetadataProvider = metadataProvider;
RequestServices = requestServices;

View File

@ -2,12 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
{
public interface IClientModelValidator
{
IEnumerable<ModelClientValidationRule> GetClientValidationRules([NotNull] ClientModelValidationContext context);
IEnumerable<ModelClientValidationRule> GetClientValidationRules(ClientModelValidationContext context);
}
}

View File

@ -1,8 +1,6 @@
// 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 Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
{
/// <summary>
@ -15,6 +13,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
/// by updating <see cref="ClientValidatorProviderContext.Validators"/>.
/// </summary>
/// <param name="context">The <see cref="ClientModelValidationContext"/> associated with this call.</param>
void GetValidators([NotNull] ClientValidatorProviderContext context);
void GetValidators(ClientValidatorProviderContext context);
}
}

View File

@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
{
@ -12,15 +11,29 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
private readonly Dictionary<string, object> _validationParameters =
new Dictionary<string, object>(StringComparer.Ordinal);
public ModelClientValidationRule([NotNull] string errorMessage)
public ModelClientValidationRule(string errorMessage)
: this(validationType: string.Empty, errorMessage: errorMessage)
{
if (errorMessage == null)
{
throw new ArgumentNullException(nameof(errorMessage));
}
}
public ModelClientValidationRule(
[NotNull] string validationType,
[NotNull] string errorMessage)
string validationType,
string errorMessage)
{
if (validationType == null)
{
throw new ArgumentNullException(nameof(validationType));
}
if (errorMessage == null)
{
throw new ArgumentNullException(nameof(errorMessage));
}
ValidationType = validationType;
ErrorMessage = errorMessage;
}

View File

@ -1,18 +1,28 @@
// 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 System.Collections.Generic;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class ValueProviderFactoryContext
{
public ValueProviderFactoryContext(
[NotNull] HttpContext httpContext,
[NotNull] IDictionary<string, object> routeValues)
HttpContext httpContext,
IDictionary<string, object> routeValues)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (routeValues == null)
{
throw new ArgumentNullException(nameof(routeValues));
}
HttpContext = httpContext;
RouteValues = routeValues;
}

View File

@ -22,10 +22,6 @@
"version": "1.0.0-*",
"type": "build"
},
"Microsoft.Framework.NotNullAttribute.Sources": {
"version": "1.0.0-*",
"type": "build"
},
"Microsoft.Framework.PropertyHelper.Sources": {
"version": "1.0.0-*",
"type": "build"

View File

@ -4,19 +4,28 @@
using System;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection
{
public static class MvcServiceCollectionExtensions
{
public static IMvcBuilder AddMvc([NotNull] this IServiceCollection services)
public static IMvcBuilder AddMvc(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
return AddMvc(services, setupAction: null);
}
public static IMvcBuilder AddMvc([NotNull] this IServiceCollection services, Action<MvcOptions> setupAction)
public static IMvcBuilder AddMvc(this IServiceCollection services, Action<MvcOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
var builder = services.AddMvcCore();
builder.AddApiExplorer();

View File

@ -210,6 +210,7 @@ namespace Microsoft.AspNet.Mvc.Controllers
var actionBindingContext = new ActionBindingContext()
{
ModelBinder = binder.Object,
ValueProvider = new SimpleValueProvider(),
};
var mockValidator = new Mock<IObjectModelValidator>(MockBehavior.Strict);
@ -305,6 +306,7 @@ namespace Microsoft.AspNet.Mvc.Controllers
var actionBindingContext = new ActionBindingContext()
{
ModelBinder = binder.Object,
ValueProvider = new SimpleValueProvider(),
};
var mockValidator = new Mock<IObjectModelValidator>(MockBehavior.Strict);
@ -413,6 +415,7 @@ namespace Microsoft.AspNet.Mvc.Controllers
var actionBindingContext = new ActionBindingContext()
{
ModelBinder = binder.Object,
ValueProvider = new SimpleValueProvider(),
};
var argumentBinder = GetArgumentBinder();
@ -451,6 +454,7 @@ namespace Microsoft.AspNet.Mvc.Controllers
var actionBindingContext = new ActionBindingContext()
{
ModelBinder = binder.Object,
ValueProvider = new SimpleValueProvider(),
};
var argumentBinder = GetArgumentBinder();
@ -600,6 +604,7 @@ namespace Microsoft.AspNet.Mvc.Controllers
var actionBindingContext = new ActionBindingContext
{
ModelBinder = binder.Object,
ValueProvider = new SimpleValueProvider(),
};
// Act

View File

@ -379,6 +379,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
ModelState = new ModelStateDictionary(),
OperationBindingContext = new OperationBindingContext(),
ValueProvider = new SimpleValueProvider(),
FieldName = "test-field",
};
// Act
@ -528,6 +529,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
IsTopLevelObject = true,
ModelMetadata = metadataProvider.GetMetadataForType(type),
ModelName = "parameter",
FieldName = "parameter",
ModelState = new ModelStateDictionary(),
ValueProvider = valueProvider,
OperationBindingContext = new OperationBindingContext

View File

@ -278,7 +278,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
MetadataProvider = TestModelMetadataProvider.CreateDefaultProvider(),
},
// Setting it to empty ensures that model does not get created becasue of no model name.
ModelName = "dummyName"
ModelName = "dummyName",
ModelState = new ModelStateDictionary(),
}
};
@ -339,7 +340,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Setting it to empty ensures that model does not get created becasue of no model name.
ModelName = "dummyName",
BindingSource = modelMetadata.BindingSource,
BinderModelName = modelMetadata.BinderModelName
BinderModelName = modelMetadata.BinderModelName,
ModelState = new ModelStateDictionary(),
}
};
@ -384,7 +386,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
MetadataProvider = TestModelMetadataProvider.CreateDefaultProvider(),
},
// Setting it to empty ensures that model does not get created becasue of no model name.
ModelName = "dummyName"
ModelName = "dummyName",
ModelState = new ModelStateDictionary(),
}
};
@ -424,7 +427,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
ModelBinder = mockBinder.Object,
MetadataProvider = TestModelMetadataProvider.CreateDefaultProvider(),
ValidatorProvider = Mock.Of<IModelValidatorProvider>()
}
},
ModelState = new ModelStateDictionary(),
};
var model = new Person();
@ -475,7 +479,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
ModelBinder = mockBinder.Object,
MetadataProvider = TestModelMetadataProvider.CreateDefaultProvider(),
ValidatorProvider = Mock.Of<IModelValidatorProvider>()
}
},
ModelState = new ModelStateDictionary(),
};
var model = new Person();

View File

@ -293,7 +293,8 @@ namespace Microsoft.AspNet.Mvc.IntegrationTests
{
BinderModelName = "CustomParameter",
},
ParameterType = typeof(Person2)
ParameterType = typeof(Person2),
Name = "param-name",
};
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(
@ -352,7 +353,8 @@ namespace Microsoft.AspNet.Mvc.IntegrationTests
{
BinderModelName = "CustomParameter",
},
ParameterType = typeof(Person3)
ParameterType = typeof(Person3),
Name = "param-name",
};
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(