* Simplify MvcOptions
* Remove facades for accessing Options<T> and pass options to the invoker Fixes #2266 Fixes #2269
This commit is contained in:
parent
689f5bfa79
commit
f9d53e341c
|
|
@ -7,6 +7,7 @@ using System.Linq;
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
|
|
@ -115,7 +116,8 @@ namespace Microsoft.AspNet.Mvc
|
|||
.ActionContext
|
||||
.HttpContext
|
||||
.RequestServices
|
||||
.GetRequiredService<IOutputFormattersProvider>()
|
||||
.GetRequiredService<IOptions<MvcOptions>>()
|
||||
.Options
|
||||
.OutputFormatters
|
||||
.OfType<IJsonOutputFormatter>()
|
||||
.ToArray();
|
||||
|
|
|
|||
|
|
@ -273,10 +273,12 @@ namespace Microsoft.AspNet.Mvc
|
|||
IEnumerable<IOutputFormatter> formatters = null;
|
||||
if (Formatters == null || Formatters.Count == 0)
|
||||
{
|
||||
formatters = context.HttpContext
|
||||
.RequestServices
|
||||
.GetRequiredService<IOutputFormattersProvider>()
|
||||
.OutputFormatters;
|
||||
formatters = context
|
||||
.HttpContext
|
||||
.RequestServices
|
||||
.GetRequiredService<IOptions<MvcOptions>>()
|
||||
.Options
|
||||
.OutputFormatters;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -23,20 +23,20 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
[NotNull] IReadOnlyList<IFilterProvider> filterProviders,
|
||||
[NotNull] IControllerFactory controllerFactory,
|
||||
[NotNull] ControllerActionDescriptor descriptor,
|
||||
[NotNull] IInputFormattersProvider inputFormatterProvider,
|
||||
[NotNull] IReadOnlyList<IInputFormatter> inputFormatters,
|
||||
[NotNull] IControllerActionArgumentBinder controllerActionArgumentBinder,
|
||||
[NotNull] IModelBinderProvider modelBinderProvider,
|
||||
[NotNull] IModelValidatorProviderProvider modelValidatorProviderProvider,
|
||||
[NotNull] IValueProviderFactoryProvider valueProviderFactoryProvider,
|
||||
[NotNull] IReadOnlyList<IModelBinder> modelBinders,
|
||||
[NotNull] IReadOnlyList<IModelValidatorProvider> modelValidatorProviders,
|
||||
[NotNull] IReadOnlyList<IValueProviderFactory> valueProviderFactories,
|
||||
[NotNull] IScopedInstance<ActionBindingContext> actionBindingContextAccessor,
|
||||
[NotNull] ITempDataDictionary tempData)
|
||||
: base(
|
||||
actionContext,
|
||||
filterProviders,
|
||||
inputFormatterProvider,
|
||||
modelBinderProvider,
|
||||
modelValidatorProviderProvider,
|
||||
valueProviderFactoryProvider,
|
||||
inputFormatters,
|
||||
modelBinders,
|
||||
modelValidatorProviders,
|
||||
valueProviderFactories,
|
||||
actionBindingContextAccessor)
|
||||
{
|
||||
_descriptor = descriptor;
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ using System.Linq;
|
|||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Core
|
||||
{
|
||||
|
|
@ -14,31 +15,28 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
private readonly IControllerActionArgumentBinder _argumentBinder;
|
||||
private readonly IControllerFactory _controllerFactory;
|
||||
private readonly IFilterProvider[] _filterProviders;
|
||||
private readonly IInputFormattersProvider _inputFormattersProvider;
|
||||
private readonly IModelBinderProvider _modelBinderProvider;
|
||||
private readonly IModelValidatorProviderProvider _modelValidationProviderProvider;
|
||||
private readonly IValueProviderFactoryProvider _valueProviderFactoryProvider;
|
||||
private readonly IReadOnlyList<IInputFormatter> _inputFormatters;
|
||||
private readonly IReadOnlyList<IModelBinder> _modelBinders;
|
||||
private readonly IReadOnlyList<IModelValidatorProvider> _modelValidatorProviders;
|
||||
private readonly IReadOnlyList<IValueProviderFactory> _valueProviderFactories;
|
||||
private readonly IScopedInstance<ActionBindingContext> _actionBindingContextAccessor;
|
||||
private readonly ITempDataDictionary _tempData;
|
||||
|
||||
public ControllerActionInvokerProvider(
|
||||
IControllerFactory controllerFactory,
|
||||
IInputFormattersProvider inputFormattersProvider,
|
||||
IEnumerable<IFilterProvider> filterProviders,
|
||||
IControllerActionArgumentBinder argumentBinder,
|
||||
IModelBinderProvider modelBinderProvider,
|
||||
IModelValidatorProviderProvider modelValidationProviderProvider,
|
||||
IValueProviderFactoryProvider valueProviderFactoryProvider,
|
||||
IOptions<MvcOptions> optionsAccessor,
|
||||
IScopedInstance<ActionBindingContext> actionBindingContextAccessor,
|
||||
ITempDataDictionary tempData)
|
||||
{
|
||||
_controllerFactory = controllerFactory;
|
||||
_inputFormattersProvider = inputFormattersProvider;
|
||||
_filterProviders = filterProviders.OrderBy(item => item.Order).ToArray();
|
||||
_argumentBinder = argumentBinder;
|
||||
_modelBinderProvider = modelBinderProvider;
|
||||
_modelValidationProviderProvider = modelValidationProviderProvider;
|
||||
_valueProviderFactoryProvider = valueProviderFactoryProvider;
|
||||
_inputFormatters = optionsAccessor.Options.InputFormatters.ToArray();
|
||||
_modelBinders = optionsAccessor.Options.ModelBinders.ToArray();
|
||||
_modelValidatorProviders = optionsAccessor.Options.ModelValidatorProviders.ToArray();
|
||||
_valueProviderFactories = optionsAccessor.Options.ValueProviderFactories.ToArray();
|
||||
_actionBindingContextAccessor = actionBindingContextAccessor;
|
||||
_tempData = tempData;
|
||||
}
|
||||
|
|
@ -60,11 +58,11 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
_filterProviders,
|
||||
_controllerFactory,
|
||||
actionDescriptor,
|
||||
_inputFormattersProvider,
|
||||
_inputFormatters,
|
||||
_argumentBinder,
|
||||
_modelBinderProvider,
|
||||
_modelValidationProviderProvider,
|
||||
_valueProviderFactoryProvider,
|
||||
_modelBinders,
|
||||
_modelValidatorProviders,
|
||||
_valueProviderFactories,
|
||||
_actionBindingContextAccessor,
|
||||
_tempData);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,9 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Mvc.Core;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Metadata;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
|
@ -37,7 +35,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
|
||||
public async Task<IDictionary<string, object>> BindActionArgumentsAsync(
|
||||
ActionContext actionContext,
|
||||
ActionBindingContext actionBindingContext,
|
||||
ActionBindingContext actionBindingContext,
|
||||
object controller)
|
||||
{
|
||||
var actionDescriptor = actionContext.ActionDescriptor as ControllerActionDescriptor;
|
||||
|
|
@ -66,7 +64,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
actionArguments,
|
||||
actionDescriptor.Parameters);
|
||||
return actionArguments;
|
||||
}
|
||||
}
|
||||
|
||||
private void ActivateProperties(object controller, Type containerType, Dictionary<string, object> properties)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,37 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.ModelBinding.Validation;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class DefaultValidationExcludeFiltersProvider
|
||||
: OptionDescriptorBasedProvider<IExcludeTypeValidationFilter>, IValidationExcludeFiltersProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DefaultValidationExcludeFiltersProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="options">An accessor to the <see cref="MvcOptions"/> configured for this application.</param>
|
||||
/// <param name="typeActivatorCache">The <see cref="ITypeActivatorCache"/> cache.</param>
|
||||
/// <param name="serviceProvider">The <see cref="IServiceProvider"/>.</param>
|
||||
public DefaultValidationExcludeFiltersProvider(IOptions<MvcOptions> optionsAccessor,
|
||||
ITypeActivatorCache typeActivatorCache,
|
||||
IServiceProvider serviceProvider)
|
||||
: base(optionsAccessor.Options.ValidationExcludeFilters, typeActivatorCache, serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<IExcludeTypeValidationFilter> ExcludeFilters
|
||||
{
|
||||
get
|
||||
{
|
||||
return Options;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ using Microsoft.AspNet.Mvc.ModelBinding.Metadata;
|
|||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.AspNet.Routing.Template;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Description
|
||||
|
|
@ -21,21 +22,23 @@ namespace Microsoft.AspNet.Mvc.Description
|
|||
/// </summary>
|
||||
public class DefaultApiDescriptionProvider : IApiDescriptionProvider
|
||||
{
|
||||
private readonly IOutputFormattersProvider _formattersProvider;
|
||||
private readonly IList<IOutputFormatter> _outputFormatters;
|
||||
private readonly IModelMetadataProvider _modelMetadataProvider;
|
||||
private readonly IInlineConstraintResolver _constraintResolver;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="DefaultApiDescriptionProvider"/>.
|
||||
/// </summary>
|
||||
/// <param name="formattersProvider">The <see cref="IOutputFormattersProvider"/>.</param>
|
||||
/// <param name="optionsAccessor">The accessor for <see cref="MvcOptions"/>.</param>
|
||||
/// <param name="constraintResolver">The <see cref="IInlineConstraintResolver"/> used for resolving inline
|
||||
/// constraints.</param>
|
||||
/// <param name="modelMetadataProvider">The <see cref="IModelMetadataProvider"/>.</param>
|
||||
public DefaultApiDescriptionProvider(
|
||||
IOutputFormattersProvider formattersProvider,
|
||||
IOptions<MvcOptions> optionsAccessor,
|
||||
IInlineConstraintResolver constraintResolver,
|
||||
IModelMetadataProvider modelMetadataProvider)
|
||||
{
|
||||
_formattersProvider = formattersProvider;
|
||||
_outputFormatters = optionsAccessor.Options.OutputFormatters;
|
||||
_constraintResolver = constraintResolver;
|
||||
_modelMetadataProvider = modelMetadataProvider;
|
||||
}
|
||||
|
|
@ -316,10 +319,9 @@ namespace Microsoft.AspNet.Mvc.Description
|
|||
contentTypes.Add(null);
|
||||
}
|
||||
|
||||
var formatters = _formattersProvider.OutputFormatters;
|
||||
foreach (var contentType in contentTypes)
|
||||
{
|
||||
foreach (var formatter in formatters)
|
||||
foreach (var formatter in _outputFormatters)
|
||||
{
|
||||
var responseFormatMetadataProvider = formatter as IApiResponseFormatMetadataProvider;
|
||||
if (responseFormatMetadataProvider != null)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.ModelBinding.Validation;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions for <see cref="IList{IExcludeTypeValidationFilter}"/>.
|
||||
/// </summary>
|
||||
public static class ExcludeTypeValidationFilterExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a descriptor to the specified <paramref name="excludeTypeValidationFilters" /> that excludes the properties of
|
||||
/// the <see cref="Type"/> specified and its derived types from validaton.
|
||||
/// </summary>
|
||||
/// <param name="excludeTypeValidationFilters">A list of <see cref="IExcludeTypeValidationFilter"/> which are used to
|
||||
/// get a collection of exclude filters to be applied for filtering model properties during validation.
|
||||
/// </param>
|
||||
/// <param name="type"><see cref="Type"/> which should be excluded from validation.</param>
|
||||
public static void Add(this IList<IExcludeTypeValidationFilter> excludeTypeValidationFilters, Type type)
|
||||
{
|
||||
var typeBasedExcludeFilter = new DefaultTypeBasedExcludeFilter(type);
|
||||
excludeTypeValidationFilters.Add(typeBasedExcludeFilter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a descriptor to the specified <paramref name="excludeTypeValidationFilters" /> that excludes the properties of
|
||||
/// the type specified and its derived types from validaton.
|
||||
/// </summary>
|
||||
/// <param name="excludeTypeValidationFilters">A list of <see cref="IExcludeTypeValidationFilter"/> which are used to
|
||||
/// get a collection of exclude filters to be applied for filtering model properties during validation.
|
||||
/// </param>
|
||||
/// <param name="typeFullName">Full name of the type which should be excluded from validation.</param>
|
||||
public static void Add(this IList<IExcludeTypeValidationFilter> excludeTypeValidationFilters, string typeFullName)
|
||||
{
|
||||
var filter = new DefaultTypeNameBasedExcludeFilter(typeFullName);
|
||||
excludeTypeValidationFilters.Add(filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -16,10 +16,11 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
public abstract class FilterActionInvoker : IActionInvoker
|
||||
{
|
||||
private readonly IReadOnlyList<IFilterProvider> _filterProviders;
|
||||
private readonly IInputFormattersProvider _inputFormatterProvider;
|
||||
private readonly IModelBinderProvider _modelBinderProvider;
|
||||
private readonly IModelValidatorProviderProvider _modelValidatorProviderProvider;
|
||||
private readonly IValueProviderFactoryProvider _valueProviderFactoryProvider;
|
||||
private readonly IReadOnlyList<IInputFormatter> _inputFormatters;
|
||||
private readonly IReadOnlyList<IModelBinder> _modelBinders;
|
||||
private readonly IReadOnlyList<IModelValidatorProvider> _modelValidatorProviders;
|
||||
private readonly IReadOnlyList<IValueProviderFactory> _valueProviderFactories;
|
||||
|
||||
private readonly IScopedInstance<ActionBindingContext> _actionBindingContextAccessor;
|
||||
|
||||
private IFilter[] _filters;
|
||||
|
|
@ -41,19 +42,19 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
public FilterActionInvoker(
|
||||
[NotNull] ActionContext actionContext,
|
||||
[NotNull] IReadOnlyList<IFilterProvider> filterProviders,
|
||||
[NotNull] IInputFormattersProvider inputFormatterProvider,
|
||||
[NotNull] IModelBinderProvider modelBinderProvider,
|
||||
[NotNull] IModelValidatorProviderProvider modelValidatorProviderProvider,
|
||||
[NotNull] IValueProviderFactoryProvider valueProviderFactoryProvider,
|
||||
[NotNull] IReadOnlyList<IInputFormatter> inputFormatters,
|
||||
[NotNull] IReadOnlyList<IModelBinder> modelBinders,
|
||||
[NotNull] IReadOnlyList<IModelValidatorProvider> modelValidatorProviders,
|
||||
[NotNull] IReadOnlyList<IValueProviderFactory> valueProviderFactories,
|
||||
[NotNull] IScopedInstance<ActionBindingContext> actionBindingContextAccessor)
|
||||
{
|
||||
ActionContext = actionContext;
|
||||
|
||||
_filterProviders = filterProviders;
|
||||
_inputFormatterProvider = inputFormatterProvider;
|
||||
_modelBinderProvider = modelBinderProvider;
|
||||
_modelValidatorProviderProvider = modelValidatorProviderProvider;
|
||||
_valueProviderFactoryProvider = valueProviderFactoryProvider;
|
||||
_inputFormatters = inputFormatters;
|
||||
_modelBinders = modelBinders;
|
||||
_modelValidatorProviders = modelValidatorProviders;
|
||||
_valueProviderFactories = valueProviderFactories;
|
||||
_actionBindingContextAccessor = actionBindingContextAccessor;
|
||||
}
|
||||
|
||||
|
|
@ -206,14 +207,10 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
|
||||
var context = new ResourceExecutingContext(ActionContext, _filters);
|
||||
|
||||
context.InputFormatters = new List<IInputFormatter>(_inputFormatterProvider.InputFormatters);
|
||||
context.ModelBinders = new List<IModelBinder>(_modelBinderProvider.ModelBinders);
|
||||
|
||||
context.ValidatorProviders = new List<IModelValidatorProvider>(
|
||||
_modelValidatorProviderProvider.ModelValidatorProviders);
|
||||
|
||||
context.ValueProviderFactories = new List<IValueProviderFactory>(
|
||||
_valueProviderFactoryProvider.ValueProviderFactories);
|
||||
context.InputFormatters = new List<IInputFormatter>(_inputFormatters);
|
||||
context.ModelBinders = new List<IModelBinder>(_modelBinders);
|
||||
context.ValidatorProviders = new List<IModelValidatorProvider>(_modelValidatorProviders);
|
||||
context.ValueProviderFactories = new List<IValueProviderFactory>(_valueProviderFactories);
|
||||
|
||||
_resourceExecutingContext = context;
|
||||
await InvokeResourceFilterAsync();
|
||||
|
|
|
|||
|
|
@ -1,38 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class DefaultInputFormattersProvider
|
||||
: OptionDescriptorBasedProvider<IInputFormatter>, IInputFormattersProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the DefaultInputFormattersProvider class.
|
||||
/// </summary>
|
||||
/// <param name="options">An accessor to the <see cref="MvcOptions"/> configured for this application.</param>
|
||||
/// <param name="typeActivatorCache">As <see cref="ITypeActivatorCache"/> instance that creates an instance
|
||||
/// of type <see cref="IInputFormatter"/>.</param>
|
||||
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
|
||||
/// service collection.</param>
|
||||
public DefaultInputFormattersProvider(IOptions<MvcOptions> optionsAccessor,
|
||||
ITypeActivatorCache typeActivatorCache,
|
||||
IServiceProvider serviceProvider)
|
||||
: base(optionsAccessor.Options.InputFormatters, typeActivatorCache, serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<IInputFormatter> InputFormatters
|
||||
{
|
||||
get
|
||||
{
|
||||
return Options;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.OptionDescriptors;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class DefaultOutputFormattersProvider
|
||||
: OptionDescriptorBasedProvider<IOutputFormatter>, IOutputFormattersProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the DefaultOutputFormattersProvider class.
|
||||
/// </summary>
|
||||
/// <param name="options">An accessor to the <see cref="MvcOptions"/> configured for this application.</param>
|
||||
/// <param name="typeActivatorCache">As <see cref="ITypeActivatorCache"/> instance that creates an instance
|
||||
/// of type <see cref="IOutputFormatter"/>.</param>
|
||||
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
|
||||
/// service collection.</param>
|
||||
public DefaultOutputFormattersProvider(IOptions<MvcOptions> optionsAccessor,
|
||||
ITypeActivatorCache typeActivatorCache,
|
||||
IServiceProvider serviceProvider)
|
||||
: base(optionsAccessor.Options.OutputFormatters, typeActivatorCache, serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<IOutputFormatter> OutputFormatters
|
||||
{
|
||||
get
|
||||
{
|
||||
return Options;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,6 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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 System.Threading.Tasks;
|
||||
using Microsoft.AspNet.WebUtilities;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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 System.Threading.Tasks;
|
||||
using Microsoft.AspNet.WebUtilities;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
|
|
|||
|
|
@ -1,18 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an activated collection of <see cref="IInputFormatter"/> instances.
|
||||
/// </summary>
|
||||
public interface IInputFormattersProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a collection of activated InputFormatter instances.
|
||||
/// </summary>
|
||||
IReadOnlyList<IInputFormatter> InputFormatters { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The <see cref="JsonResult"/> class filter the collection of
|
||||
/// <see cref="IOutputFormattersProvider.OutputFormatters"/> and use only those which implement
|
||||
/// <see cref="MvcOptions.OutputFormatters"/> and use only those which implement
|
||||
/// <see cref="IJsonOutputFormatter"/>.
|
||||
///
|
||||
/// To create a custom formatter that can be used by <see cref="JsonResult"/>, derive from
|
||||
|
|
|
|||
|
|
@ -1,18 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an activated collection of <see cref="IOutputFormatter"/> instances.
|
||||
/// </summary>
|
||||
public interface IOutputFormattersProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a collection of activated OutputFormatter instances.
|
||||
/// </summary>
|
||||
IReadOnlyList<IOutputFormatter> OutputFormatters { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -6,8 +6,6 @@ using System.Linq;
|
|||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Mvc.Core;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
|
|
@ -19,39 +17,25 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
|||
/// </summary>
|
||||
public class BodyModelBinder : BindingSourceModelBinder
|
||||
{
|
||||
private readonly ActionContext _actionContext;
|
||||
private readonly IScopedInstance<ActionBindingContext> _bindingContext;
|
||||
private readonly IInputFormatterSelector _formatterSelector;
|
||||
private readonly IValidationExcludeFiltersProvider _bodyValidationExcludeFiltersProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="BodyModelBinder"/>.
|
||||
/// </summary>
|
||||
/// <param name="context">An accessor to the <see cref="ActionContext"/>.</param>
|
||||
/// <param name="bindingContext">An accessor to the <see cref="ActionBindingContext"/>.</param>
|
||||
/// <param name="selector">The <see cref="IInputFormatterSelector"/>.</param>
|
||||
/// <param name="bodyValidationExcludeFiltersProvider">
|
||||
/// The <see cref="IValidationExcludeFiltersProvider"/>.
|
||||
/// </param>
|
||||
public BodyModelBinder([NotNull] IScopedInstance<ActionContext> context,
|
||||
[NotNull] IScopedInstance<ActionBindingContext> bindingContext,
|
||||
[NotNull] IInputFormatterSelector selector,
|
||||
[NotNull] IValidationExcludeFiltersProvider bodyValidationExcludeFiltersProvider)
|
||||
public BodyModelBinder()
|
||||
: base(BindingSource.Body)
|
||||
{
|
||||
_actionContext = context.Value;
|
||||
_bindingContext = bindingContext;
|
||||
_formatterSelector = selector;
|
||||
_bodyValidationExcludeFiltersProvider = bodyValidationExcludeFiltersProvider;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected async override Task<ModelBindingResult> BindModelCoreAsync([NotNull] ModelBindingContext bindingContext)
|
||||
{
|
||||
var formatters = _bindingContext.Value.InputFormatters;
|
||||
var requestServices = bindingContext.OperationBindingContext.HttpContext.RequestServices;
|
||||
|
||||
var formatterContext = new InputFormatterContext(_actionContext, bindingContext.ModelType);
|
||||
var formatter = _formatterSelector.SelectFormatter(formatters.ToList(), formatterContext);
|
||||
var formatterSelector = requestServices.GetRequiredService<IInputFormatterSelector>();
|
||||
var actionContext = requestServices.GetRequiredService<IScopedInstance<ActionContext>>().Value;
|
||||
var formatters = requestServices.GetRequiredService<IScopedInstance<ActionBindingContext>>().Value.InputFormatters;
|
||||
|
||||
var formatterContext = new InputFormatterContext(actionContext, bindingContext.ModelType);
|
||||
var formatter = formatterSelector.SelectFormatter(formatters.ToList(), formatterContext);
|
||||
|
||||
if (formatter == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Mvc.ApplicationModels;
|
||||
using Microsoft.AspNet.Mvc.Core;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Metadata;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
|
|
@ -22,16 +22,16 @@ namespace Microsoft.AspNet.Mvc
|
|||
public MvcOptions()
|
||||
{
|
||||
Conventions = new List<IApplicationModelConvention>();
|
||||
ModelBinders = new List<ModelBinderDescriptor>();
|
||||
ModelBinders = new List<IModelBinder>();
|
||||
ViewEngines = new List<ViewEngineDescriptor>();
|
||||
ValueProviderFactories = new List<ValueProviderFactoryDescriptor>();
|
||||
OutputFormatters = new List<OutputFormatterDescriptor>();
|
||||
InputFormatters = new List<InputFormatterDescriptor>();
|
||||
ValueProviderFactories = new List<IValueProviderFactory>();
|
||||
OutputFormatters = new List<IOutputFormatter>();
|
||||
InputFormatters = new List<IInputFormatter>();
|
||||
Filters = new List<IFilter>();
|
||||
FormatterMappings = new FormatterMappings();
|
||||
ValidationExcludeFilters = new List<ExcludeValidationDescriptor>();
|
||||
ValidationExcludeFilters = new List<IExcludeTypeValidationFilter>();
|
||||
ModelMetadataDetailsProviders = new List<IMetadataDetailsProvider>();
|
||||
ModelValidatorProviders = new List<ModelValidatorProviderDescriptor>();
|
||||
ModelValidatorProviders = new List<IModelValidatorProvider>();
|
||||
CacheProfiles = new Dictionary<string, CacheProfile>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
|
|
@ -68,25 +68,23 @@ namespace Microsoft.AspNet.Mvc
|
|||
/// Gets a list of <see cref="IFilter"/> which are used to construct filters that
|
||||
/// apply to all actions.
|
||||
/// </summary>
|
||||
public ICollection<IFilter> Filters { get; private set; }
|
||||
public ICollection<IFilter> Filters { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of the <see cref="OutputFormatterDescriptor" /> which are used to construct
|
||||
/// a list of <see cref="IOutputFormatter"/> by <see cref="IOutputFormattersProvider"/>.
|
||||
/// Gets a list of <see cref="IOutputFormatter"/>s that are used by this application.
|
||||
/// </summary>
|
||||
public IList<OutputFormatterDescriptor> OutputFormatters { get; }
|
||||
public IList<IOutputFormatter> OutputFormatters { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of the <see cref="InputFormatterDescriptor" /> which are used to construct
|
||||
/// a list of <see cref="IInputFormatter"/> by <see cref="IInputFormattersProvider"/>.
|
||||
/// Gets a list of <see cref="IInputFormatter"/>s that are used by this application.
|
||||
/// </summary>
|
||||
public IList<InputFormatterDescriptor> InputFormatters { get; }
|
||||
public IList<IInputFormatter> InputFormatters { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of <see cref="ExcludeValidationDescriptor"/> which are used to construct a list
|
||||
/// of exclude filters by <see cref="ModelBinding.Validation.IValidationExcludeFiltersProvider"/>.
|
||||
/// Gets a list of <see cref="IExcludeTypeValidationFilter"/>s that are used by this application.
|
||||
/// </summary>
|
||||
public IList<ExcludeValidationDescriptor> ValidationExcludeFilters { get; }
|
||||
public IList<IExcludeTypeValidationFilter> ValidationExcludeFilters { get; }
|
||||
= new List<IExcludeTypeValidationFilter>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the maximum number of validation errors that are allowed by this application before further
|
||||
|
|
@ -107,17 +105,14 @@ namespace Microsoft.AspNet.Mvc
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a list of the <see cref="ModelBinderDescriptor" /> used by the
|
||||
/// Gets a list of the <see cref="ModelBinderDescriptor" /> used by the
|
||||
/// <see cref="ModelBinding.CompositeModelBinder" />.
|
||||
/// Gets a list of <see cref="IModelBinder"/>s used by this application.
|
||||
/// </summary>
|
||||
public IList<ModelBinderDescriptor> ModelBinders { get; }
|
||||
public IList<IModelBinder> ModelBinders { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of the <see cref="ModelValidatorProviderDescriptor" />s used by
|
||||
/// <see cref="ModelBinding.Validation.CompositeModelValidatorProvider"/>.
|
||||
/// Gets a list of <see cref="IModelValidatorProvider"/>s used by this application.
|
||||
/// </summary>
|
||||
public IList<ModelValidatorProviderDescriptor> ModelValidatorProviders { get; }
|
||||
public IList<IModelValidatorProvider> ModelValidatorProviders { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of descriptors that represent <see cref="Rendering.IViewEngine"/> used
|
||||
|
|
@ -126,10 +121,9 @@ namespace Microsoft.AspNet.Mvc
|
|||
public IList<ViewEngineDescriptor> ViewEngines { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of descriptors that represent
|
||||
/// <see cref="ModelBinding.IValueProviderFactory"/> used by this application.
|
||||
/// Gets a list of <see cref="IValueProviderFactory"/> used by this application.
|
||||
/// </summary>
|
||||
public IList<ValueProviderFactoryDescriptor> ValueProviderFactories { get; }
|
||||
public IList<IValueProviderFactory> ValueProviderFactories { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of <see cref="IApplicationModelConvention"/> instances that will be applied to
|
||||
|
|
|
|||
|
|
@ -1,39 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.ModelBinding;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class DefaultModelBindersProvider : OptionDescriptorBasedProvider<IModelBinder>, IModelBinderProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the DefaultModelBindersProvider class.
|
||||
/// </summary>
|
||||
/// <param name="options">An accessor to the <see cref="MvcOptions"/> configured for this application.</param>
|
||||
/// <param name="typeActivatorCache">As <see cref="ITypeActivatorCache"/> instance that creates an instance
|
||||
/// of type <see cref="IModelBinder"/>.</param>
|
||||
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
|
||||
/// service collection.</param>
|
||||
public DefaultModelBindersProvider(
|
||||
IOptions<MvcOptions> optionsAccessor,
|
||||
ITypeActivatorCache typeActivatorCache,
|
||||
IServiceProvider serviceProvider)
|
||||
: base(optionsAccessor.Options.ModelBinders, typeActivatorCache, serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<IModelBinder> ModelBinders
|
||||
{
|
||||
get
|
||||
{
|
||||
return Options;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.ModelBinding.Validation;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class DefaultModelValidatorProviderProvider : OptionDescriptorBasedProvider<IModelValidatorProvider>,
|
||||
IModelValidatorProviderProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DefaultModelValidatorProviderProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="options">An accessor to the <see cref="MvcOptions"/> configured for this application.</param>
|
||||
/// <param name="typeActivatorCache">As <see cref="ITypeActivatorCache"/> instance that creates an instance
|
||||
/// of type <see cref="IModelValidatorProvider"/>.</param>
|
||||
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
|
||||
/// service collection.</param>
|
||||
public DefaultModelValidatorProviderProvider(
|
||||
IOptions<MvcOptions> optionsAccessor,
|
||||
ITypeActivatorCache typeActivatorCache,
|
||||
IServiceProvider serviceProvider)
|
||||
: base(optionsAccessor.Options.ModelValidatorProviders, typeActivatorCache, serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<IModelValidatorProvider> ModelValidatorProviders
|
||||
{
|
||||
get { return Options; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.ModelBinding;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class DefaultValueProviderFactoryProvider :
|
||||
OptionDescriptorBasedProvider<IValueProviderFactory>, IValueProviderFactoryProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DefaultValueProviderFactoryProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="options">An accessor to the <see cref="MvcOptions"/> configured for this application.</param>
|
||||
/// <param name = "typeActivatorCache" > As <see cref="ITypeActivatorCache"/> instance that creates
|
||||
/// an instance of type <see cref="IValueProviderFactory"/>.</param>
|
||||
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
|
||||
/// service collection.</param>
|
||||
public DefaultValueProviderFactoryProvider(
|
||||
IOptions<MvcOptions> optionsAccessor,
|
||||
ITypeActivatorCache typeActivatorCache,
|
||||
IServiceProvider serviceProvider)
|
||||
: base(optionsAccessor.Options.ValueProviderFactories, typeActivatorCache, serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<IValueProviderFactory> ValueProviderFactories
|
||||
{
|
||||
get
|
||||
{
|
||||
return Options;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Rendering;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class DefaultViewEngineProvider : OptionDescriptorBasedProvider<IViewEngine>, IViewEngineProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DefaultViewEngineProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="options">An accessor to the <see cref="MvcOptions"/> configured for this application.</param>
|
||||
/// <param name="typeActivatorCache">As <see cref="ITypeActivatorCache"/> instance that creates
|
||||
/// an instance of type <see cref="IViewEngine"/>.</param>
|
||||
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
|
||||
/// service collection.</param>
|
||||
public DefaultViewEngineProvider(
|
||||
IOptions<MvcOptions> optionsAccessor,
|
||||
ITypeActivatorCache typeActivatorCache,
|
||||
IServiceProvider serviceProvider)
|
||||
: base(optionsAccessor.Options.ViewEngines, typeActivatorCache, serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<IViewEngine> ViewEngines
|
||||
{
|
||||
get
|
||||
{
|
||||
return Options;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Encapsulates information that describes an <see cref="IExcludeTypeValidationFilter"/>.
|
||||
/// </summary>
|
||||
public class ExcludeValidationDescriptor : OptionDescriptor<IExcludeTypeValidationFilter>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ExcludeValidationDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">
|
||||
/// A <see cref="IExcludeTypeValidationFilter"/> type that the descriptor represents.
|
||||
/// </param>
|
||||
public ExcludeValidationDescriptor([NotNull] Type type)
|
||||
: base(type)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ExcludeValidationDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="validationFilter">An instance of <see cref="IExcludeTypeValidationFilter"/>
|
||||
/// that the descriptor represents.</param>
|
||||
public ExcludeValidationDescriptor([NotNull] IExcludeTypeValidationFilter validationFilter)
|
||||
: base(validationFilter)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Encapsulates information that describes a <typeparamref name="TOption"/> option on <see cref="MvcOptions"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TOption">The type of the option.</typeparam>
|
||||
public interface IOptionDescriptor<out TOption>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the type of the <typeparamref name="TOption"/> described by this
|
||||
/// <see cref="IOptionDescriptor{TOption}"/>.
|
||||
/// </summary>
|
||||
Type OptionType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the instance of <typeparamref name="TOption"/> described by this
|
||||
/// <see cref="IOptionDescriptor{TOption}"/>.
|
||||
/// </summary>
|
||||
TOption Instance { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Encapsulates information that describes an <see cref="IInputFormatter"/>.
|
||||
/// </summary>
|
||||
public class InputFormatterDescriptor : OptionDescriptor<IInputFormatter>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="InputFormatterDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">A <see cref="IInputFormatter"/> type that the descriptor represents.
|
||||
/// </param>
|
||||
public InputFormatterDescriptor([NotNull] Type type)
|
||||
: base(type)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="InputFormatterDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="inputFormatter">An instance of <see cref="IInputFormatter"/>
|
||||
/// that the descriptor represents.</param>
|
||||
public InputFormatterDescriptor([NotNull] IInputFormatter inputFormatter)
|
||||
: base(inputFormatter)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,84 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.OptionDescriptors;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for adding Input formatters to a collection.
|
||||
/// </summary>
|
||||
public static class InputFormatterDescriptorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a type representing a <see cref="IInputFormatter"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of InputFormatterDescriptors</param>
|
||||
/// <param name="inputFormatterType">Type representing an <see cref="IInputFormatter"/>.</param>
|
||||
/// <returns>InputFormatterDescriptor representing the added instance.</returns>
|
||||
public static InputFormatterDescriptor Add([NotNull] this IList<InputFormatterDescriptor> descriptors,
|
||||
[NotNull] Type inputFormatterType)
|
||||
{
|
||||
var descriptor = new InputFormatterDescriptor(inputFormatterType);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts a type representing a <see cref="IInputFormatter"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of InputFormatterDescriptors</param>
|
||||
/// <param name="inputFormatterType">Type representing an <see cref="IInputFormatter"/>.</param>
|
||||
/// <returns>InputFormatterDescriptor representing the inserted instance.</returns>
|
||||
public static InputFormatterDescriptor Insert([NotNull] this IList<InputFormatterDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] Type inputFormatterType)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
}
|
||||
|
||||
var descriptor = new InputFormatterDescriptor(inputFormatterType);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an <see cref="IInputFormatter"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of InputFormatterDescriptors</param>
|
||||
/// <param name="inputFormatter">An <see cref="IInputFormatter"/> instance.</param>
|
||||
/// <returns>InputFormatterDescriptor representing the added instance.</returns>
|
||||
public static InputFormatterDescriptor Add([NotNull] this IList<InputFormatterDescriptor> descriptors,
|
||||
[NotNull] IInputFormatter inputFormatter)
|
||||
{
|
||||
var descriptor = new InputFormatterDescriptor(inputFormatter);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Insert an <see cref="IInputFormatter"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of InputFormatterDescriptors</param>
|
||||
/// <param name="inputFormatter">An <see cref="IInputFormatter"/> instance.</param>
|
||||
/// <returns>InputFormatterDescriptor representing the added instance.</returns>
|
||||
public static InputFormatterDescriptor Insert([NotNull] this IList<InputFormatterDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] IInputFormatter inputFormatter)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
}
|
||||
|
||||
var descriptor = new InputFormatterDescriptor(inputFormatter);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Mvc.ModelBinding;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Encapsulates information that describes an <see cref="IModelBinder"/>.
|
||||
/// </summary>
|
||||
public class ModelBinderDescriptor : OptionDescriptor<IModelBinder>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ModelBinderDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">A type that represents a <see cref="IModelBinder"/>.</param>
|
||||
public ModelBinderDescriptor([NotNull] Type type)
|
||||
: base(type)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ModelBinderDescriptor"/> with the specified instance.
|
||||
/// </summary>
|
||||
/// <param name="option">An instance of <see cref="IModelBinder"/>.</param>
|
||||
public ModelBinderDescriptor([NotNull] IModelBinder binder)
|
||||
: base(binder)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,103 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for adding model binders to a collection.
|
||||
/// </summary>
|
||||
public static class ModelBinderDescriptorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a type representing a <see cref="IModelBinder"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ModelBinderDescriptors</param>
|
||||
/// <param name="modelBinderType">Type representing an <see cref="IModelBinder"/>.</param>
|
||||
/// <returns>ModelBinderDescriptor representing the added instance.</returns>
|
||||
public static ModelBinderDescriptor Add([NotNull] this IList<ModelBinderDescriptor> descriptors,
|
||||
[NotNull] Type modelBinderType)
|
||||
{
|
||||
var descriptor = new ModelBinderDescriptor(modelBinderType);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts a type representing a <see cref="IModelBinder"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ModelBinderDescriptors</param>
|
||||
/// <param name="modelBinderType">Type representing an <see cref="IModelBinder"/>.</param>
|
||||
/// <returns>ModelBinderDescriptor representing the inserted instance.</returns>
|
||||
public static ModelBinderDescriptor Insert([NotNull] this IList<ModelBinderDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] Type modelBinderType)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
}
|
||||
|
||||
var descriptor = new ModelBinderDescriptor(modelBinderType);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an <see cref="IModelBinder"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ModelBinderDescriptors</param>
|
||||
/// <param name="modelBinder">An <see cref="IModelBinder"/> instance.</param>
|
||||
/// <returns>ModelBinderDescriptor representing the added instance.</returns>
|
||||
public static ModelBinderDescriptor Add([NotNull] this IList<ModelBinderDescriptor> descriptors,
|
||||
[NotNull] IModelBinder modelBinder)
|
||||
{
|
||||
var descriptor = new ModelBinderDescriptor(modelBinder);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Insert an <see cref="IModelBinder"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ModelBinderDescriptors</param>
|
||||
/// <param name="modelBinder">An <see cref="IModelBinder"/> instance.</param>
|
||||
/// <returns>ModelBinderDescriptor representing the added instance.</returns>
|
||||
public static ModelBinderDescriptor Insert([NotNull] this IList<ModelBinderDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] IModelBinder modelBinder)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
}
|
||||
|
||||
var descriptor = new ModelBinderDescriptor(modelBinder);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes instances of <typeparamref name="TInstance"/> from a descriptor collection
|
||||
/// where the type exactly matches <typeparamref name="TInstance"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInstance">A type that implements <see cref="IModelBinder"/>.</typeparam>
|
||||
/// <param name="descriptors">A list of ModelBinderDescriptors.</param>
|
||||
public static void RemoveTypesOf<TInstance>([NotNull] this IList<ModelBinderDescriptor> descriptors)
|
||||
where TInstance : class, IModelBinder
|
||||
{
|
||||
for (var i = descriptors.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (descriptors[i].OptionType == typeof(TInstance))
|
||||
{
|
||||
descriptors.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Encapsulates information that describes an <see cref="IModelValidatorProvider"/>.
|
||||
/// </summary>
|
||||
public class ModelValidatorProviderDescriptor : OptionDescriptor<IModelValidatorProvider>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ModelValidatorProviderDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">A type that represents a <see cref="IModelValidatorProvider"/>.</param>
|
||||
public ModelValidatorProviderDescriptor([NotNull] Type type)
|
||||
: base(type)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ModelValidatorProviderDescriptor"/> with the specified instance.
|
||||
/// </summary>
|
||||
/// <param name="option">An instance of <see cref="IModelValidatorProvider"/>.</param>
|
||||
public ModelValidatorProviderDescriptor([NotNull] IModelValidatorProvider validatorProvider)
|
||||
: base(validatorProvider)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.ModelBinding.Validation;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for adding validator provider to a collection.
|
||||
/// </summary>
|
||||
public static class ModelValidatorProviderDescriptorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a type representing a <see cref="IModelValidatorProvider"/> to <paramref name="descriptors"/>.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of <see cref="ModelValidatorProviderDescriptor"/>.</param>
|
||||
/// <param name="modelValidatorProviderType">Type representing an <see cref="IModelValidatorProvider"/></param>
|
||||
/// <returns>A <see cref="ModelValidatorProviderDescriptor"/> representing the added instance.</returns>
|
||||
public static ModelValidatorProviderDescriptor Add(
|
||||
[NotNull] this IList<ModelValidatorProviderDescriptor> descriptors,
|
||||
[NotNull] Type modelValidatorProviderType)
|
||||
{
|
||||
var descriptor = new ModelValidatorProviderDescriptor(modelValidatorProviderType);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts a type representing a <see cref="IModelValidatorProvider"/> in to <paramref name="descriptors"/> at
|
||||
/// the specified <paramref name="index"/>.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of <see cref="ModelValidatorProviderDescriptor"/>.</param>
|
||||
/// <param name="index">The zero-based index at which <paramref name="modelValidatorProviderType"/>
|
||||
/// should be inserted.</param>
|
||||
/// <param name="modelValidatorProviderType">Type representing an <see cref="IModelValidatorProvider"/></param>
|
||||
/// <returns>A <see cref="ModelValidatorProviderDescriptor"/> representing the inserted instance.</returns>
|
||||
public static ModelValidatorProviderDescriptor Insert(
|
||||
[NotNull] this IList<ModelValidatorProviderDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] Type modelValidatorProviderType)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
|
||||
var descriptor = new ModelValidatorProviderDescriptor(modelValidatorProviderType);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an <see cref="IModelValidatorProvider"/> to <paramref name="descriptors"/>.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of <see cref="ModelValidatorProviderDescriptor"/>.</param>
|
||||
/// <param name="modelValidatorProvider">An <see cref="IModelValidatorProvider"/> instance.</param>
|
||||
/// <returns>A <see cref="ModelValidatorProviderDescriptor"/> representing the added instance.</returns>
|
||||
public static ModelValidatorProviderDescriptor Add(
|
||||
[NotNull] this IList<ModelValidatorProviderDescriptor> descriptors,
|
||||
[NotNull] IModelValidatorProvider modelValidatorProvider)
|
||||
{
|
||||
var descriptor = new ModelValidatorProviderDescriptor(modelValidatorProvider);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Insert an <see cref="IModelValidatorProvider"/> in to <paramref name="descriptors"/> at the specified
|
||||
/// <paramref name="index"/>.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of <see cref="ModelValidatorProviderDescriptor"/>.</param>
|
||||
/// <param name="index">The zero-based index at which <paramref name="modelValidatorProvider"/>
|
||||
/// should be inserted.</param>
|
||||
/// <param name="modelValidatorProvider">An <see cref="IModelValidatorProvider"/> instance.</param>
|
||||
/// <returns>A <see cref="ModelValidatorProviderDescriptor"/> representing the added instance.</returns>
|
||||
public static ModelValidatorProviderDescriptor Insert(
|
||||
[NotNull] this IList<ModelValidatorProviderDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] IModelValidatorProvider modelValidatorProvider)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
|
||||
var descriptor = new ModelValidatorProviderDescriptor(modelValidatorProvider);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Mvc.Core;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Encapsulates information that describes a <typeparamref name="TOption"/> option on <see cref="MvcOptions"/> .
|
||||
/// </summary>
|
||||
/// <typeparam name="TOption">The type of the option.</typeparam>
|
||||
public class OptionDescriptor<TOption> : IOptionDescriptor<TOption>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="OptionDescriptor{TOption}"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">A type that represents <typeparamref name="TOption"/>.</param>
|
||||
public OptionDescriptor([NotNull] Type type)
|
||||
{
|
||||
var optionType = typeof(TOption);
|
||||
if (!optionType.IsAssignableFrom(type))
|
||||
{
|
||||
var message = Resources.FormatTypeMustDeriveFromType(type.FullName, optionType.FullName);
|
||||
throw new ArgumentException(message, "type");
|
||||
}
|
||||
|
||||
OptionType = type;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="OptionDescriptor{TOption}"/> with the specified instance.
|
||||
/// </summary>
|
||||
/// <param name="option">An instance of <typeparamref name="TOption"/> that the descriptor represents.</param>
|
||||
public OptionDescriptor([NotNull] TOption option)
|
||||
{
|
||||
Instance = option;
|
||||
OptionType = option.GetType();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of the <typeparamref name="TOption"/> described by this
|
||||
/// <see cref="OptionDescriptor{TOption}"/>.
|
||||
/// </summary>
|
||||
public Type OptionType
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the instance of <typeparamref name="TOption"/> described by this
|
||||
/// <see cref="OptionDescriptor{TOption}"/>.
|
||||
/// </summary>
|
||||
public TOption Instance
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,63 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.DependencyInjection;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a default implementation for instantiating options from a sequence of
|
||||
/// <see cref="OptionDescriptor{TOption}"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TOption">The type of the option.</typeparam>
|
||||
public abstract class OptionDescriptorBasedProvider<TOption>
|
||||
{
|
||||
private readonly IEnumerable<OptionDescriptor<TOption>> _optionDescriptors;
|
||||
private ITypeActivatorCache _typeActivatorCache;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="OptionDescriptorBasedProvider{TOption}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="optionDescriptors">An enumerable of <see cref="OptionDescriptor{TOption}"/>.</param>
|
||||
/// <param name="typeActivatorCache">As <see cref="ITypeActivatorCache"/> instance that creates an
|
||||
/// instance of type <typeparamref name="TOption"/>.</param>
|
||||
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
|
||||
/// service collection.</param>
|
||||
public OptionDescriptorBasedProvider(
|
||||
[NotNull] IEnumerable<OptionDescriptor<TOption>> optionDescriptors,
|
||||
[NotNull] ITypeActivatorCache typeActivatorCache,
|
||||
[NotNull] IServiceProvider serviceProvider)
|
||||
{
|
||||
_optionDescriptors = optionDescriptors;
|
||||
_typeActivatorCache = typeActivatorCache;
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an activated sequence of <typeparamref name="TOption"/> instance.
|
||||
/// </summary>
|
||||
protected IReadOnlyList<TOption> Options
|
||||
{
|
||||
get
|
||||
{
|
||||
var result = new List<TOption>();
|
||||
foreach (var descriptor in _optionDescriptors)
|
||||
{
|
||||
var instance = descriptor.Instance;
|
||||
if (instance == null)
|
||||
{
|
||||
instance = _typeActivatorCache.CreateInstance<TOption>(_serviceProvider, descriptor.OptionType);
|
||||
}
|
||||
|
||||
result.Add(instance);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for collections of option descriptors.
|
||||
/// </summary>
|
||||
public static class OptionDescriptorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the only instance of <typeparamref name="TInstance"/> from a sequence of option descriptors.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInstance">The type of the instance to find.</typeparam>
|
||||
/// <param name="descriptors">A sequence of <see cref="IOptionDescriptor{object}"/>.</param>
|
||||
/// <returns>The only instance of <typeparamref name="TInstance"/> from a sequence of
|
||||
/// <see cref="IOptionDescriptor{object}"/>.</returns>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// Thrown if there is not exactly one <typeparamref name="TInstance"/> in the sequence.</exception>
|
||||
public static TInstance InstanceOf<TInstance>(
|
||||
[NotNull] this IEnumerable<IOptionDescriptor<object>> descriptors)
|
||||
{
|
||||
var instance = descriptors
|
||||
.Single(descriptor => descriptor.OptionType == typeof(TInstance) && descriptor.Instance != null)
|
||||
.Instance;
|
||||
return (TInstance)instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the only instance of <typeparamref name="TInstance"/> from a sequence of option descriptors,
|
||||
/// or a default value if the sequence is empty.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInstance">The type of the instance to find.</typeparam>
|
||||
/// <param name="descriptors">A sequence of <see cref="IOptionDescriptor{object}"/>.</param>
|
||||
/// <returns>The only instance of <typeparamref name="TInstance"/> from a sequence of
|
||||
/// <see cref="IOptionDescriptor{object}"/>,
|
||||
/// or a default value if the sequence is empty.</returns>
|
||||
/// <exception cref="System.InvalidOperationException">
|
||||
/// Thrown if there is more than one <typeparamref name="TInstance"/> in the sequence.</exception>
|
||||
public static TInstance InstanceOfOrDefault<TInstance>([NotNull] this
|
||||
IEnumerable<IOptionDescriptor<object>> descriptors)
|
||||
{
|
||||
var item = descriptors
|
||||
.SingleOrDefault(
|
||||
descriptor => descriptor.OptionType == typeof(TInstance) && descriptor.Instance != null);
|
||||
var instance = default(TInstance);
|
||||
if (item != null)
|
||||
{
|
||||
instance = (TInstance)item.Instance;
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all the instances of <typeparamref name="TInstance"/> from a sequence of option descriptors.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInstance">The type of the instances to find.</typeparam>
|
||||
/// <param name="descriptors">A sequence of <see cref="IOptionDescriptor{object}"/>.</param>
|
||||
/// <returns>An IEnumerable of <typeparamref name="TInstance"/> that contains instances from a sequence
|
||||
/// of <see cref="IOptionDescriptor{object}"/>.</returns>
|
||||
public static IEnumerable<TInstance> InstancesOf<TInstance>([NotNull] this
|
||||
IEnumerable<IOptionDescriptor<object>> descriptors)
|
||||
{
|
||||
var instances = descriptors
|
||||
.Where(descriptor => descriptor.OptionType == typeof(TInstance) && descriptor.Instance != null)
|
||||
.Select(d => (TInstance)d.Instance);
|
||||
return instances;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Encapsulates information that describes an <see cref="IOutputFormatter"/>.
|
||||
/// </summary>
|
||||
public class OutputFormatterDescriptor : OptionDescriptor<IOutputFormatter>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">A <see cref="IOutputFormatter"/> type that the descriptor represents.
|
||||
/// </param>
|
||||
public OutputFormatterDescriptor([NotNull] Type type)
|
||||
: base(type)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="outputFormatter">An instance of <see cref="IOutputFormatter"/>
|
||||
/// that the descriptor represents.</param>
|
||||
public OutputFormatterDescriptor([NotNull] IOutputFormatter outputFormatter)
|
||||
: base(outputFormatter)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,102 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.OptionDescriptors;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for adding output formatters to a collection.
|
||||
/// </summary>
|
||||
public static class OutputFormatterDescriptorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a type representing a <see cref="IOutputFormatter"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of OutputFormatterDescriptors</param>
|
||||
/// <param name="outputFormatterType">Type representing an <see cref="IOutputFormatter"/>.</param>
|
||||
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
|
||||
public static OutputFormatterDescriptor Add([NotNull] this IList<OutputFormatterDescriptor> descriptors,
|
||||
[NotNull] Type outputFormatterType)
|
||||
{
|
||||
var descriptor = new OutputFormatterDescriptor(outputFormatterType);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts a type representing a <see cref="IOutputFormatter"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of OutputFormatterDescriptors</param>
|
||||
/// <param name="outputFormatterType">Type representing an <see cref="IOutputFormatter"/>.</param>
|
||||
/// <returns>OutputFormatterDescriptor representing the inserted instance.</returns>
|
||||
public static OutputFormatterDescriptor Insert([NotNull] this IList<OutputFormatterDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] Type outputFormatterType)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
}
|
||||
|
||||
var descriptor = new OutputFormatterDescriptor(outputFormatterType);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an <see cref="IOutputFormatter"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of OutputFormatterDescriptors</param>
|
||||
/// <param name="outputFormatter">An <see cref="IOutputFormatter"/> instance.</param>
|
||||
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
|
||||
public static OutputFormatterDescriptor Add([NotNull] this IList<OutputFormatterDescriptor> descriptors,
|
||||
[NotNull] IOutputFormatter outputFormatter)
|
||||
{
|
||||
var descriptor = new OutputFormatterDescriptor(outputFormatter);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Insert an <see cref="IOutputFormatter"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of OutputFormatterDescriptors</param>
|
||||
/// <param name="outputFormatter">An <see cref="IOutputFormatter"/> instance.</param>
|
||||
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
|
||||
public static OutputFormatterDescriptor Insert([NotNull] this IList<OutputFormatterDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] IOutputFormatter outputFormatter)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
}
|
||||
|
||||
var descriptor = new OutputFormatterDescriptor(outputFormatter);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes instances of <typeparamref name="TInstance"/> from a descriptor collection
|
||||
/// where the type exactly matches <typeparamref name="TInstance"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInstance">A type that implements <see cref="IOutputFormatter"/>.</typeparam>
|
||||
/// <param name="descriptors">A list of OutputFormatterDescriptors.</param>
|
||||
public static void RemoveTypesOf<TInstance>([NotNull] this IList<OutputFormatterDescriptor> descriptors)
|
||||
where TInstance : class, IOutputFormatter
|
||||
{
|
||||
for (var i = descriptors.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (descriptors[i].OptionType == typeof(TInstance))
|
||||
{
|
||||
descriptors.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.ModelBinding.Validation;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions for <see cref="MvcOptions.ValidationExcludeFilters"/>.
|
||||
/// </summary>
|
||||
public static class ValidationExcludeFiltersExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a descriptor to the specified <paramref name="descriptorCollection" /> that excludes the properties of
|
||||
/// the <see cref="Type"/> specified and its derived types from validaton.
|
||||
/// </summary>
|
||||
/// <param name="descriptorCollection">A list of <see cref="ExcludeValidationDescriptor"/> which are used to
|
||||
/// get a collection of exclude filters to be applied for filtering model properties during validation.
|
||||
/// </param>
|
||||
/// <param name="type"><see cref="Type"/> which should be excluded from validation.</param>
|
||||
public static void Add(this IList<ExcludeValidationDescriptor> descriptorCollection, Type type)
|
||||
{
|
||||
var typeBasedExcludeFilter = new DefaultTypeBasedExcludeFilter(type);
|
||||
descriptorCollection.Add(new ExcludeValidationDescriptor(typeBasedExcludeFilter));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a descriptor to the specified <paramref name="descriptorCollection" /> that excludes the properties of
|
||||
/// the type specified and its derived types from validaton.
|
||||
/// </summary>
|
||||
/// <param name="descriptorCollection">A list of <see cref="ExcludeValidationDescriptor"/> which are used to
|
||||
/// get a collection of exclude filters to be applied for filtering model properties during validation.
|
||||
/// </param>
|
||||
/// <param name="typeFullName">Full name of the type which should be excluded from validation.</param>
|
||||
public static void Add(this IList<ExcludeValidationDescriptor> descriptorCollection, string typeFullName)
|
||||
{
|
||||
var filter = new DefaultTypeNameBasedExcludeFilter(typeFullName);
|
||||
descriptorCollection.Add(new ExcludeValidationDescriptor(filter));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a descriptor to the specified <paramref name="descriptorCollection" /> that excludes the properties of
|
||||
/// the type specified and its derived types from validaton.
|
||||
/// </summary>
|
||||
/// <param name="descriptorCollection">A list of <see cref="ExcludeValidationDescriptor"/> which are used to
|
||||
/// get a collection of exclude filters to be applied for filtering model properties during validation.
|
||||
/// </param>
|
||||
/// <param name="filter"><see cref="IExcludeTypeValidationFilter"/> which should be excluded from validation.
|
||||
/// </param>
|
||||
public static void Add(this IList<ExcludeValidationDescriptor> descriptorCollection,
|
||||
IExcludeTypeValidationFilter filter)
|
||||
{
|
||||
descriptorCollection.Add(new ExcludeValidationDescriptor(filter));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Mvc.ModelBinding;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Encapsulates information that describes an <see cref="IValueProviderFactory"/>.
|
||||
/// </summary>
|
||||
public class ValueProviderFactoryDescriptor : OptionDescriptor<IValueProviderFactory>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ValueProviderFactoryDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">The <see cref="IValueProviderFactory"/> type that the descriptor represents.</param>
|
||||
public ValueProviderFactoryDescriptor([NotNull] Type type)
|
||||
: base(type)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ValueProviderFactoryDescriptor"/> using the specified type.
|
||||
/// </summary>
|
||||
/// <param name="valueProviderFactory">An instance of <see cref="IValueProviderFactory"/>
|
||||
/// that the descriptor represents.</param>
|
||||
public ValueProviderFactoryDescriptor([NotNull] IValueProviderFactory valueProviderFactory)
|
||||
: base(valueProviderFactory)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,107 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for adding <see cref="IValueProviderFactory"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
public static class ValueProviderFactoryDescriptorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a type representing a <see cref="IValueProviderFactory"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ValueProviderFactoryDescriptors</param>
|
||||
/// <param name="valueProviderType">Type representing an <see cref="IValueProviderFactory"/>.</param>
|
||||
/// <returns>ValueProviderFactoryDescriptor representing the added instance.</returns>
|
||||
public static ValueProviderFactoryDescriptor Add(
|
||||
[NotNull] this IList<ValueProviderFactoryDescriptor> descriptors,
|
||||
[NotNull] Type valueProviderType)
|
||||
{
|
||||
var descriptor = new ValueProviderFactoryDescriptor(valueProviderType);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts a type representing a <see cref="IValueProviderFactory"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ValueProviderFactoryDescriptors</param>
|
||||
/// <param name="viewEngineType">Type representing an <see cref="IValueProviderFactory"/>.</param>
|
||||
/// <returns>ValueProviderFactoryDescriptor representing the inserted instance.</returns>
|
||||
public static ValueProviderFactoryDescriptor Insert(
|
||||
[NotNull] this IList<ValueProviderFactoryDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] Type viewEngineType)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
}
|
||||
|
||||
var descriptor = new ValueProviderFactoryDescriptor(viewEngineType);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an <see cref="IValueProviderFactory"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ValueProviderFactoryDescriptors</param>
|
||||
/// <param name="valueProviderFactory">An <see cref="IValueProviderFactory"/> instance.</param>
|
||||
/// <returns>ValueProviderFactoryDescriptor representing the added instance.</returns>
|
||||
public static ValueProviderFactoryDescriptor Add(
|
||||
[NotNull] this IList<ValueProviderFactoryDescriptor> descriptors,
|
||||
[NotNull] IValueProviderFactory valueProviderFactory)
|
||||
{
|
||||
var descriptor = new ValueProviderFactoryDescriptor(valueProviderFactory);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Insert an <see cref="IValueProviderFactory"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ValueProviderFactoryDescriptors</param>
|
||||
/// <param name="valueProviderFactory">An <see cref="IValueProviderFactory"/> instance.</param>
|
||||
/// <returns>ValueProviderFactoryDescriptor representing the added instance.</returns>
|
||||
public static ValueProviderFactoryDescriptor Insert(
|
||||
[NotNull] this IList<ValueProviderFactoryDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] IValueProviderFactory valueProviderFactory)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
}
|
||||
|
||||
var descriptor = new ValueProviderFactoryDescriptor(valueProviderFactory);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes instances of <typeparamref name="TInstance"/> from a descriptor collection
|
||||
/// where the type exactly matches <typeparamref name="TInstance"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInstance">A type that implements <see cref="IValueProviderFactory"/>.</typeparam>
|
||||
/// <param name="descriptors">A list of ValueProviderFactoryDescriptors.</param>
|
||||
public static void RemoveTypesOf<TInstance>([NotNull] this IList<ValueProviderFactoryDescriptor> descriptors)
|
||||
where TInstance : class, IValueProviderFactory
|
||||
{
|
||||
for (var i = descriptors.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (descriptors[i].OptionType == typeof(TInstance))
|
||||
{
|
||||
descriptors.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,85 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.OptionDescriptors;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for adding view engines to a descriptor collection.
|
||||
/// </summary>
|
||||
public static class ViewEngineDescriptorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a type representing a <see cref="IViewEngine"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ViewEngineDescriptors</param>
|
||||
/// <param name="viewEngineType">Type representing an <see cref="IViewEngine"/>.</param>
|
||||
/// <returns>ViewEngineDescriptor representing the added instance.</returns>
|
||||
public static ViewEngineDescriptor Add([NotNull] this IList<ViewEngineDescriptor> descriptors,
|
||||
[NotNull] Type viewEngineType)
|
||||
{
|
||||
var descriptor = new ViewEngineDescriptor(viewEngineType);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts a type representing a <see cref="IViewEngine"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ViewEngineDescriptors</param>
|
||||
/// <param name="viewEngineType">Type representing an <see cref="IViewEngine"/>.</param>
|
||||
/// <returns>ViewEngineDescriptor representing the inserted instance.</returns>
|
||||
public static ViewEngineDescriptor Insert([NotNull] this IList<ViewEngineDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] Type viewEngineType)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
}
|
||||
|
||||
var descriptor = new ViewEngineDescriptor(viewEngineType);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an <see cref="IViewEngine"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ViewEngineDescriptors</param>
|
||||
/// <param name="viewEngine">An <see cref="IViewEngine"/> instance.</param>
|
||||
/// <returns>ViewEngineDescriptor representing the added instance.</returns>
|
||||
public static ViewEngineDescriptor Add([NotNull] this IList<ViewEngineDescriptor> descriptors,
|
||||
[NotNull] IViewEngine viewEngine)
|
||||
{
|
||||
var descriptor = new ViewEngineDescriptor(viewEngine);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Insert an <see cref="IViewEngine"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ViewEngineDescriptors</param>
|
||||
/// <param name="viewEngine">An <see cref="IViewEngine"/> instance.</param>
|
||||
/// <returns>ViewEngineDescriptor representing the added instance.</returns>
|
||||
public static ViewEngineDescriptor Insert([NotNull] this IList<ViewEngineDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] IViewEngine viewEngine)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
}
|
||||
|
||||
var descriptor = new ViewEngineDescriptor(viewEngine);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,24 +1,55 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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 System.Linq;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Rendering
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class CompositeViewEngine : ICompositeViewEngine
|
||||
{
|
||||
public CompositeViewEngine(IViewEngineProvider viewEngineProvider)
|
||||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="CompositeViewEngine"/>.
|
||||
/// </summary>
|
||||
/// <param name="optionsAccessor">The options accessor for <see cref="MvcOptions"/>.</param>
|
||||
/// <param name="typeActivatorCache">As <see cref="ITypeActivatorCache"/> instance that creates
|
||||
/// an instance of type <see cref="IViewEngine"/>.</param>
|
||||
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
|
||||
/// service collection.</param>
|
||||
public CompositeViewEngine(
|
||||
IOptions<MvcOptions> optionsAccessor,
|
||||
ITypeActivatorCache typeActivatorCache,
|
||||
IServiceProvider serviceProvider)
|
||||
{
|
||||
ViewEngines = viewEngineProvider.ViewEngines;
|
||||
var viewEngines = new List<IViewEngine>();
|
||||
foreach (var descriptor in optionsAccessor.Options.ViewEngines)
|
||||
{
|
||||
IViewEngine viewEngine;
|
||||
if (descriptor.ViewEngine != null)
|
||||
{
|
||||
viewEngine = descriptor.ViewEngine;
|
||||
}
|
||||
else
|
||||
{
|
||||
viewEngine = typeActivatorCache.CreateInstance<IViewEngine>(
|
||||
serviceProvider,
|
||||
descriptor.ViewEngineType);
|
||||
}
|
||||
|
||||
viewEngines.Add(viewEngine);
|
||||
}
|
||||
|
||||
ViewEngines = viewEngines;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of ViewEngines the CompositeViewEngine delegates to.
|
||||
/// Gets the list of <see cref="IViewEngine"/> this instance of <see cref="CompositeViewEngine"/> delegates to.
|
||||
/// </summary>
|
||||
public IReadOnlyList<IViewEngine> ViewEngines { get; private set; }
|
||||
public IReadOnlyList<IViewEngine> ViewEngines { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public ViewEngineResult FindPartialView([NotNull] ActionContext context,
|
||||
|
|
|
|||
|
|
@ -1,18 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Rendering
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an activated collection of <see cref="IViewEngine"/> instances.
|
||||
/// </summary>
|
||||
public interface IViewEngineProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a collection of activated IViewEngine instances.
|
||||
/// </summary>
|
||||
IReadOnlyList<IViewEngine> ViewEngines { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -2,23 +2,30 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Microsoft.AspNet.Mvc.Core;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Encapsulates information that describes an <see cref="IViewEngine"/>.
|
||||
/// </summary>
|
||||
public class ViewEngineDescriptor : OptionDescriptor<IViewEngine>
|
||||
public class ViewEngineDescriptor
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ViewEngineDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">The <see cref="IViewEngine"/> type that the descriptor represents.</param>
|
||||
public ViewEngineDescriptor([NotNull] Type type)
|
||||
: base(type)
|
||||
{
|
||||
if (!typeof(IViewEngine).IsAssignableFrom(type))
|
||||
{
|
||||
var message = Resources.FormatTypeMustDeriveFromType(type.FullName, typeof(IViewEngine).FullName);
|
||||
throw new ArgumentException(message, nameof(type));
|
||||
}
|
||||
|
||||
ViewEngineType = type;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -26,8 +33,19 @@ namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
|||
/// </summary>
|
||||
/// <param name="viewEngine">An instance of <see cref="IViewEngine"/> that the descriptor represents.</param>
|
||||
public ViewEngineDescriptor([NotNull] IViewEngine viewEngine)
|
||||
: base(viewEngine)
|
||||
{
|
||||
ViewEngine = viewEngine;
|
||||
ViewEngineType = viewEngine.GetType();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of the <see cref="IViewEngine"/> described by this <see cref="ViewEngineDescriptor"/>.
|
||||
/// </summary>
|
||||
public Type ViewEngineType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IViewEngine"/> instance described by this <see cref="ViewEngineDescriptor"/>.
|
||||
/// </summary>
|
||||
public IViewEngine ViewEngine { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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 System.Linq;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for adding view engines to a descriptor collection.
|
||||
/// </summary>
|
||||
public static class ViewEngineDescriptorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a type representing a <see cref="IViewEngine"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ViewEngineDescriptors</param>
|
||||
/// <param name="viewEngineType">Type representing an <see cref="IViewEngine"/>.</param>
|
||||
/// <returns>ViewEngineDescriptor representing the added instance.</returns>
|
||||
public static ViewEngineDescriptor Add(
|
||||
[NotNull] this IList<ViewEngineDescriptor> descriptors,
|
||||
[NotNull] Type viewEngineType)
|
||||
{
|
||||
var descriptor = new ViewEngineDescriptor(viewEngineType);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts a type representing a <see cref="IViewEngine"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ViewEngineDescriptors</param>
|
||||
/// <param name="viewEngineType">Type representing an <see cref="IViewEngine"/>.</param>
|
||||
/// <returns>ViewEngineDescriptor representing the inserted instance.</returns>
|
||||
public static ViewEngineDescriptor Insert(
|
||||
[NotNull] this IList<ViewEngineDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] Type viewEngineType)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
|
||||
var descriptor = new ViewEngineDescriptor(viewEngineType);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an <see cref="IViewEngine"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ViewEngineDescriptors</param>
|
||||
/// <param name="viewEngine">An <see cref="IViewEngine"/> instance.</param>
|
||||
/// <returns>ViewEngineDescriptor representing the added instance.</returns>
|
||||
public static ViewEngineDescriptor Add(
|
||||
[NotNull] this IList<ViewEngineDescriptor> descriptors,
|
||||
[NotNull] IViewEngine viewEngine)
|
||||
{
|
||||
var descriptor = new ViewEngineDescriptor(viewEngine);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Insert an <see cref="IViewEngine"/> to a descriptor collection.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of ViewEngineDescriptors</param>
|
||||
/// <param name="viewEngine">An <see cref="IViewEngine"/> instance.</param>
|
||||
/// <returns>ViewEngineDescriptor representing the added instance.</returns>
|
||||
public static ViewEngineDescriptor Insert(
|
||||
[NotNull] this IList<ViewEngineDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] IViewEngine viewEngine)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
|
||||
var descriptor = new ViewEngineDescriptor(viewEngine);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the only instance of <typeparamref name="TInstance"/> from <paramref name="descriptors" />.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInstance">The type of the instance to find.</typeparam>
|
||||
/// <param name="descriptors">The <see cref="IList{ViewEngineDescriptor}"/> to search.</param>
|
||||
/// <returns>The only instance of <typeparamref name="TInstance"/> in <paramref name="descriptors"/>.</returns>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// Thrown if there is not exactly one <typeparamref name="TInstance"/> in <paramref name="descriptors" />.
|
||||
/// </exception>
|
||||
public static TInstance InstanceOf<TInstance>(
|
||||
[NotNull] this IList<ViewEngineDescriptor> descriptors)
|
||||
{
|
||||
return descriptors
|
||||
.Select(descriptor => descriptor.ViewEngine)
|
||||
.OfType<TInstance>()
|
||||
.Single();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the only instance of <typeparamref name="TInstance"/> from <paramref name="descriptors" />
|
||||
/// or <c>null</c> if the sequence is empty.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInstance">The type of the instance to find.</typeparam>
|
||||
/// <param name="descriptors">The <see cref="IList{ViewEngineDescriptor}"/> to search.</param>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// Thrown if there is more than one <typeparamref name="TInstance"/> in <paramref name="descriptors" />.
|
||||
/// </exception>
|
||||
public static TInstance InstanceOfOrDefault<TInstance>(
|
||||
[NotNull] this IList<ViewEngineDescriptor> descriptors)
|
||||
{
|
||||
return descriptors
|
||||
.Select(descriptor => descriptor.ViewEngine)
|
||||
.OfType<TInstance>()
|
||||
.SingleOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all instances of <typeparamref name="TInstance"/> from <paramref name="descriptors" />.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInstance">The type of the instances to find.</typeparam>
|
||||
/// <param name="descriptors">The <see cref="IList{ViewEngineDescriptor}"/> to search.</param>
|
||||
/// <returns>An IEnumerable of <typeparamref name="TInstance"/> that contains instances from
|
||||
/// <paramref name="descriptors"/>.</returns>
|
||||
public static IEnumerable<TInstance> InstancesOf<TInstance>(
|
||||
[NotNull] this IList<ViewEngineDescriptor> descriptors)
|
||||
{
|
||||
return descriptors
|
||||
.Select(descriptor => descriptor.ViewEngine)
|
||||
.OfType<TInstance>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,14 +6,12 @@ using System;
|
|||
namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a <see cref="Type"/> which implements <see cref="IModelBinder"/> or
|
||||
/// <see cref="IModelBinderProvider"/>.
|
||||
/// Provides a <see cref="Type"/> which implements <see cref="IModelBinder"/>.
|
||||
/// </summary>
|
||||
public interface IBinderTypeProviderMetadata : IBindingSourceMetadata
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="Type"/> which implements either <see cref="IModelBinder"/> or
|
||||
/// <see cref="IModelBinderProvider"/>.
|
||||
/// A <see cref="Type"/> which implements either <see cref="IModelBinder"/>.
|
||||
/// </summary>
|
||||
Type BinderType { get; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,7 @@ using Microsoft.AspNet.Mvc.ModelBinding;
|
|||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// An attribute that can specify a model name or type of <see cref="IModelBinder"/> or
|
||||
/// <see cref="IModelBinderProvider"/> to use for binding.
|
||||
/// An attribute that can specify a model name or type of <see cref="IModelBinder"/> to use for binding.
|
||||
/// </summary>
|
||||
[AttributeUsage(
|
||||
|
||||
|
|
@ -41,14 +40,12 @@ namespace Microsoft.AspNet.Mvc
|
|||
{
|
||||
if (value != null)
|
||||
{
|
||||
if (!typeof(IModelBinder).IsAssignableFrom(value) &&
|
||||
!typeof(IModelBinderProvider).IsAssignableFrom(value))
|
||||
if (!typeof(IModelBinder).IsAssignableFrom(value))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
Resources.FormatBinderType_MustBeIModelBinderOrIModelBinderProvider(
|
||||
Resources.FormatBinderType_MustBeIModelBinder(
|
||||
value.FullName,
|
||||
typeof(IModelBinder).FullName,
|
||||
typeof(IModelBinderProvider).FullName));
|
||||
typeof(IModelBinder).FullName));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,16 +11,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
|||
/// <summary>
|
||||
/// An <see cref="IModelBinder"/> which can bind a model based on the value of
|
||||
/// <see cref="ModelMetadata.BinderType"/>. The supplied <see cref="IModelBinder"/>
|
||||
/// type or <see cref="IModelBinderProvider"/> type will be used to bind the model.
|
||||
/// type will be used to bind the model.
|
||||
/// </summary>
|
||||
public class BinderTypeBasedModelBinder : IModelBinder
|
||||
{
|
||||
private readonly Func<Type, ObjectFactory> _createFactory =
|
||||
(t) => ActivatorUtilities.CreateFactory(t, Type.EmptyTypes);
|
||||
private ConcurrentDictionary<Type, ObjectFactory> _typeActivatorCache =
|
||||
private readonly ConcurrentDictionary<Type, ObjectFactory> _typeActivatorCache =
|
||||
new ConcurrentDictionary<Type, ObjectFactory>();
|
||||
|
||||
|
||||
public async Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext)
|
||||
{
|
||||
if (bindingContext.BinderType == null)
|
||||
|
|
@ -37,19 +36,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
|||
var modelBinder = instance as IModelBinder;
|
||||
if (modelBinder == null)
|
||||
{
|
||||
var modelBinderProvider = instance as IModelBinderProvider;
|
||||
if (modelBinderProvider != null)
|
||||
{
|
||||
modelBinder = new CompositeModelBinder(modelBinderProvider.ModelBinders);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
Resources.FormatBinderType_MustBeIModelBinderOrIModelBinderProvider(
|
||||
bindingContext.BinderType.FullName,
|
||||
typeof(IModelBinder).FullName,
|
||||
typeof(IModelBinderProvider).FullName));
|
||||
}
|
||||
throw new InvalidOperationException(
|
||||
Resources.FormatBinderType_MustBeIModelBinder(
|
||||
bindingContext.BinderType.FullName,
|
||||
typeof(IModelBinder).FullName));
|
||||
}
|
||||
|
||||
var result = await modelBinder.BindModelAsync(bindingContext);
|
||||
|
|
|
|||
|
|
@ -1,18 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an activated collection of <see cref="IModelBinder"/> instances.
|
||||
/// </summary>
|
||||
public interface IModelBinderProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a collection of activated ModelBinders instances.
|
||||
/// </summary>
|
||||
IReadOnlyList<IModelBinder> ModelBinders { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -76,8 +76,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
|||
public abstract string BinderModelName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="Type"/> of an <see cref="IModelBinder"/> or an
|
||||
/// <see cref="IModelBinderProvider"/> of a model if specified explicitly using
|
||||
/// Gets the <see cref="Type"/> of an <see cref="IModelBinder"/> of a model if specified explicitly using
|
||||
/// <see cref="IBinderTypeProviderMetadata"/>.
|
||||
/// </summary>
|
||||
public abstract Type BinderType { get; }
|
||||
|
|
|
|||
|
|
@ -459,19 +459,19 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// The type '{0}' must implement either '{1}' or '{2}' to be used as a model binder.
|
||||
/// The type '{0}' must implement '{1}' to be used as a model binder.
|
||||
/// </summary>
|
||||
internal static string BinderType_MustBeIModelBinderOrIModelBinderProvider
|
||||
internal static string BinderType_MustBeIModelBinder
|
||||
{
|
||||
get { return GetString("BinderType_MustBeIModelBinderOrIModelBinderProvider"); }
|
||||
get { return GetString("BinderType_MustBeIModelBinder"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The type '{0}' must implement either '{1}' or '{2}' to be used as a model binder.
|
||||
/// The type '{0}' must implement '{1}' to be used as a model binder.
|
||||
/// </summary>
|
||||
internal static string FormatBinderType_MustBeIModelBinderOrIModelBinderProvider(object p0, object p1, object p2)
|
||||
internal static string FormatBinderType_MustBeIModelBinder(object p0, object p1)
|
||||
{
|
||||
return string.Format(CultureInfo.CurrentCulture, GetString("BinderType_MustBeIModelBinderOrIModelBinderProvider"), p0, p1, p2);
|
||||
return string.Format(CultureInfo.CurrentCulture, GetString("BinderType_MustBeIModelBinder"), p0, p1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -201,8 +201,8 @@
|
|||
<data name="ModelStateDictionary_MaxModelStateErrors" xml:space="preserve">
|
||||
<value>The maximum number of allowed model errors has been reached.</value>
|
||||
</data>
|
||||
<data name="BinderType_MustBeIModelBinderOrIModelBinderProvider" xml:space="preserve">
|
||||
<value>The type '{0}' must implement either '{1}' or '{2}' to be used as a model binder.</value>
|
||||
<data name="BinderType_MustBeIModelBinder" xml:space="preserve">
|
||||
<value>The type '{0}' must implement '{1}' to be used as a model binder.</value>
|
||||
</data>
|
||||
<data name="ModelBinderUtil_ValueInvalid" xml:space="preserve">
|
||||
<value>The value '{0}' is not valid for {1}.</value>
|
||||
|
|
|
|||
|
|
@ -17,15 +17,21 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
|
|||
/// </summary>
|
||||
public class DefaultObjectValidator : IObjectModelValidator
|
||||
{
|
||||
private readonly IValidationExcludeFiltersProvider _excludeFilterProvider;
|
||||
private readonly IList<IExcludeTypeValidationFilter> _excludeFilters;
|
||||
private readonly IModelMetadataProvider _modelMetadataProvider;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="DefaultObjectValidator"/>.
|
||||
/// </summary>
|
||||
/// <param name="excludeFilters"><see cref="IExcludeTypeValidationFilter"/>s that determine
|
||||
/// types to exclude from validation.</param>
|
||||
/// <param name="modelMetadataProvider">The <see cref="IModelMetadataProvider"/>.</param>
|
||||
public DefaultObjectValidator(
|
||||
IValidationExcludeFiltersProvider excludeFilterProvider,
|
||||
IModelMetadataProvider modelMetadataProvider)
|
||||
[NotNull] IList<IExcludeTypeValidationFilter> excludeFilters,
|
||||
[NotNull] IModelMetadataProvider modelMetadataProvider)
|
||||
{
|
||||
_excludeFilterProvider = excludeFilterProvider;
|
||||
_modelMetadataProvider = modelMetadataProvider;
|
||||
_excludeFilters = excludeFilters;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
@ -39,13 +45,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
|
|||
|
||||
ValidateNonVisitedNodeAndChildren(
|
||||
modelValidationContext.RootPrefix,
|
||||
validationContext,
|
||||
validationContext,
|
||||
validators: null);
|
||||
}
|
||||
|
||||
private bool ValidateNonVisitedNodeAndChildren(
|
||||
string modelKey,
|
||||
ValidationContext validationContext,
|
||||
ValidationContext validationContext,
|
||||
IList<IModelValidator> validators)
|
||||
{
|
||||
var modelValidationContext = validationContext.ModelValidationContext;
|
||||
|
|
@ -98,7 +104,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
|
|||
|
||||
// We don't need to recursively traverse the graph for types that shouldn't be validated
|
||||
var modelType = modelExplorer.Model.GetType();
|
||||
if (IsTypeExcludedFromValidation(_excludeFilterProvider.ExcludeFilters, modelType))
|
||||
if (IsTypeExcludedFromValidation(_excludeFilters, modelType))
|
||||
{
|
||||
var result = ShallowValidate(modelKey, modelExplorer, validationContext, validators);
|
||||
MarkPropertiesAsSkipped(modelKey, modelExplorer.Metadata, validationContext);
|
||||
|
|
@ -146,7 +152,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
|
|||
if (fieldValidationState != ModelValidationState.Unvalidated)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var childMetadata in metadata.Properties)
|
||||
{
|
||||
|
|
@ -182,7 +188,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
|
|||
var propertyBindingName = propertyMetadata.BinderModelName ?? propertyMetadata.PropertyName;
|
||||
var childKey = ModelBindingHelper.CreatePropertyModelName(currentModelKey, propertyBindingName);
|
||||
if (!ValidateNonVisitedNodeAndChildren(
|
||||
childKey,
|
||||
childKey,
|
||||
propertyValidationContext,
|
||||
validators: null))
|
||||
{
|
||||
|
|
@ -300,14 +306,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
|
|||
return isValid;
|
||||
}
|
||||
|
||||
private bool IsTypeExcludedFromValidation(IReadOnlyList<IExcludeTypeValidationFilter> filters, Type type)
|
||||
private bool IsTypeExcludedFromValidation(IList<IExcludeTypeValidationFilter> filters, Type type)
|
||||
{
|
||||
// This can be set to null in ModelBinding scenarios which does not flow through this path.
|
||||
if (filters == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return filters.Any(filter => filter.IsTypeExcluded(type));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,10 +2,9 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an implementation of <see cref="IExcludeTypeValidationFilter"/> which can filter
|
||||
|
|
@ -2,10 +2,9 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an implementation of <see cref="IExcludeTypeValidationFilter"/> which can filter
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an activated collection of <see cref="IModelValidatorProvider"/> instances.
|
||||
/// </summary>
|
||||
public interface IModelValidatorProviderProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a collection of activated <see cref="IModelValidatorProvider"/> instances.
|
||||
/// </summary>
|
||||
IReadOnlyList<IModelValidatorProvider> ModelValidatorProviders { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an activated collection of <see cref="IExcludeTypeValidationFilter"/> instances.
|
||||
/// </summary>
|
||||
public interface IValidationExcludeFiltersProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a collection of activated <see cref="IExcludeTypeValidationFilter"/> instances.
|
||||
/// </summary>
|
||||
IReadOnlyList<IExcludeTypeValidationFilter> ExcludeFilters { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class CompositeValueProviderFactory : ICompositeValueProviderFactory
|
||||
{
|
||||
private readonly IReadOnlyList<IValueProviderFactory> _valueProviderFactories;
|
||||
|
||||
public CompositeValueProviderFactory(IValueProviderFactoryProvider valueProviderFactoryProvider)
|
||||
{
|
||||
_valueProviderFactories = valueProviderFactoryProvider.ValueProviderFactories;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IValueProvider GetValueProvider([NotNull] ValueProviderFactoryContext context)
|
||||
{
|
||||
var valueProviders = _valueProviderFactories.Select(factory => factory.GetValueProvider(context))
|
||||
.Where(vp => vp != null);
|
||||
|
||||
return new CompositeValueProvider(valueProviders);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an activated collection of <see cref="IValueProviderFactory"/> instances.
|
||||
/// </summary>
|
||||
public interface IValueProviderFactoryProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a collection of activated IValueProviderFactory instances.
|
||||
/// </summary>
|
||||
IReadOnlyList<IValueProviderFactory> ValueProviderFactories { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.OptionDescriptors;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor.OptionDescriptors
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class DefaultViewLocationExpanderProvider :
|
||||
OptionDescriptorBasedProvider<IViewLocationExpander>, IViewLocationExpanderProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DefaultViewLocationExpanderProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="options">An accessor to the <see cref="MvcOptions"/> configured for this application.</param>
|
||||
/// <param name="typeActivatorCache">A <see cref="ITypeActivatorCache"/> instance that creates a new instance
|
||||
/// of type <see cref="IViewLocationExpander"/>.</param>
|
||||
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
|
||||
/// service collection.</param>
|
||||
public DefaultViewLocationExpanderProvider(
|
||||
IOptions<RazorViewEngineOptions> optionsAccessor,
|
||||
ITypeActivatorCache typeActivatorCache,
|
||||
IServiceProvider serviceProvider)
|
||||
: base(optionsAccessor.Options.ViewLocationExpanders, typeActivatorCache, serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<IViewLocationExpander> ViewLocationExpanders
|
||||
{
|
||||
get { return Options; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor.OptionDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an activated collection of <see cref="IViewLocationExpander"/> instances.
|
||||
/// </summary>
|
||||
public interface IViewLocationExpanderProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a collection of activated <see cref="IViewLocationExpander"/> instances.
|
||||
/// </summary>
|
||||
IReadOnlyList<IViewLocationExpander> ViewLocationExpanders { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Mvc.OptionDescriptors;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor.OptionDescriptors
|
||||
{
|
||||
/// <summary>
|
||||
/// Encapsulates information that describes an <see cref="IViewLocationExpander"/>.
|
||||
/// </summary>
|
||||
public class ViewLocationExpanderDescriptor : OptionDescriptor<IViewLocationExpander>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ViewLocationExpanderDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">A <see cref="IViewLocationExpander"/> type that the descriptor represents.
|
||||
/// </param>
|
||||
public ViewLocationExpanderDescriptor([NotNull] Type type)
|
||||
: base(type)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ViewLocationExpanderDescriptor"/>.
|
||||
/// </summary>
|
||||
/// <param name="viewLocationExpander">An instance of <see cref="IViewLocationExpander"/>
|
||||
/// that the descriptor represents.</param>
|
||||
public ViewLocationExpanderDescriptor([NotNull] IViewLocationExpander viewLocationExpander)
|
||||
: base(viewLocationExpander)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Razor.OptionDescriptors;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for adding view location expanders to a collection.
|
||||
/// </summary>
|
||||
public static class ViewLocationExpanderDescriptorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a type representing a <see cref="IViewLocationExpander"/> to <paramref name="descriptors"/>.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of <see cref="ViewLocationExpanderDescriptor"/>.</param>
|
||||
/// <param name="viewLocationExpanderType">Type representing an <see cref="IViewLocationExpander"/></param>
|
||||
/// <returns>A <see cref="ViewLocationExpanderDescriptor"/> representing the added instance.</returns>
|
||||
public static ViewLocationExpanderDescriptor Add(
|
||||
[NotNull] this IList<ViewLocationExpanderDescriptor> descriptors,
|
||||
[NotNull] Type viewLocationExpanderType)
|
||||
{
|
||||
var descriptor = new ViewLocationExpanderDescriptor(viewLocationExpanderType);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts a type representing a <see cref="IViewLocationExpander"/> in to <paramref name="descriptors"/> at
|
||||
/// the specified <paramref name="index"/>.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of <see cref="ViewLocationExpanderDescriptor"/>.</param>
|
||||
/// <param name="index">The zero-based index at which <paramref name="viewLocationExpanderType"/>
|
||||
/// should be inserted.</param>
|
||||
/// <param name="viewLocationExpanderType">Type representing an <see cref="IViewLocationExpander"/></param>
|
||||
/// <returns>A <see cref="ViewLocationExpanderDescriptor"/> representing the inserted instance.</returns>
|
||||
public static ViewLocationExpanderDescriptor Insert(
|
||||
[NotNull] this IList<ViewLocationExpanderDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] Type viewLocationExpanderType)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
|
||||
var descriptor = new ViewLocationExpanderDescriptor(viewLocationExpanderType);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an <see cref="IViewLocationExpander"/> to <paramref name="descriptors"/>.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of <see cref="ViewLocationExpanderDescriptor"/>.</param>
|
||||
/// <param name="viewLocationExpander">An <see cref="IViewLocationExpander"/> instance.</param>
|
||||
/// <returns>A <see cref="ViewLocationExpanderDescriptor"/> representing the added instance.</returns>
|
||||
public static ViewLocationExpanderDescriptor Add(
|
||||
[NotNull] this IList<ViewLocationExpanderDescriptor> descriptors,
|
||||
[NotNull] IViewLocationExpander viewLocationExpander)
|
||||
{
|
||||
var descriptor = new ViewLocationExpanderDescriptor(viewLocationExpander);
|
||||
descriptors.Add(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Insert an <see cref="IViewLocationExpander"/> in to <paramref name="descriptors"/> at the specified
|
||||
/// <paramref name="index"/>.
|
||||
/// </summary>
|
||||
/// <param name="descriptors">A list of <see cref="ViewLocationExpanderDescriptor"/>.</param>
|
||||
/// <param name="index">The zero-based index at which <paramref name="viewLocationExpander"/>
|
||||
/// should be inserted.</param>
|
||||
/// <param name="viewLocationExpander">An <see cref="IViewLocationExpander"/> instance.</param>
|
||||
/// <returns>A <see cref="ViewLocationExpanderDescriptor"/> representing the added instance.</returns>
|
||||
public static ViewLocationExpanderDescriptor Insert(
|
||||
[NotNull] this IList<ViewLocationExpanderDescriptor> descriptors,
|
||||
int index,
|
||||
[NotNull] IViewLocationExpander viewLocationExpander)
|
||||
{
|
||||
if (index < 0 || index > descriptors.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
|
||||
var descriptor = new ViewLocationExpanderDescriptor(viewLocationExpander);
|
||||
descriptors.Insert(index, descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -219,7 +219,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// {0} has not been called for the page '{1}'.
|
||||
/// {0} has not been called for the page at '{1}'.
|
||||
/// </summary>
|
||||
internal static string RenderBodyNotCalled
|
||||
{
|
||||
|
|
@ -227,7 +227,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// {0} has not been called for the page '{1}'.
|
||||
/// {0} has not been called for the page at '{1}'.
|
||||
/// </summary>
|
||||
internal static string FormatRenderBodyNotCalled(object p0, object p1)
|
||||
{
|
||||
|
|
@ -283,7 +283,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// The following sections have been defined but have not been rendered for the page '{0}': '{1}'.
|
||||
/// The following sections have been defined but have not been rendered by the page at '{0}': '{1}'.
|
||||
/// </summary>
|
||||
internal static string SectionsNotRendered
|
||||
{
|
||||
|
|
@ -291,7 +291,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// The following sections have been defined but have not been rendered for the page '{0}': '{1}'.
|
||||
/// The following sections have been defined but have not been rendered by the page at '{0}': '{1}'.
|
||||
/// </summary>
|
||||
internal static string FormatSectionsNotRendered(object p0, object p1)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,10 +6,9 @@ using System.Collections.Generic;
|
|||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNet.Mvc.Razor.Internal;
|
||||
using Microsoft.AspNet.Mvc.Razor.OptionDescriptors;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor
|
||||
{
|
||||
|
|
@ -41,7 +40,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
|
||||
private readonly IRazorPageFactory _pageFactory;
|
||||
private readonly IRazorViewFactory _viewFactory;
|
||||
private readonly IReadOnlyList<IViewLocationExpander> _viewLocationExpanders;
|
||||
private readonly IList<IViewLocationExpander> _viewLocationExpanders;
|
||||
private readonly IViewLocationCache _viewLocationCache;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -50,12 +49,12 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
/// <param name="pageFactory">The page factory used for creating <see cref="IRazorPage"/> instances.</param>
|
||||
public RazorViewEngine(IRazorPageFactory pageFactory,
|
||||
IRazorViewFactory viewFactory,
|
||||
IViewLocationExpanderProvider viewLocationExpanderProvider,
|
||||
IOptions<RazorViewEngineOptions> optionsAccessor,
|
||||
IViewLocationCache viewLocationCache)
|
||||
{
|
||||
_pageFactory = pageFactory;
|
||||
_viewFactory = viewFactory;
|
||||
_viewLocationExpanders = viewLocationExpanderProvider.ViewLocationExpanders;
|
||||
_viewLocationExpanders = optionsAccessor.Options.ViewLocationExpanders;
|
||||
_viewLocationCache = viewLocationCache;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,23 +4,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.FileProviders;
|
||||
using Microsoft.AspNet.Mvc.Razor.OptionDescriptors;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides programmatic configuration for the default <see cref="Rendering.IViewEngine"/>.
|
||||
/// Provides programmatic configuration for the <see cref="RazorViewEngine"/>.
|
||||
/// </summary>
|
||||
public class RazorViewEngineOptions
|
||||
{
|
||||
private IFileProvider _fileProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Get a <see cref="IList{T}"/> of descriptors for <see cref="IViewLocationExpander" />s used by this
|
||||
/// application.
|
||||
/// Get a <see cref="IList{IViewLocationExpander}"/> used by the <see cref="RazorViewEngine"/>.
|
||||
/// </summary>
|
||||
public IList<ViewLocationExpanderDescriptor> ViewLocationExpanders { get; }
|
||||
= new List<ViewLocationExpanderDescriptor>();
|
||||
public IList<IViewLocationExpander> ViewLocationExpanders { get; }
|
||||
= new List<IViewLocationExpander>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="IFileProvider" /> used by <see cref="RazorViewEngine"/> to locate Razor files on
|
||||
|
|
@ -32,7 +32,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
// Set up ModelBinding
|
||||
options.ModelBinders.Add(new BinderTypeBasedModelBinder());
|
||||
options.ModelBinders.Add(new ServicesModelBinder());
|
||||
options.ModelBinders.Add(typeof(BodyModelBinder));
|
||||
options.ModelBinders.Add(new BodyModelBinder());
|
||||
options.ModelBinders.Add(new HeaderModelBinder());
|
||||
options.ModelBinders.Add(new TypeConverterModelBinder());
|
||||
options.ModelBinders.Add(new TypeMatchModelBinder());
|
||||
|
|
|
|||
|
|
@ -10,11 +10,9 @@ using Microsoft.AspNet.Mvc.Internal;
|
|||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Metadata;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Microsoft.AspNet.Mvc.Razor;
|
||||
using Microsoft.AspNet.Mvc.Razor.Compilation;
|
||||
using Microsoft.AspNet.Mvc.Razor.Directives;
|
||||
using Microsoft.AspNet.Mvc.Razor.OptionDescriptors;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.AspNet.Mvc.Routing;
|
||||
using Microsoft.AspNet.Mvc.ViewComponents;
|
||||
|
|
@ -63,7 +61,12 @@ namespace Microsoft.AspNet.Mvc
|
|||
services.AddSingleton<IActionSelectorDecisionTreeProvider, ActionSelectorDecisionTreeProvider>();
|
||||
services.AddSingleton<IActionSelector, DefaultActionSelector>();
|
||||
services.AddTransient<IControllerActionArgumentBinder, DefaultControllerActionArgumentBinder>();
|
||||
services.AddTransient<IObjectModelValidator, DefaultObjectValidator>();
|
||||
services.AddTransient<IObjectModelValidator>(serviceProvider =>
|
||||
{
|
||||
var options = serviceProvider.GetRequiredService<IOptions<MvcOptions>>().Options;
|
||||
var modelMetadataProvider = serviceProvider.GetRequiredService<IModelMetadataProvider>();
|
||||
return new DefaultObjectValidator(options.ValidationExcludeFilters, modelMetadataProvider);
|
||||
});
|
||||
|
||||
services.AddTransient<IActionDescriptorProvider, ControllerActionDescriptorProvider>();
|
||||
|
||||
|
|
@ -90,24 +93,14 @@ namespace Microsoft.AspNet.Mvc
|
|||
});
|
||||
|
||||
services.AddTransient<IInputFormatterSelector, DefaultInputFormatterSelector>();
|
||||
services.AddScoped<IInputFormattersProvider, DefaultInputFormattersProvider>();
|
||||
|
||||
services.AddTransient<IModelBinderProvider, DefaultModelBindersProvider>();
|
||||
services.AddTransient<IValueProviderFactoryProvider, DefaultValueProviderFactoryProvider>();
|
||||
services.AddTransient<IOutputFormattersProvider, DefaultOutputFormattersProvider>();
|
||||
services.AddInstance(new JsonOutputFormatter());
|
||||
|
||||
services.AddTransient<IModelValidatorProviderProvider, DefaultModelValidatorProviderProvider>();
|
||||
services.AddTransient<IValidationExcludeFiltersProvider, DefaultValidationExcludeFiltersProvider>();
|
||||
|
||||
// Razor, Views and runtime compilation
|
||||
|
||||
// The provider is inexpensive to initialize and provides ViewEngines that may require request
|
||||
// specific services.
|
||||
services.AddScoped<ICompositeViewEngine, CompositeViewEngine>();
|
||||
services.AddTransient<IViewEngineProvider, DefaultViewEngineProvider>();
|
||||
// Transient since the IViewLocationExpanders returned by the instance is cached by view engines.
|
||||
services.AddTransient<IViewLocationExpanderProvider, DefaultViewLocationExpanderProvider>();
|
||||
|
||||
// Caches view locations that are valid for the lifetime of the application.
|
||||
services.AddSingleton<IViewLocationCache, DefaultViewLocationCache>();
|
||||
services.AddSingleton<ICodeTreeCache>(serviceProvider =>
|
||||
|
|
|
|||
|
|
@ -92,13 +92,13 @@ namespace Microsoft.AspNet.Mvc
|
|||
httpContext.Request.PathBase = new PathString("");
|
||||
httpContext.Response.Body = new MemoryStream();
|
||||
var services = new Mock<IServiceProvider>();
|
||||
services.Setup(p => p.GetService(typeof(IOutputFormattersProvider))).Returns(new TestOutputFormatterProvider());
|
||||
httpContext.RequestServices = services.Object;
|
||||
var options = new Mock<IOptions<MvcOptions>>();
|
||||
options.SetupGet(o => o.Options)
|
||||
.Returns(new MvcOptions());
|
||||
|
||||
var optionsAccessor = new MockMvcOptionsAccessor();
|
||||
optionsAccessor.Options.OutputFormatters.Add(new StringOutputFormatter());
|
||||
optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter());
|
||||
services.Setup(p => p.GetService(typeof(IOptions<MvcOptions>)))
|
||||
.Returns(options.Object);
|
||||
.Returns(optionsAccessor);
|
||||
|
||||
return httpContext;
|
||||
}
|
||||
|
|
@ -110,20 +110,5 @@ namespace Microsoft.AspNet.Mvc
|
|||
|
||||
return urlHelper.Object;
|
||||
}
|
||||
|
||||
private class TestOutputFormatterProvider : IOutputFormattersProvider
|
||||
{
|
||||
public IReadOnlyList<IOutputFormatter> OutputFormatters
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<IOutputFormatter>()
|
||||
{
|
||||
new StringOutputFormatter(),
|
||||
new JsonOutputFormatter()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -99,15 +99,13 @@ namespace Microsoft.AspNet.Mvc
|
|||
|
||||
httpContext.Setup(o => o.Request)
|
||||
.Returns(request);
|
||||
var options = new Mock<IOptions<MvcOptions>>();
|
||||
options.SetupGet(o => o.Options)
|
||||
.Returns(new MvcOptions());
|
||||
var optionsAccessor = new MockMvcOptionsAccessor();
|
||||
optionsAccessor.Options.OutputFormatters.Add(new StringOutputFormatter());
|
||||
optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter());
|
||||
httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions<MvcOptions>)))
|
||||
.Returns(options.Object);
|
||||
.Returns(optionsAccessor);
|
||||
httpContext.Setup(o => o.Response)
|
||||
.Returns(response);
|
||||
httpContext.Setup(o => o.RequestServices.GetService(typeof(IOutputFormattersProvider)))
|
||||
.Returns(new TestOutputFormatterProvider());
|
||||
|
||||
return httpContext.Object;
|
||||
}
|
||||
|
|
@ -119,20 +117,5 @@ namespace Microsoft.AspNet.Mvc
|
|||
|
||||
return urlHelper.Object;
|
||||
}
|
||||
|
||||
private class TestOutputFormatterProvider : IOutputFormattersProvider
|
||||
{
|
||||
public IReadOnlyList<IOutputFormatter> OutputFormatters
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<IOutputFormatter>()
|
||||
{
|
||||
new StringOutputFormatter(),
|
||||
new JsonOutputFormatter()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -87,29 +87,12 @@ namespace Microsoft.AspNet.Mvc
|
|||
.Returns(request);
|
||||
httpContext.Setup(o => o.Response)
|
||||
.Returns(response);
|
||||
httpContext.Setup(o => o.RequestServices.GetService(typeof(IOutputFormattersProvider)))
|
||||
.Returns(new TestOutputFormatterProvider());
|
||||
var options = new Mock<IOptions<MvcOptions>>();
|
||||
options.SetupGet(o => o.Options)
|
||||
.Returns(new MvcOptions());
|
||||
httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions<MvcOptions>)))
|
||||
.Returns(options.Object);
|
||||
var optionsAccessor = new MockMvcOptionsAccessor();
|
||||
optionsAccessor.Options.OutputFormatters.Add(new StringOutputFormatter());
|
||||
optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter());
|
||||
httpContext.Setup(p => p.RequestServices.GetService(typeof(IOptions<MvcOptions>)))
|
||||
.Returns(optionsAccessor);
|
||||
return httpContext.Object;
|
||||
}
|
||||
|
||||
private class TestOutputFormatterProvider : IOutputFormattersProvider
|
||||
{
|
||||
public IReadOnlyList<IOutputFormatter> OutputFormatters
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<IOutputFormatter>()
|
||||
{
|
||||
new StringOutputFormatter(),
|
||||
new JsonOutputFormatter()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -71,7 +71,6 @@ namespace Microsoft.AspNet.Mvc
|
|||
string requestAcceptCharsetHeader = "",
|
||||
bool respectBrowserAcceptHeader = false)
|
||||
{
|
||||
var formatters = new IOutputFormatter[] { new StringOutputFormatter(), new JsonOutputFormatter() };
|
||||
var httpContext = new Mock<HttpContext>();
|
||||
if (response != null)
|
||||
{
|
||||
|
|
@ -89,17 +88,12 @@ namespace Microsoft.AspNet.Mvc
|
|||
|
||||
httpContext.Setup(o => o.Request).Returns(request);
|
||||
httpContext.Setup(o => o.RequestServices).Returns(GetServiceProvider());
|
||||
httpContext.Setup(o => o.RequestServices.GetService(typeof(IOutputFormattersProvider)))
|
||||
.Returns(new TestOutputFormatterProvider(formatters));
|
||||
|
||||
var options = new Mock<IOptions<MvcOptions>>();
|
||||
options.SetupGet(o => o.Options)
|
||||
.Returns(new MvcOptions()
|
||||
{
|
||||
RespectBrowserAcceptHeader = respectBrowserAcceptHeader
|
||||
});
|
||||
var optionsAccessor = new MockMvcOptionsAccessor();
|
||||
optionsAccessor.Options.OutputFormatters.Add(new StringOutputFormatter());
|
||||
optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter());
|
||||
optionsAccessor.Options.RespectBrowserAcceptHeader = respectBrowserAcceptHeader;
|
||||
httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions<MvcOptions>)))
|
||||
.Returns(options.Object);
|
||||
.Returns(optionsAccessor);
|
||||
|
||||
return new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());
|
||||
}
|
||||
|
|
@ -113,26 +107,8 @@ namespace Microsoft.AspNet.Mvc
|
|||
optionsAccessor.SetupGet(o => o.Options).Returns(options);
|
||||
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.AddInstance<IOptions<MvcOptions>>(optionsAccessor.Object);
|
||||
serviceCollection.AddInstance(optionsAccessor.Object);
|
||||
return serviceCollection.BuildServiceProvider();
|
||||
}
|
||||
|
||||
private class TestOutputFormatterProvider : IOutputFormattersProvider
|
||||
{
|
||||
private readonly IEnumerable<IOutputFormatter> _formatters;
|
||||
|
||||
public TestOutputFormatterProvider(IEnumerable<IOutputFormatter> formatters)
|
||||
{
|
||||
_formatters = formatters;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IOutputFormatter> OutputFormatters
|
||||
{
|
||||
get
|
||||
{
|
||||
return _formatters.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -866,17 +866,15 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
|
||||
httpContext.Setup(o => o.Request).Returns(request);
|
||||
httpContext.Setup(o => o.RequestServices).Returns(GetServiceProvider());
|
||||
httpContext.Setup(o => o.RequestServices.GetService(typeof(IOutputFormattersProvider)))
|
||||
.Returns(new TestOutputFormatterProvider(outputFormatters));
|
||||
|
||||
var options = new Mock<IOptions<MvcOptions>>();
|
||||
options.SetupGet(o => o.Options)
|
||||
.Returns(new MvcOptions()
|
||||
{
|
||||
RespectBrowserAcceptHeader = respectBrowserAcceptHeader
|
||||
});
|
||||
var optionsAccessor = new MockMvcOptionsAccessor();
|
||||
foreach (var formatter in outputFormatters)
|
||||
{
|
||||
optionsAccessor.Options.OutputFormatters.Add(formatter);
|
||||
}
|
||||
optionsAccessor.Options.RespectBrowserAcceptHeader = respectBrowserAcceptHeader;
|
||||
httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions<MvcOptions>)))
|
||||
.Returns(options.Object);
|
||||
.Returns(optionsAccessor);
|
||||
|
||||
return new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());
|
||||
}
|
||||
|
|
@ -912,7 +910,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
optionsAccessor.SetupGet(o => o.Options).Returns(options);
|
||||
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.AddInstance<IOptions<MvcOptions>>(optionsAccessor.Object);
|
||||
serviceCollection.AddInstance(optionsAccessor.Object);
|
||||
return serviceCollection.BuildServiceProvider();
|
||||
}
|
||||
|
||||
|
|
@ -929,24 +927,6 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
}
|
||||
}
|
||||
|
||||
private class TestOutputFormatterProvider : IOutputFormattersProvider
|
||||
{
|
||||
private readonly IEnumerable<IOutputFormatter> _formatters;
|
||||
|
||||
public TestOutputFormatterProvider(IEnumerable<IOutputFormatter> formatters)
|
||||
{
|
||||
_formatters = formatters;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IOutputFormatter> OutputFormatters
|
||||
{
|
||||
get
|
||||
{
|
||||
return _formatters.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Person
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ using System.Threading.Tasks;
|
|||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Http.Core;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
|
@ -27,13 +27,14 @@ namespace Microsoft.AspNet.Mvc
|
|||
mockInputFormatter.Setup(o => o.ReadAsync(It.IsAny<InputFormatterContext>()))
|
||||
.Returns(Task.FromResult<object>(new Person()))
|
||||
.Verifiable();
|
||||
var inputFormatter = mockInputFormatter.Object;
|
||||
|
||||
var provider = new TestModelMetadataProvider();
|
||||
provider.ForType<Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);
|
||||
|
||||
var bindingContext = GetBindingContext(typeof(Person), metadataProvider: provider);
|
||||
var bindingContext = GetBindingContext(typeof(Person), inputFormatter, metadataProvider: provider);
|
||||
|
||||
var binder = GetBodyBinder(mockInputFormatter.Object);
|
||||
var binder = new BodyModelBinder();
|
||||
|
||||
// Act
|
||||
var binderResult = await binder.BindModelAsync(bindingContext);
|
||||
|
|
@ -199,6 +200,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
{
|
||||
httpContext = new DefaultHttpContext();
|
||||
}
|
||||
UpdateServiceProvider(httpContext, inputFormatter);
|
||||
|
||||
if (metadataProvider == null)
|
||||
{
|
||||
|
|
@ -207,7 +209,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
|
||||
var operationBindingContext = new OperationBindingContext
|
||||
{
|
||||
ModelBinder = GetBodyBinder(httpContext, inputFormatter),
|
||||
ModelBinder = new BodyModelBinder(),
|
||||
MetadataProvider = metadataProvider,
|
||||
HttpContext = httpContext,
|
||||
};
|
||||
|
|
@ -225,14 +227,9 @@ namespace Microsoft.AspNet.Mvc
|
|||
return bindingContext;
|
||||
}
|
||||
|
||||
private static BodyModelBinder GetBodyBinder(IInputFormatter inputFormatter)
|
||||
private static void UpdateServiceProvider(HttpContext httpContext, IInputFormatter inputFormatter)
|
||||
{
|
||||
return GetBodyBinder(new DefaultHttpContext(), inputFormatter);
|
||||
}
|
||||
|
||||
private static BodyModelBinder GetBodyBinder(HttpContext httpContext, IInputFormatter inputFormatter)
|
||||
{
|
||||
var actionContext = CreateActionContext(httpContext);
|
||||
var serviceProvider = new ServiceCollection();
|
||||
var inputFormatterSelector = new Mock<IInputFormatterSelector>();
|
||||
inputFormatterSelector
|
||||
.Setup(o => o.SelectFormatter(
|
||||
|
|
@ -240,9 +237,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
It.IsAny<InputFormatterContext>()))
|
||||
.Returns(inputFormatter);
|
||||
|
||||
var bodyValidationPredicatesProvider = new Mock<IValidationExcludeFiltersProvider>();
|
||||
bodyValidationPredicatesProvider.SetupGet(o => o.ExcludeFilters)
|
||||
.Returns(new List<IExcludeTypeValidationFilter>());
|
||||
serviceProvider.AddInstance(inputFormatterSelector.Object);
|
||||
|
||||
var bindingContext = new ActionBindingContext()
|
||||
{
|
||||
|
|
@ -253,14 +248,10 @@ namespace Microsoft.AspNet.Mvc
|
|||
{
|
||||
Value = bindingContext,
|
||||
};
|
||||
serviceProvider.AddInstance<IScopedInstance<ActionBindingContext>>(bindingContextAccessor);
|
||||
serviceProvider.AddInstance(CreateActionContext(httpContext));
|
||||
|
||||
var binder = new BodyModelBinder(
|
||||
actionContext,
|
||||
bindingContextAccessor,
|
||||
inputFormatterSelector.Object,
|
||||
bodyValidationPredicatesProvider.Object);
|
||||
|
||||
return binder;
|
||||
httpContext.RequestServices = serviceProvider.BuildServiceProvider();
|
||||
}
|
||||
|
||||
private static IScopedInstance<ActionContext> CreateActionContext(HttpContext context)
|
||||
|
|
|
|||
|
|
@ -1991,26 +1991,22 @@ namespace Microsoft.AspNet.Mvc
|
|||
var httpContext = new Mock<HttpContext>(MockBehavior.Loose);
|
||||
var httpRequest = new DefaultHttpContext().Request;
|
||||
var httpResponse = new DefaultHttpContext().Response;
|
||||
var mockFormattersProvider = new Mock<IOutputFormattersProvider>();
|
||||
mockFormattersProvider.SetupGet(o => o.OutputFormatters)
|
||||
.Returns(
|
||||
new List<IOutputFormatter>()
|
||||
{
|
||||
new JsonOutputFormatter()
|
||||
});
|
||||
|
||||
httpContext.SetupGet(c => c.Request).Returns(httpRequest);
|
||||
httpContext.SetupGet(c => c.Response).Returns(httpResponse);
|
||||
httpContext.Setup(o => o.RequestServices.GetService(typeof(IOutputFormattersProvider)))
|
||||
.Returns(mockFormattersProvider.Object);
|
||||
httpContext.Setup(o => o.RequestServices.GetService(typeof(ITempDataDictionary)))
|
||||
.Returns(tempData);
|
||||
httpResponse.Body = new MemoryStream();
|
||||
|
||||
var options = new Mock<IOptions<MvcOptions>>();
|
||||
options.SetupGet(o => o.Options)
|
||||
.Returns(new MvcOptions());
|
||||
var options = new MvcOptions();
|
||||
options.OutputFormatters.Add(new JsonOutputFormatter());
|
||||
|
||||
var optionsAccessor = new Mock<IOptions<MvcOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Options)
|
||||
.Returns(options);
|
||||
|
||||
httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions<MvcOptions>)))
|
||||
.Returns(options.Object);
|
||||
.Returns(optionsAccessor.Object);
|
||||
|
||||
var actionContext = new ActionContext(
|
||||
httpContext: httpContext.Object,
|
||||
|
|
@ -2035,22 +2031,16 @@ namespace Microsoft.AspNet.Mvc
|
|||
.Returns(DefaultOrder.DefaultFrameworkSortOrder);
|
||||
|
||||
|
||||
var inputFormattersProvider = new Mock<IInputFormattersProvider>();
|
||||
inputFormattersProvider.SetupGet(o => o.InputFormatters)
|
||||
.Returns(new List<IInputFormatter>());
|
||||
var excludeFilterProvider = new Mock<IValidationExcludeFiltersProvider>();
|
||||
excludeFilterProvider.SetupGet(o => o.ExcludeFilters)
|
||||
.Returns(new List<IExcludeTypeValidationFilter>());
|
||||
var invoker = new TestControllerActionInvoker(
|
||||
actionContext,
|
||||
new[] { filterProvider.Object },
|
||||
new MockControllerFactory(this),
|
||||
actionDescriptor,
|
||||
inputFormattersProvider.Object,
|
||||
new InputFormatter[0],
|
||||
Mock.Of<IControllerActionArgumentBinder>(),
|
||||
new MockModelBinderProvider(),
|
||||
new MockModelValidatorProviderProvider(),
|
||||
new MockValueProviderFactoryProvider(),
|
||||
new IModelBinder[0],
|
||||
new IModelValidatorProvider[0],
|
||||
new IValueProviderFactory[0],
|
||||
new MockScopedInstance<ActionBindingContext>(),
|
||||
tempData);
|
||||
|
||||
|
|
@ -2095,23 +2085,20 @@ namespace Microsoft.AspNet.Mvc
|
|||
var controllerFactory = new Mock<IControllerFactory>();
|
||||
controllerFactory.Setup(c => c.CreateController(It.IsAny<ActionContext>()))
|
||||
.Returns(new TestController());
|
||||
var inputFormattersProvider = new Mock<IInputFormattersProvider>();
|
||||
inputFormattersProvider.SetupGet(o => o.InputFormatters)
|
||||
.Returns(new List<IInputFormatter>());
|
||||
var metadataProvider = new EmptyModelMetadataProvider();
|
||||
var invoker = new ControllerActionInvoker(
|
||||
actionContext,
|
||||
new List<IFilterProvider>(),
|
||||
controllerFactory.Object,
|
||||
actionDescriptor,
|
||||
inputFormattersProvider.Object,
|
||||
new IInputFormatter[0],
|
||||
new DefaultControllerActionArgumentBinder(
|
||||
metadataProvider,
|
||||
new DefaultObjectValidator(Mock.Of<IValidationExcludeFiltersProvider>(), metadataProvider),
|
||||
new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider),
|
||||
new MockMvcOptionsAccessor()),
|
||||
new MockModelBinderProvider() { ModelBinders = new List<IModelBinder>() { binder.Object } },
|
||||
new MockModelValidatorProviderProvider(),
|
||||
new MockValueProviderFactoryProvider(),
|
||||
new IModelBinder[] { binder.Object },
|
||||
new IModelValidatorProvider[0],
|
||||
new IValueProviderFactory[0],
|
||||
new MockScopedInstance<ActionBindingContext>(),
|
||||
Mock.Of<ITempDataDictionary>());
|
||||
|
||||
|
|
@ -2205,11 +2192,11 @@ namespace Microsoft.AspNet.Mvc
|
|||
IFilterProvider[] filterProvider,
|
||||
MockControllerFactory controllerFactory,
|
||||
ControllerActionDescriptor descriptor,
|
||||
IInputFormattersProvider inputFormattersProvider,
|
||||
IReadOnlyList<IInputFormatter> inputFormatters,
|
||||
IControllerActionArgumentBinder controllerActionArgumentBinder,
|
||||
IModelBinderProvider modelBinderProvider,
|
||||
IModelValidatorProviderProvider modelValidatorProviderProvider,
|
||||
IValueProviderFactoryProvider valueProviderFactoryProvider,
|
||||
IReadOnlyList<IModelBinder> modelBinders,
|
||||
IReadOnlyList<IModelValidatorProvider> modelValidatorProviders,
|
||||
IReadOnlyList<IValueProviderFactory> valueProviderFactories,
|
||||
IScopedInstance<ActionBindingContext> actionBindingContext,
|
||||
ITempDataDictionary tempData)
|
||||
: base(
|
||||
|
|
@ -2217,11 +2204,11 @@ namespace Microsoft.AspNet.Mvc
|
|||
filterProvider,
|
||||
controllerFactory,
|
||||
descriptor,
|
||||
inputFormattersProvider,
|
||||
inputFormatters,
|
||||
controllerActionArgumentBinder,
|
||||
modelBinderProvider,
|
||||
modelValidatorProviderProvider,
|
||||
valueProviderFactoryProvider,
|
||||
modelBinders,
|
||||
modelValidatorProviders,
|
||||
valueProviderFactories,
|
||||
actionBindingContext,
|
||||
tempData)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1681,7 +1681,7 @@ namespace Microsoft.AspNet.Mvc.Test
|
|||
MetadataProvider = metadataProvider,
|
||||
ViewData = viewData,
|
||||
TempData = tempData,
|
||||
ObjectValidator = new DefaultObjectValidator(Mock.Of<IValidationExcludeFiltersProvider>(), metadataProvider)
|
||||
ObjectValidator = new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider)
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -226,7 +226,7 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
services.Setup(s => s.GetService(typeof(TestService)))
|
||||
.Returns(new TestService());
|
||||
services.Setup(s => s.GetService(typeof(IObjectModelValidator)))
|
||||
.Returns(new DefaultObjectValidator(Mock.Of<IValidationExcludeFiltersProvider>(), metadataProvider));
|
||||
.Returns(new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider));
|
||||
services
|
||||
.Setup(s => s.GetService(typeof(IScopedInstance<ActionBindingContext>)))
|
||||
.Returns(new MockScopedInstance<ActionBindingContext>());
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ using Microsoft.AspNet.Mvc.ModelBinding.Metadata;
|
|||
using Microsoft.AspNet.Mvc.Routing;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.AspNet.Routing.Constraints;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
|
@ -911,14 +912,14 @@ namespace Microsoft.AspNet.Mvc.Description
|
|||
// Act
|
||||
var descriptions = GetApiDescriptions(action);
|
||||
|
||||
// Assert
|
||||
// Assert
|
||||
var description = Assert.Single(descriptions);
|
||||
Assert.Equal(5, description.ParameterDescriptions.Count);
|
||||
|
||||
var name = Assert.Single(description.ParameterDescriptions, p => p.Name == "name");
|
||||
Assert.Same(BindingSource.Query, name.Source);
|
||||
Assert.Equal(typeof(string), name.Type);
|
||||
|
||||
|
||||
var id = Assert.Single(description.ParameterDescriptions, p => p.Name == "Id");
|
||||
Assert.Same(BindingSource.Path, id.Source);
|
||||
Assert.Equal(typeof(int), id.Type);
|
||||
|
|
@ -947,8 +948,15 @@ namespace Microsoft.AspNet.Mvc.Description
|
|||
{
|
||||
var context = new ApiDescriptionProviderContext(new ActionDescriptor[] { action });
|
||||
|
||||
var formattersProvider = new Mock<IOutputFormattersProvider>(MockBehavior.Strict);
|
||||
formattersProvider.Setup(fp => fp.OutputFormatters).Returns(formatters);
|
||||
var options = new MvcOptions();
|
||||
foreach (var formatter in formatters)
|
||||
{
|
||||
options.OutputFormatters.Add(formatter);
|
||||
}
|
||||
|
||||
var optionsAccessor = new Mock<IOptions<MvcOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Options)
|
||||
.Returns(options);
|
||||
|
||||
var constraintResolver = new Mock<IInlineConstraintResolver>();
|
||||
constraintResolver.Setup(c => c.ResolveConstraint("int"))
|
||||
|
|
@ -957,7 +965,7 @@ namespace Microsoft.AspNet.Mvc.Description
|
|||
var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
|
||||
|
||||
var provider = new DefaultApiDescriptionProvider(
|
||||
formattersProvider.Object,
|
||||
optionsAccessor.Object,
|
||||
constraintResolver.Object,
|
||||
modelMetadataProvider);
|
||||
|
||||
|
|
@ -1118,7 +1126,7 @@ namespace Microsoft.AspNet.Mvc.Description
|
|||
{
|
||||
}
|
||||
|
||||
private void AcceptsFormatters_Services([FromServices] IOutputFormattersProvider formatters)
|
||||
private void AcceptsFormatters_Services([FromServices] ITestService tempDataProvider)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -1345,5 +1353,10 @@ namespace Microsoft.AspNet.Mvc.Description
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private interface ITestService
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class ExcludeTypeValidationFilterExtensionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void InputFormatterDescriptors_AddsTypesAndTypeNames()
|
||||
{
|
||||
// Arrange
|
||||
var type1 = typeof(BaseType);
|
||||
var collection = new List<IExcludeTypeValidationFilter>();
|
||||
|
||||
// Act
|
||||
collection.Add(type1);
|
||||
collection.Add(type1.FullName);
|
||||
|
||||
// Assert
|
||||
Assert.Collection(collection,
|
||||
first =>
|
||||
{
|
||||
var filter = Assert.IsType<DefaultTypeBasedExcludeFilter>(first);
|
||||
Assert.Equal(type1, filter.ExcludedType);
|
||||
},
|
||||
second =>
|
||||
{
|
||||
var filter = Assert.IsType<DefaultTypeNameBasedExcludeFilter>(second);
|
||||
Assert.Equal(type1.FullName, filter.ExcludedTypeName);
|
||||
});
|
||||
}
|
||||
|
||||
private class BaseType
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -176,7 +176,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
}
|
||||
|
||||
private HttpContext GetHttpContext(
|
||||
IReadOnlyList<IOutputFormatter> optionsFormatters = null,
|
||||
IReadOnlyList<IOutputFormatter> outputFormatters = null,
|
||||
bool enableFallback = false)
|
||||
{
|
||||
var httpContext = new DefaultHttpContext();
|
||||
|
|
@ -185,20 +185,21 @@ namespace Microsoft.AspNet.Mvc
|
|||
var services = new Mock<IServiceProvider>(MockBehavior.Strict);
|
||||
httpContext.RequestServices = services.Object;
|
||||
|
||||
var mockFormattersProvider = new Mock<IOutputFormattersProvider>();
|
||||
mockFormattersProvider
|
||||
.SetupGet(o => o.OutputFormatters)
|
||||
.Returns(optionsFormatters ?? new List<IOutputFormatter>());
|
||||
var options = new MvcOptions();
|
||||
if (outputFormatters != null)
|
||||
{
|
||||
foreach (var formatter in outputFormatters)
|
||||
{
|
||||
options.OutputFormatters.Add(formatter);
|
||||
}
|
||||
}
|
||||
|
||||
services
|
||||
.Setup(s => s.GetService(typeof(IOutputFormattersProvider)))
|
||||
.Returns(mockFormattersProvider.Object);
|
||||
var optionsAccessor = new Mock<IOptions<MvcOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Options)
|
||||
.Returns(options);
|
||||
|
||||
var options = new Mock<IOptions<MvcOptions>>();
|
||||
options.SetupGet(o => o.Options)
|
||||
.Returns(new MvcOptions());
|
||||
services.Setup(s => s.GetService(typeof(IOptions<MvcOptions>)))
|
||||
.Returns(options.Object);
|
||||
.Returns(optionsAccessor.Object);
|
||||
|
||||
// This is the ultimate fallback, it will be used if none of the formatters from options
|
||||
// work.
|
||||
|
|
|
|||
|
|
@ -1,14 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||
{
|
||||
public class MockModelBinderProvider : IModelBinderProvider
|
||||
{
|
||||
public List<IModelBinder> ModelBinders { get; set; } = new List<IModelBinder>();
|
||||
|
||||
IReadOnlyList<IModelBinder> IModelBinderProvider.ModelBinders { get { return ModelBinders; } }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
|
||||
{
|
||||
public class MockModelValidatorProviderProvider : IModelValidatorProviderProvider
|
||||
{
|
||||
public List<IModelValidatorProvider> ModelValidatorProviders { get; } = new List<IModelValidatorProvider>();
|
||||
|
||||
IReadOnlyList<IModelValidatorProvider> IModelValidatorProviderProvider.ModelValidatorProviders
|
||||
{
|
||||
get { return ModelValidatorProviders; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||
{
|
||||
public class MockValueProviderFactoryProvider : IValueProviderFactoryProvider
|
||||
{
|
||||
public List<IValueProviderFactory> ValueProviderFactories { get; } = new List<IValueProviderFactory>();
|
||||
|
||||
IReadOnlyList<IValueProviderFactory> IValueProviderFactoryProvider.ValueProviderFactories
|
||||
{
|
||||
get { return ValueProviderFactories; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,66 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Threading.Tasks;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
public class DefaultModelBindersProviderTest
|
||||
{
|
||||
[Fact]
|
||||
public void DefaultModelBindersProvider_ProvidesInstancesOfModelBinders()
|
||||
{
|
||||
// Arrange
|
||||
var service = Mock.Of<ITestService>();
|
||||
var binder = new TypeMatchModelBinder();
|
||||
var options = new MvcOptions();
|
||||
options.ModelBinders.Add(binder);
|
||||
options.ModelBinders.Add(typeof(TestModelBinder));
|
||||
var optionsAccessor = new Mock<IOptions<MvcOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Options)
|
||||
.Returns(options);
|
||||
var serviceProvider = new Mock<IServiceProvider>();
|
||||
serviceProvider.Setup(p => p.GetService(typeof(ITestService)))
|
||||
.Returns(service);
|
||||
var typeActivatorCache = new DefaultTypeActivatorCache();
|
||||
|
||||
var provider = new DefaultModelBindersProvider(optionsAccessor.Object,
|
||||
typeActivatorCache,
|
||||
serviceProvider.Object);
|
||||
|
||||
// Act
|
||||
var binders = provider.ModelBinders;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, binders.Count);
|
||||
Assert.Same(binder, binders[0]);
|
||||
var testModelBinder = Assert.IsType<TestModelBinder>(binders[1]);
|
||||
Assert.Same(service, testModelBinder.Service);
|
||||
}
|
||||
|
||||
private class TestModelBinder : IModelBinder
|
||||
{
|
||||
public TestModelBinder(ITestService service)
|
||||
{
|
||||
Service = service;
|
||||
}
|
||||
|
||||
public ITestService Service { get; private set; }
|
||||
|
||||
public Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public interface ITestService
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,152 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Mvc.ModelBinding;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
public class DefaultValidationExcludeFiltersProviderTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(typeof(BaseType))]
|
||||
[InlineData(typeof(DerivedType))]
|
||||
public void Add_WithType_RegistersTypesAndDerivedType_ToBeExcluded(Type type)
|
||||
{
|
||||
// Arrange
|
||||
var options = new MvcOptions();
|
||||
options.ValidationExcludeFilters.Add(type);
|
||||
var optionsAccessor = new Mock<IOptions<MvcOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Options)
|
||||
.Returns(options);
|
||||
var typeActivatorCache = new Mock<ITypeActivatorCache>();
|
||||
var service = Mock.Of<ITestService>();
|
||||
var serviceProvider = new Mock<IServiceProvider>();
|
||||
serviceProvider.Setup(p => p.GetService(typeof(ITestService)))
|
||||
.Returns(service);
|
||||
var provider = new DefaultValidationExcludeFiltersProvider(optionsAccessor.Object,
|
||||
typeActivatorCache.Object,
|
||||
serviceProvider.Object);
|
||||
|
||||
// Act
|
||||
var filters = provider.ExcludeFilters;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1, filters.Count);
|
||||
Assert.True(filters[0].IsTypeExcluded(type));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(typeof(BaseType))]
|
||||
[InlineData(typeof(UnRelatedType))]
|
||||
public void Add_RegisterDerivedType_BaseAndUnrealatedTypesAreNotExcluded(Type type)
|
||||
{
|
||||
// Arrange
|
||||
var options = new MvcOptions();
|
||||
options.ValidationExcludeFilters.Add(typeof(DerivedType));
|
||||
var optionsAccessor = new Mock<IOptions<MvcOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Options)
|
||||
.Returns(options);
|
||||
var typeActivatorCache = new Mock<ITypeActivatorCache>();
|
||||
var service = Mock.Of<ITestService>();
|
||||
var serviceProvider = new Mock<IServiceProvider>();
|
||||
serviceProvider.Setup(p => p.GetService(typeof(ITestService)))
|
||||
.Returns(service);
|
||||
var provider = new DefaultValidationExcludeFiltersProvider(optionsAccessor.Object,
|
||||
typeActivatorCache.Object,
|
||||
serviceProvider.Object);
|
||||
|
||||
// Act
|
||||
var filters = provider.ExcludeFilters;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1, filters.Count);
|
||||
Assert.False(filters[0].IsTypeExcluded(type));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(typeof(BaseType))]
|
||||
[InlineData(typeof(DerivedType))]
|
||||
public void Add_WithTypeName_RegistersTypesAndDerivedType_ToBeExcluded(Type type)
|
||||
{
|
||||
// Arrange
|
||||
var options = new MvcOptions();
|
||||
options.ValidationExcludeFilters.Add(type.FullName);
|
||||
var optionsAccessor = new Mock<IOptions<MvcOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Options)
|
||||
.Returns(options);
|
||||
var typeActivatorCache = new Mock<ITypeActivatorCache>();
|
||||
var service = Mock.Of<ITestService>();
|
||||
var serviceProvider = new Mock<IServiceProvider>();
|
||||
serviceProvider.Setup(p => p.GetService(typeof(ITestService)))
|
||||
.Returns(service);
|
||||
var provider = new DefaultValidationExcludeFiltersProvider(optionsAccessor.Object,
|
||||
typeActivatorCache.Object,
|
||||
serviceProvider.Object);
|
||||
|
||||
// Act
|
||||
var filters = provider.ExcludeFilters;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1, filters.Count);
|
||||
Assert.True(filters[0].IsTypeExcluded(type));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(typeof(BaseType))]
|
||||
[InlineData(typeof(UnRelatedType))]
|
||||
[InlineData(typeof(SubNameSpace.UnRelatedType))]
|
||||
public void Add_WithTypeName_RegisterDerivedType_BaseAndUnrealatedTypesAreNotExcluded(Type type)
|
||||
{
|
||||
// Arrange
|
||||
var options = new MvcOptions();
|
||||
options.ValidationExcludeFilters.Add(typeof(DerivedType).FullName);
|
||||
var optionsAccessor = new Mock<IOptions<MvcOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Options)
|
||||
.Returns(options);
|
||||
var service = Mock.Of<ITestService>();
|
||||
var serviceProvider = new Mock<IServiceProvider>();
|
||||
serviceProvider.Setup(p => p.GetService(typeof(ITestService)))
|
||||
.Returns(service);
|
||||
var typeActivatorCache = new Mock<ITypeActivatorCache>();
|
||||
var provider = new DefaultValidationExcludeFiltersProvider(optionsAccessor.Object,
|
||||
typeActivatorCache.Object,
|
||||
serviceProvider.Object);
|
||||
|
||||
// Act
|
||||
var filters = provider.ExcludeFilters;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1, filters.Count);
|
||||
Assert.False(filters[0].IsTypeExcluded(type));
|
||||
}
|
||||
|
||||
private class BaseType
|
||||
{
|
||||
}
|
||||
|
||||
private class DerivedType : BaseType
|
||||
{
|
||||
}
|
||||
|
||||
private class UnRelatedType
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
namespace SubNameSpace
|
||||
{
|
||||
public class UnRelatedType
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public interface ITestService
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1,65 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
public class DefaultValidationProviderProviderTest
|
||||
{
|
||||
[Fact]
|
||||
public void ValidationProviders_ReturnsInstantiatedListOfValueProviders()
|
||||
{
|
||||
// Arrange
|
||||
var service = Mock.Of<ITestService>();
|
||||
var validationProvider = Mock.Of<IModelValidatorProvider>();
|
||||
var type = typeof(TestModelValidationProvider);
|
||||
var serviceProvider = new Mock<IServiceProvider>();
|
||||
serviceProvider.Setup(p => p.GetService(typeof(ITestService)))
|
||||
.Returns(service);
|
||||
var typeActivatorCache = new DefaultTypeActivatorCache();
|
||||
var options = new MvcOptions();
|
||||
options.ModelValidatorProviders.Add(type);
|
||||
options.ModelValidatorProviders.Add(validationProvider);
|
||||
var accessor = new Mock<IOptions<MvcOptions>>();
|
||||
accessor.SetupGet(a => a.Options)
|
||||
.Returns(options);
|
||||
var provider = new DefaultModelValidatorProviderProvider(accessor.Object,
|
||||
typeActivatorCache,
|
||||
serviceProvider.Object);
|
||||
|
||||
// Act
|
||||
var result = provider.ModelValidatorProviders;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result.Count);
|
||||
var testModelValidationProvider = Assert.IsType<TestModelValidationProvider>(result[0]);
|
||||
Assert.Same(service, testModelValidationProvider.Service);
|
||||
Assert.Same(validationProvider, result[1]);
|
||||
}
|
||||
|
||||
private class TestModelValidationProvider : IModelValidatorProvider
|
||||
{
|
||||
public TestModelValidationProvider(ITestService service)
|
||||
{
|
||||
Service = service;
|
||||
}
|
||||
|
||||
public ITestService Service { get; private set; }
|
||||
|
||||
public void GetValidators(ModelValidatorProviderContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public interface ITestService
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Mvc.ModelBinding;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
public class DefaultValueProviderFactoryProviderTest
|
||||
{
|
||||
[Fact]
|
||||
public void ViewEngine_ReturnsInstantiatedListOfViewEngines()
|
||||
{
|
||||
// Arrange
|
||||
var service = Mock.Of<ITestService>();
|
||||
var valueProviderFactory = Mock.Of<IValueProviderFactory>();
|
||||
var type = typeof(TestValueProviderFactory);
|
||||
var serviceProvider = new Mock<IServiceProvider>();
|
||||
serviceProvider.Setup(p => p.GetService(typeof(ITestService)))
|
||||
.Returns(service);
|
||||
var typeActivatorCache = new DefaultTypeActivatorCache();
|
||||
var options = new MvcOptions();
|
||||
options.ValueProviderFactories.Add(valueProviderFactory);
|
||||
options.ValueProviderFactories.Add(type);
|
||||
var accessor = new Mock<IOptions<MvcOptions>>();
|
||||
accessor.SetupGet(a => a.Options)
|
||||
.Returns(options);
|
||||
var provider = new DefaultValueProviderFactoryProvider(accessor.Object,
|
||||
typeActivatorCache,
|
||||
serviceProvider.Object);
|
||||
|
||||
// Act
|
||||
var result = provider.ValueProviderFactories;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result.Count);
|
||||
Assert.Same(valueProviderFactory, result[0]);
|
||||
var testValueProviderFactory = Assert.IsType<TestValueProviderFactory>(result[1]);
|
||||
Assert.Same(service, testValueProviderFactory.Service);
|
||||
}
|
||||
|
||||
private class TestValueProviderFactory : IValueProviderFactory
|
||||
{
|
||||
public TestValueProviderFactory(ITestService service)
|
||||
{
|
||||
Service = service;
|
||||
}
|
||||
|
||||
public ITestService Service { get; private set; }
|
||||
|
||||
public IValueProvider GetValueProvider(ValueProviderFactoryContext context)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public interface ITestService
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,67 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Mvc.Rendering;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
public class DefaultViewEngineProviderTest
|
||||
{
|
||||
[Fact]
|
||||
public void ViewEngine_ReturnsInstantiatedListOfViewEngines()
|
||||
{
|
||||
// Arrange
|
||||
var service = Mock.Of<ITestService>();
|
||||
var viewEngine = Mock.Of<IViewEngine>();
|
||||
var type = typeof(TestViewEngine);
|
||||
var serviceProvider = new Mock<IServiceProvider>();
|
||||
serviceProvider.Setup(p => p.GetService(typeof(ITestService)))
|
||||
.Returns(service);
|
||||
var typeActivatorCache = new DefaultTypeActivatorCache();
|
||||
var options = new MvcOptions();
|
||||
options.ViewEngines.Add(viewEngine);
|
||||
options.ViewEngines.Add(type);
|
||||
var accessor = new Mock<IOptions<MvcOptions>>();
|
||||
accessor.SetupGet(a => a.Options)
|
||||
.Returns(options);
|
||||
var provider = new DefaultViewEngineProvider(accessor.Object, typeActivatorCache, serviceProvider.Object);
|
||||
|
||||
// Act
|
||||
var result = provider.ViewEngines;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result.Count);
|
||||
Assert.Same(viewEngine, result[0]);
|
||||
var testViewEngine = Assert.IsType<TestViewEngine>(result[1]);
|
||||
Assert.Same(service, testViewEngine.Service);
|
||||
}
|
||||
|
||||
private class TestViewEngine : IViewEngine
|
||||
{
|
||||
public TestViewEngine(ITestService service)
|
||||
{
|
||||
Service = service;
|
||||
}
|
||||
|
||||
public ITestService Service { get; private set; }
|
||||
|
||||
public ViewEngineResult FindPartialView(ActionContext context, string partialViewName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ViewEngineResult FindView(ActionContext context, string viewName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public interface ITestService
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.AspNet.Testing;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
public class ExcludeValidationDescriptorTests
|
||||
{
|
||||
[Fact]
|
||||
public void ConstructorThrows_IfTypeIsNotIExcludeTypeFromBodyValidation()
|
||||
{
|
||||
// Arrange
|
||||
var expected = "The type 'System.String' must derive from " +
|
||||
"'Microsoft.AspNet.Mvc.ModelBinding.Validation.IExcludeTypeValidationFilter'.";
|
||||
var type = typeof(string);
|
||||
|
||||
// Act & Assert
|
||||
ExceptionAssert.ThrowsArgument(() => new ExcludeValidationDescriptor(type), "type", expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstructorSetsOptionType()
|
||||
{
|
||||
// Arrange
|
||||
var type = typeof(TestExcludeFilter);
|
||||
|
||||
// Act
|
||||
var descriptor = new ExcludeValidationDescriptor(type);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(type, descriptor.OptionType);
|
||||
Assert.Null(descriptor.Instance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstructorSetsInstanceeAndOptionType()
|
||||
{
|
||||
// Arrange
|
||||
var instance = new TestExcludeFilter();
|
||||
|
||||
// Act
|
||||
var descriptor = new ExcludeValidationDescriptor(instance);
|
||||
|
||||
// Assert
|
||||
Assert.Same(instance, descriptor.Instance);
|
||||
Assert.Equal(instance.GetType(), descriptor.OptionType);
|
||||
}
|
||||
|
||||
private class TestExcludeFilter : IExcludeTypeValidationFilter
|
||||
{
|
||||
public bool IsTypeExcluded([NotNull] Type propertyType)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,72 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.OptionDescriptors;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Core.Test
|
||||
{
|
||||
public class InputFormatterDescriptorExtensionTest
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(-1)]
|
||||
[InlineData(5)]
|
||||
public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index)
|
||||
{
|
||||
// Arrange
|
||||
var collection = new List<InputFormatterDescriptor>
|
||||
{
|
||||
new InputFormatterDescriptor(Mock.Of<IInputFormatter>()),
|
||||
new InputFormatterDescriptor(Mock.Of<IInputFormatter>())
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentOutOfRangeException>("index",
|
||||
() => collection.Insert(index, typeof(IInputFormatter)));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-2)]
|
||||
[InlineData(3)]
|
||||
public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index)
|
||||
{
|
||||
// Arrange
|
||||
var collection = new List<InputFormatterDescriptor>
|
||||
{
|
||||
new InputFormatterDescriptor(Mock.Of<IInputFormatter>()),
|
||||
new InputFormatterDescriptor(Mock.Of<IInputFormatter>())
|
||||
};
|
||||
var formatter = Mock.Of<IInputFormatter>();
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentOutOfRangeException>("index", () => collection.Insert(index, formatter));
|
||||
}
|
||||
|
||||
[InlineData]
|
||||
public void InputFormatterDescriptors_AddsTypesAndInstances()
|
||||
{
|
||||
// Arrange
|
||||
var formatter1 = Mock.Of<IInputFormatter>();
|
||||
var formatter2 = Mock.Of<IInputFormatter>();
|
||||
var type1 = typeof(JsonInputFormatter);
|
||||
var type2 = typeof(IInputFormatter);
|
||||
var collection = new List<InputFormatterDescriptor>();
|
||||
|
||||
// Act
|
||||
collection.Add(formatter1);
|
||||
collection.Insert(1, formatter2);
|
||||
collection.Add(type1);
|
||||
collection.Insert(2, type2);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(4, collection.Count);
|
||||
Assert.Equal(formatter1, collection[0].Instance);
|
||||
Assert.Equal(formatter2, collection[1].Instance);
|
||||
Assert.Equal(type2, collection[2].OptionType);
|
||||
Assert.Equal(type1, collection[3].OptionType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Threading.Tasks;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Microsoft.AspNet.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class InputFormatterDescriptorTest
|
||||
{
|
||||
[Fact]
|
||||
public void ConstructorThrows_IfTypeIsNotInputFormatter()
|
||||
{
|
||||
// Arrange
|
||||
var expected = "The type 'System.String' must derive from " +
|
||||
"'Microsoft.AspNet.Mvc.IInputFormatter'.";
|
||||
|
||||
var type = typeof(string);
|
||||
|
||||
// Act & Assert
|
||||
ExceptionAssert.ThrowsArgument(() => new InputFormatterDescriptor(type), "type", expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstructorSets_InputFormatterType()
|
||||
{
|
||||
// Arrange
|
||||
var type = typeof(TestInputFormatter);
|
||||
|
||||
// Act
|
||||
var descriptor = new InputFormatterDescriptor(type);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(type, descriptor.OptionType);
|
||||
Assert.Null(descriptor.Instance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstructorSets_InputFormatterInstanceAndType()
|
||||
{
|
||||
// Arrange
|
||||
var testFormatter = new TestInputFormatter();
|
||||
|
||||
// Act
|
||||
var descriptor = new InputFormatterDescriptor(testFormatter);
|
||||
|
||||
// Assert
|
||||
Assert.Same(testFormatter, descriptor.Instance);
|
||||
Assert.Equal(testFormatter.GetType(), descriptor.OptionType);
|
||||
}
|
||||
|
||||
private class TestInputFormatter : IInputFormatter
|
||||
{
|
||||
public bool CanRead(InputFormatterContext context)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<object> ReadAsync(InputFormatterContext context)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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 System.Linq;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class ModelBinderDescriptorExtensionsTest
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(-1)]
|
||||
[InlineData(5)]
|
||||
public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index)
|
||||
{
|
||||
// Arrange
|
||||
var collection = new List<ModelBinderDescriptor>
|
||||
{
|
||||
new ModelBinderDescriptor(Mock.Of<IModelBinder>()),
|
||||
new ModelBinderDescriptor(Mock.Of<IModelBinder>())
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentOutOfRangeException>("index", () => collection.Insert(index, typeof(IModelBinder)));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-2)]
|
||||
[InlineData(3)]
|
||||
public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index)
|
||||
{
|
||||
// Arrange
|
||||
var collection = new List<ModelBinderDescriptor>
|
||||
{
|
||||
new ModelBinderDescriptor(Mock.Of<IModelBinder>()),
|
||||
new ModelBinderDescriptor(Mock.Of<IModelBinder>())
|
||||
};
|
||||
var binder = Mock.Of<IModelBinder>();
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentOutOfRangeException>("index", () => collection.Insert(index, binder));
|
||||
}
|
||||
|
||||
[InlineData]
|
||||
public void ModelBinderDescriptors_AddsTypesAndInstances()
|
||||
{
|
||||
// Arrange
|
||||
var binder1 = Mock.Of<IModelBinder>();
|
||||
var binder2 = Mock.Of<IModelBinder>();
|
||||
var type1 = typeof(TypeMatchModelBinder);
|
||||
var type2 = typeof(TypeConverterModelBinder);
|
||||
var collection = new List<ModelBinderDescriptor>();
|
||||
|
||||
// Act
|
||||
collection.Add(binder1);
|
||||
collection.Insert(1, binder2);
|
||||
collection.Add(type1);
|
||||
collection.Insert(2, type2);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(4, collection.Count);
|
||||
Assert.Equal(binder1, collection[0].Instance);
|
||||
Assert.Equal(binder2, collection[1].Instance);
|
||||
Assert.Equal(type2, collection[2].OptionType);
|
||||
Assert.Equal(type1, collection[3].OptionType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ModelBinders_RemoveTypesOf_RemovesDescriptorsOfIModelBinder()
|
||||
{
|
||||
// Arrange
|
||||
var modelBinders = new MvcOptions().ModelBinders;
|
||||
modelBinders.Add(new ByteArrayModelBinder());
|
||||
modelBinders.Add(Mock.Of<IModelBinder>());
|
||||
modelBinders.Add(typeof(ByteArrayModelBinder));
|
||||
modelBinders.Add(Mock.Of<IModelBinder>());
|
||||
|
||||
// Act
|
||||
modelBinders.RemoveTypesOf<ByteArrayModelBinder>();
|
||||
|
||||
// Assert
|
||||
Assert.DoesNotContain(modelBinders, descriptor => descriptor.OptionType == typeof(ByteArrayModelBinder));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Threading.Tasks;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
public class ModelBinderDescriptorTest
|
||||
{
|
||||
[Fact]
|
||||
public void ConstructorThrows_IfTypeIsNotIModelBinder()
|
||||
{
|
||||
// Arrange
|
||||
var expected = "The type 'System.String' must derive from " +
|
||||
"'Microsoft.AspNet.Mvc.ModelBinding.IModelBinder'.";
|
||||
var type = typeof(string);
|
||||
|
||||
// Act & Assert
|
||||
ExceptionAssert.ThrowsArgument(() => new ModelBinderDescriptor(type), "type", expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstructorSetsOptionType()
|
||||
{
|
||||
// Arrange
|
||||
var type = typeof(TestModelBinder);
|
||||
|
||||
// Act
|
||||
var descriptor = new ModelBinderDescriptor(type);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(type, descriptor.OptionType);
|
||||
Assert.Null(descriptor.Instance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstructorSetsInstanceeAndOptionType()
|
||||
{
|
||||
// Arrange
|
||||
var viewEngine = new TestModelBinder();
|
||||
|
||||
// Act
|
||||
var descriptor = new ModelBinderDescriptor(viewEngine);
|
||||
|
||||
// Assert
|
||||
Assert.Same(viewEngine, descriptor.Instance);
|
||||
Assert.Equal(viewEngine.GetType(), descriptor.OptionType);
|
||||
}
|
||||
|
||||
private class TestModelBinder : IModelBinder
|
||||
{
|
||||
public Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,63 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.AspNet.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
public class ModelValidatorProviderDescriptorTest
|
||||
{
|
||||
[Fact]
|
||||
public void ConstructorThrows_IfTypeIsNotModelValidatorProvider()
|
||||
{
|
||||
// Arrange
|
||||
var validatorProviderType = typeof(IModelValidatorProvider).FullName;
|
||||
var type = typeof(string);
|
||||
var expected = string.Format("The type '{0}' must derive from '{1}'.",
|
||||
type.FullName, validatorProviderType);
|
||||
|
||||
// Act & Assert
|
||||
ExceptionAssert.ThrowsArgument(() => new ModelValidatorProviderDescriptor(type), "type", expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstructorSetsModelValidatorProviderType()
|
||||
{
|
||||
// Arrange
|
||||
var type = typeof(TestModelValidatorProvider);
|
||||
|
||||
// Act
|
||||
var descriptor = new ModelValidatorProviderDescriptor(type);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(type, descriptor.OptionType);
|
||||
Assert.Null(descriptor.Instance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstructorSetsInstanceAndType()
|
||||
{
|
||||
// Arrange
|
||||
var validatorProvider = new TestModelValidatorProvider();
|
||||
|
||||
// Act
|
||||
var descriptor = new ModelValidatorProviderDescriptor(validatorProvider);
|
||||
|
||||
// Assert
|
||||
Assert.Same(validatorProvider, descriptor.Instance);
|
||||
Assert.Equal(validatorProvider.GetType(), descriptor.OptionType);
|
||||
}
|
||||
|
||||
private class TestModelValidatorProvider : IModelValidatorProvider
|
||||
{
|
||||
public void GetValidators(ModelValidatorProviderContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,74 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class ModelValidatorProviderDescriptorExtensionsTest
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(-1)]
|
||||
[InlineData(5)]
|
||||
public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index)
|
||||
{
|
||||
// Arrange
|
||||
var collection = new List<ModelValidatorProviderDescriptor>
|
||||
{
|
||||
new ModelValidatorProviderDescriptor(Mock.Of<IModelValidatorProvider>()),
|
||||
new ModelValidatorProviderDescriptor(Mock.Of<IModelValidatorProvider>())
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentOutOfRangeException>("index",
|
||||
() => collection.Insert(index, typeof(IModelValidatorProvider)));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-2)]
|
||||
[InlineData(3)]
|
||||
public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index)
|
||||
{
|
||||
// Arrange
|
||||
var collection = new List<ModelValidatorProviderDescriptor>
|
||||
{
|
||||
new ModelValidatorProviderDescriptor(Mock.Of<IModelValidatorProvider>()),
|
||||
new ModelValidatorProviderDescriptor(Mock.Of<IModelValidatorProvider>())
|
||||
};
|
||||
var valueProviderFactory = Mock.Of<IModelValidatorProvider>();
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentOutOfRangeException>("index", () => collection.Insert(index, valueProviderFactory));
|
||||
}
|
||||
|
||||
[InlineData]
|
||||
public void ModelValidatorProviderDescriptors_AddsTypesAndInstances()
|
||||
{
|
||||
// Arrange
|
||||
var provider1 = Mock.Of<IModelValidatorProvider>();
|
||||
var provider2 = Mock.Of<IModelValidatorProvider>();
|
||||
var type1 = typeof(DefaultModelValidatorProvider);
|
||||
var type2 = typeof(DataAnnotationsModelValidatorProvider);
|
||||
var collection = new List<ModelValidatorProviderDescriptor>();
|
||||
|
||||
// Act
|
||||
collection.Add(provider2);
|
||||
collection.Insert(0, provider1);
|
||||
collection.Add(type2);
|
||||
collection.Insert(2, type1);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(4, collection.Count);
|
||||
Assert.Same(provider1, collection[0].Instance);
|
||||
Assert.Same(provider2, collection[1].Instance);
|
||||
Assert.IsType<DefaultModelValidatorProvider>(collection[2].OptionType);
|
||||
Assert.IsType<DataAnnotationsModelValidatorProvider>(collection[3].OptionType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,276 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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 System.Linq;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
public class OptionDescriptorExtensionsTest
|
||||
{
|
||||
[Fact]
|
||||
public void InputFormatters_InstanceOf_ThrowsInvalidOperationExceptionIfMoreThanOnceInstance()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().InputFormatters;
|
||||
formatters.Add(new JsonInputFormatter());
|
||||
formatters.Add(Mock.Of<IInputFormatter>());
|
||||
formatters.Add(new JsonInputFormatter());
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidOperationException>(() => formatters.InstanceOf<JsonInputFormatter>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InputFormatters_InstanceOf_ThrowsInvalidOperationExceptionIfNoInstance()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().InputFormatters;
|
||||
formatters.Add(Mock.Of<IInputFormatter>());
|
||||
formatters.Add(typeof(JsonInputFormatter));
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidOperationException>(() => formatters.InstanceOf<JsonInputFormatter>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InputFormatters_InstanceOf_ReturnsInstanceOfIInputFormatterIfOneExists()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().InputFormatters;
|
||||
formatters.Add(Mock.Of<IInputFormatter>());
|
||||
var jsonFormatter = new JsonInputFormatter();
|
||||
formatters.Add(jsonFormatter);
|
||||
formatters.Add(typeof(JsonInputFormatter));
|
||||
|
||||
// Act
|
||||
var formatter = formatters.InstanceOf<JsonInputFormatter>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(formatter);
|
||||
Assert.IsType<JsonInputFormatter>(formatter);
|
||||
Assert.Same(jsonFormatter, formatter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InputFormatters_InstanceOfOrDefault_ThrowsInvalidOperationExceptionIfMoreThanOnceInstance()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().InputFormatters;
|
||||
formatters.Add(new JsonInputFormatter());
|
||||
formatters.Add(Mock.Of<IInputFormatter>());
|
||||
formatters.Add(new JsonInputFormatter());
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidOperationException>(() => formatters.InstanceOfOrDefault<JsonInputFormatter>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InputFormatters_InstanceOfOrDefault_ReturnsNullIfNoInstance()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().InputFormatters;
|
||||
formatters.Add(Mock.Of<IInputFormatter>());
|
||||
formatters.Add(typeof(JsonInputFormatter));
|
||||
|
||||
// Act
|
||||
var formatter = formatters.InstanceOfOrDefault<JsonInputFormatter>();
|
||||
|
||||
// Assert
|
||||
Assert.Null(formatter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InputFormatters_InstanceOfOrDefault_ReturnsInstanceOfIInputFormatterIfOneExists()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().InputFormatters;
|
||||
formatters.Add(Mock.Of<IInputFormatter>());
|
||||
formatters.Add(typeof(JsonInputFormatter));
|
||||
var jsonFormatter = new JsonInputFormatter();
|
||||
formatters.Add(jsonFormatter);
|
||||
|
||||
// Act
|
||||
var formatter = formatters.InstanceOfOrDefault<JsonInputFormatter>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(formatter);
|
||||
Assert.IsType<JsonInputFormatter>(formatter);
|
||||
Assert.Same(jsonFormatter, formatter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InputFormatters_InstancesOf_ReturnsEmptyCollectionIfNoneExist()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().InputFormatters;
|
||||
formatters.Add(Mock.Of<IInputFormatter>());
|
||||
formatters.Add(typeof(JsonInputFormatter));
|
||||
|
||||
// Act
|
||||
var jsonFormatters = formatters.InstancesOf<JsonInputFormatter>();
|
||||
|
||||
// Assert
|
||||
Assert.Empty(jsonFormatters);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InputFormatters_InstancesOf_ReturnsNonEmptyCollectionIfSomeExist()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().InputFormatters;
|
||||
formatters.Add(typeof(JsonInputFormatter));
|
||||
var formatter1 = new JsonInputFormatter();
|
||||
var formatter2 = Mock.Of<IInputFormatter>();
|
||||
var formatter3 = new JsonInputFormatter();
|
||||
var formatter4 = Mock.Of<IInputFormatter>();
|
||||
formatters.Add(formatter1);
|
||||
formatters.Add(formatter2);
|
||||
formatters.Add(formatter3);
|
||||
formatters.Add(formatter4);
|
||||
|
||||
var expectedFormatters = new List<JsonInputFormatter> { formatter1, formatter3 };
|
||||
|
||||
// Act
|
||||
var jsonFormatters = formatters.InstancesOf<JsonInputFormatter>().ToList();
|
||||
|
||||
// Assert
|
||||
Assert.NotEmpty(jsonFormatters);
|
||||
Assert.Equal(jsonFormatters, expectedFormatters);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OutputFormatters_InstanceOf_ThrowsInvalidOperationExceptionIfMoreThanOnceInstance()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().OutputFormatters;
|
||||
formatters.Add(new JsonOutputFormatter());
|
||||
formatters.Add(Mock.Of<IOutputFormatter>());
|
||||
formatters.Add(new JsonOutputFormatter());
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidOperationException>(() => formatters.InstanceOf<JsonOutputFormatter>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OutputFormatters_InstanceOf_ThrowsInvalidOperationExceptionIfNoInstance()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().OutputFormatters;
|
||||
formatters.Add(Mock.Of<IOutputFormatter>());
|
||||
formatters.Add(typeof(JsonOutputFormatter));
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidOperationException>(() => formatters.InstanceOf<JsonOutputFormatter>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OutputFormatters_InstanceOf_ReturnsInstanceOfIInputFormatterIfOneExists()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().OutputFormatters;
|
||||
formatters.Add(Mock.Of<IOutputFormatter>());
|
||||
var jsonFormatter = new JsonOutputFormatter();
|
||||
formatters.Add(jsonFormatter);
|
||||
formatters.Add(typeof(JsonOutputFormatter));
|
||||
|
||||
// Act
|
||||
var formatter = formatters.InstanceOf<JsonOutputFormatter>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(formatter);
|
||||
Assert.IsType<JsonOutputFormatter>(formatter);
|
||||
Assert.Same(jsonFormatter, formatter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OutputFormatters_InstanceOfOrDefault_ThrowsInvalidOperationExceptionIfMoreThanOnceInstance()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().OutputFormatters;
|
||||
formatters.Add(new JsonOutputFormatter());
|
||||
formatters.Add(Mock.Of<IOutputFormatter>());
|
||||
formatters.Add(new JsonOutputFormatter());
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidOperationException>(() => formatters.InstanceOfOrDefault<JsonOutputFormatter>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OutputFormatters_InstanceOfOrDefault_ReturnsNullIfNoInstance()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().OutputFormatters;
|
||||
formatters.Add(Mock.Of<IOutputFormatter>());
|
||||
formatters.Add(typeof(JsonOutputFormatter));
|
||||
|
||||
// Act
|
||||
var formatter = formatters.InstanceOfOrDefault<JsonOutputFormatter>();
|
||||
|
||||
// Assert
|
||||
Assert.Null(formatter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OutputFormatters_InstanceOfOrDefault_ReturnsInstanceOfIOutputFormatterIfOneExists()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().OutputFormatters;
|
||||
formatters.Add(Mock.Of<IOutputFormatter>());
|
||||
formatters.Add(typeof(JsonOutputFormatter));
|
||||
var jsonFormatter = new JsonOutputFormatter();
|
||||
formatters.Add(jsonFormatter);
|
||||
|
||||
// Act
|
||||
var formatter = formatters.InstanceOfOrDefault<JsonOutputFormatter>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(formatter);
|
||||
Assert.IsType<JsonOutputFormatter>(formatter);
|
||||
Assert.Same(jsonFormatter, formatter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OutputFormatters_InstancesOf_ReturnsEmptyCollectionIfNoneExist()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().OutputFormatters;
|
||||
formatters.Add(Mock.Of<IOutputFormatter>());
|
||||
formatters.Add(typeof(JsonOutputFormatter));
|
||||
|
||||
// Act
|
||||
var jsonFormatters = formatters.InstancesOf<JsonOutputFormatter>();
|
||||
|
||||
// Assert
|
||||
Assert.Empty(jsonFormatters);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OutputFormatters_InstancesOf_ReturnsNonEmptyCollectionIfSomeExist()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().OutputFormatters;
|
||||
formatters.Add(typeof(JsonOutputFormatter));
|
||||
var formatter1 = new JsonOutputFormatter();
|
||||
var formatter2 = Mock.Of<IOutputFormatter>();
|
||||
var formatter3 = new JsonOutputFormatter();
|
||||
var formatter4 = Mock.Of<IOutputFormatter>();
|
||||
formatters.Add(formatter1);
|
||||
formatters.Add(formatter2);
|
||||
formatters.Add(formatter3);
|
||||
formatters.Add(formatter4);
|
||||
|
||||
var expectedFormatters = new List<JsonOutputFormatter> { formatter1, formatter3 };
|
||||
|
||||
// Act
|
||||
var jsonFormatters = formatters.InstancesOf<JsonOutputFormatter>().ToList();
|
||||
|
||||
// Assert
|
||||
Assert.NotEmpty(jsonFormatters);
|
||||
Assert.Equal(jsonFormatters, expectedFormatters);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,91 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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 System.Linq;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Core.Test
|
||||
{
|
||||
public class OutputFormatterDescriptorExtensionTest
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(-1)]
|
||||
[InlineData(5)]
|
||||
public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index)
|
||||
{
|
||||
// Arrange
|
||||
var collection = new List<OutputFormatterDescriptor>
|
||||
{
|
||||
new OutputFormatterDescriptor(Mock.Of<IOutputFormatter>()),
|
||||
new OutputFormatterDescriptor(Mock.Of<IOutputFormatter>())
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentOutOfRangeException>("index",
|
||||
() => collection.Insert(index, typeof(IOutputFormatter)));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-2)]
|
||||
[InlineData(3)]
|
||||
public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index)
|
||||
{
|
||||
// Arrange
|
||||
var collection = new List<OutputFormatterDescriptor>
|
||||
{
|
||||
new OutputFormatterDescriptor(Mock.Of<IOutputFormatter>()),
|
||||
new OutputFormatterDescriptor(Mock.Of<IOutputFormatter>())
|
||||
};
|
||||
var formatter = Mock.Of<IOutputFormatter>();
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentOutOfRangeException>("index", () => collection.Insert(index, formatter));
|
||||
}
|
||||
|
||||
[InlineData]
|
||||
public void OutputFormatterDescriptors_AddsTypesAndInstances()
|
||||
{
|
||||
// Arrange
|
||||
var formatter1 = Mock.Of<IOutputFormatter>();
|
||||
var formatter2 = Mock.Of<IOutputFormatter>();
|
||||
var type1 = typeof(JsonOutputFormatter);
|
||||
var type2 = typeof(OutputFormatter);
|
||||
var collection = new List<OutputFormatterDescriptor>();
|
||||
|
||||
// Act
|
||||
collection.Add(formatter1);
|
||||
collection.Insert(1, formatter2);
|
||||
collection.Add(type1);
|
||||
collection.Insert(2, type2);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(4, collection.Count);
|
||||
Assert.Equal(formatter1, collection[0].Instance);
|
||||
Assert.Equal(formatter2, collection[1].Instance);
|
||||
Assert.Equal(type2, collection[2].OptionType);
|
||||
Assert.Equal(type1, collection[3].OptionType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OutputFormatters_RemoveTypesOf_RemovesDescriptorsOfIOutputFormatter()
|
||||
{
|
||||
// Arrange
|
||||
var formatters = new MvcOptions().OutputFormatters;
|
||||
formatters.Add(new JsonOutputFormatter());
|
||||
formatters.Add(Mock.Of<IOutputFormatter>());
|
||||
formatters.Add(typeof(JsonOutputFormatter));
|
||||
formatters.Add(Mock.Of<IOutputFormatter>());
|
||||
|
||||
// Act
|
||||
formatters.RemoveTypesOf<JsonOutputFormatter>();
|
||||
|
||||
// Assert
|
||||
Assert.DoesNotContain(formatters, descriptor => descriptor.OptionType == typeof(JsonOutputFormatter));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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 System.Threading.Tasks;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Microsoft.AspNet.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Core
|
||||
{
|
||||
public class OutputFormatterDescriptorTest
|
||||
{
|
||||
[Fact]
|
||||
public void ConstructorThrows_IfTypeIsNotOutputFormatter()
|
||||
{
|
||||
// Arrange
|
||||
var expected = "The type 'System.String' must derive from " +
|
||||
"'Microsoft.AspNet.Mvc.IOutputFormatter'.";
|
||||
|
||||
var type = typeof(string);
|
||||
|
||||
// Act & Assert
|
||||
ExceptionAssert.ThrowsArgument(() => new OutputFormatterDescriptor(type), "type", expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstructorSets_OutputFormatterType()
|
||||
{
|
||||
// Arrange
|
||||
var type = typeof(TestOutputFormatter);
|
||||
|
||||
// Act
|
||||
var descriptor = new OutputFormatterDescriptor(type);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(type, descriptor.OptionType);
|
||||
Assert.Null(descriptor.Instance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstructorSets_OutputFormatterInsnaceAndType()
|
||||
{
|
||||
// Arrange
|
||||
var testFormatter = new TestOutputFormatter();
|
||||
|
||||
// Act
|
||||
var descriptor = new OutputFormatterDescriptor(testFormatter);
|
||||
|
||||
// Assert
|
||||
Assert.Same(testFormatter, descriptor.Instance);
|
||||
Assert.Equal(testFormatter.GetType(), descriptor.OptionType);
|
||||
}
|
||||
|
||||
private class TestOutputFormatter : IOutputFormatter
|
||||
{
|
||||
public bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task WriteAsync(OutputFormatterContext context)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class ValidationExcludeFiltersExtensionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void InputFormatterDescriptors_AddsTypesAndTypeNames()
|
||||
{
|
||||
// Arrange
|
||||
var type1 = typeof(BaseType);
|
||||
var collection = new List<ExcludeValidationDescriptor>();
|
||||
|
||||
// Act
|
||||
collection.Add(type1);
|
||||
collection.Add(type1.FullName);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, collection.Count);
|
||||
Assert.Equal(typeof(DefaultTypeBasedExcludeFilter), collection[0].OptionType);
|
||||
Assert.Equal(typeof(DefaultTypeNameBasedExcludeFilter), collection[1].OptionType);
|
||||
}
|
||||
|
||||
private class BaseType
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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 System.Linq;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Mvc.OptionDescriptors;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class ValueProviderFactoryDescriptorExtensionsTest
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(-1)]
|
||||
[InlineData(5)]
|
||||
public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index)
|
||||
{
|
||||
// Arrange
|
||||
var collection = new List<ValueProviderFactoryDescriptor>
|
||||
{
|
||||
new ValueProviderFactoryDescriptor(Mock.Of<IValueProviderFactory>()),
|
||||
new ValueProviderFactoryDescriptor(Mock.Of<IValueProviderFactory>())
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentOutOfRangeException>("index",
|
||||
() => collection.Insert(index, typeof(IValueProviderFactory)));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-2)]
|
||||
[InlineData(3)]
|
||||
public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index)
|
||||
{
|
||||
// Arrange
|
||||
var collection = new List<ValueProviderFactoryDescriptor>
|
||||
{
|
||||
new ValueProviderFactoryDescriptor(Mock.Of<IValueProviderFactory>()),
|
||||
new ValueProviderFactoryDescriptor(Mock.Of<IValueProviderFactory>())
|
||||
};
|
||||
var valueProviderFactory = Mock.Of<IValueProviderFactory>();
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentOutOfRangeException>("index", () => collection.Insert(index, valueProviderFactory));
|
||||
}
|
||||
|
||||
[InlineData]
|
||||
public void ValueProviderFactoryDescriptors_AddsTypesAndInstances()
|
||||
{
|
||||
// Arrange
|
||||
var valueProviderFactory = Mock.Of<IValueProviderFactory>();
|
||||
var type = typeof(TestValueProviderFactory);
|
||||
var collection = new List<ValueProviderFactoryDescriptor>();
|
||||
|
||||
// Act
|
||||
collection.Add(valueProviderFactory);
|
||||
collection.Insert(0, type);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, collection.Count);
|
||||
Assert.IsType<TestValueProviderFactory>(collection[0].Instance);
|
||||
Assert.Same(valueProviderFactory, collection[0].Instance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValueProviderFactories_RemoveTypesOf_RemovesDescriptorsOfIValueProviderFactory()
|
||||
{
|
||||
// Arrange
|
||||
var factories = new MvcOptions().ValueProviderFactories;
|
||||
factories.Add(new FormValueProviderFactory());
|
||||
factories.Add(Mock.Of<IValueProviderFactory>());
|
||||
factories.Add(typeof(FormValueProviderFactory));
|
||||
factories.Add(Mock.Of<IValueProviderFactory>());
|
||||
|
||||
// Act
|
||||
factories.RemoveTypesOf<FormValueProviderFactory>();
|
||||
|
||||
// Assert
|
||||
Assert.DoesNotContain(factories, descriptor => descriptor.OptionType == typeof(FormValueProviderFactory));
|
||||
}
|
||||
|
||||
private class TestValueProviderFactory : IValueProviderFactory
|
||||
{
|
||||
public IValueProvider GetValueProvider(ValueProviderFactoryContext context)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.OptionDescriptors
|
||||
{
|
||||
public class ValueProviderFactoryDescriptorTest
|
||||
{
|
||||
[Fact]
|
||||
public void ConstructorThrows_IfTypeIsNotViewEngine()
|
||||
{
|
||||
// Arrange
|
||||
var viewEngineType = typeof(IValueProviderFactory).FullName;
|
||||
var type = typeof(string);
|
||||
var expected = string.Format("The type '{0}' must derive from '{1}'.",
|
||||
type.FullName, viewEngineType);
|
||||
|
||||
// Act & Assert
|
||||
ExceptionAssert.ThrowsArgument(() => new ValueProviderFactoryDescriptor(type), "type", expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstructorSetsViewEngineType()
|
||||
{
|
||||
// Arrange
|
||||
var type = typeof(TestValueProviderFactory);
|
||||
|
||||
// Act
|
||||
var descriptor = new ValueProviderFactoryDescriptor(type);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(type, descriptor.OptionType);
|
||||
Assert.Null(descriptor.Instance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstructorSetsViewEngineAndViewEngineType()
|
||||
{
|
||||
// Arrange
|
||||
var viewEngine = new TestValueProviderFactory();
|
||||
|
||||
// Act
|
||||
var descriptor = new ValueProviderFactoryDescriptor(viewEngine);
|
||||
|
||||
// Assert
|
||||
Assert.Same(viewEngine, descriptor.Instance);
|
||||
Assert.Equal(viewEngine.GetType(), descriptor.OptionType);
|
||||
}
|
||||
|
||||
private class TestValueProviderFactory : IValueProviderFactory
|
||||
{
|
||||
public IValueProvider GetValueProvider(ValueProviderFactoryContext context)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue