Commonizing code in option provider

* Adding ValueProviderFactoryProvider and CompositeValueProviderFactory to
  maintain parity in pattern with other option types.

Fixes #818
This commit is contained in:
Pranav K 2014-07-21 12:24:38 -07:00
parent 92e26cf8e0
commit da0bf6f7d8
46 changed files with 905 additions and 411 deletions

View File

@ -1,61 +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;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Encapsulates information that describes an <see cref="OutputFormatter"/>.
/// </summary>
public class OutputFormatterDescriptor
{
/// <summary>
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
/// </summary>
/// <param name="outputFormatterType">A <see cref="OutputFormatter/> type that the descriptor represents.
/// </param>
public OutputFormatterDescriptor([NotNull] Type outputFormatterType)
{
var formatterType = typeof(OutputFormatter);
if (!formatterType.IsAssignableFrom(outputFormatterType))
{
var message = Resources.FormatTypeMustDeriveFromType(outputFormatterType,
formatterType.FullName);
throw new ArgumentException(message, "outputFormatterType");
}
OutputFormatterType = outputFormatterType;
}
/// <summary>
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
/// </summary>
/// <param name="outputFormatter">An instance of <see cref="OutputFormatter"/>
/// that the descriptor represents.</param>
public OutputFormatterDescriptor([NotNull] OutputFormatter outputFormatter)
{
OutputFormatter = outputFormatter;
OutputFormatterType = outputFormatter.GetType();
}
/// <summary>
/// Gets the type of the <see cref="OutputFormatter"/>.
/// </summary>
public Type OutputFormatterType
{
get;
private set;
}
/// <summary>
/// Gets the instance of the <see cref="OutputFormatter"/>.
/// </summary>
public OutputFormatter OutputFormatter
{
get;
private set;
}
}
}

View File

@ -29,14 +29,24 @@
<Compile Include="ActionDescriptor.cs" />
<Compile Include="ActionDescriptorProviderContext.cs" />
<Compile Include="ActionDescriptorsCollection.cs" />
<Compile Include="OptionDescriptors\DefaultModelBinderProvider.cs" />
<Compile Include="OptionDescriptors\DefaultValueProviderFactoryProvider.cs" />
<Compile Include="OptionDescriptors\DefaultViewEngineProvider.cs" />
<Compile Include="OptionDescriptors\ModelBinderDescriptor.cs" />
<Compile Include="OptionDescriptors\OutputFormatterDescriptorExtensions.cs" />
<Compile Include="OptionDescriptors\ModelBinderDescriptorExtensions.cs" />
<Compile Include="OptionDescriptors\OptionDescriptorBasedProvider.cs" />
<Compile Include="OptionDescriptors\OptionDescriptor.cs" />
<Compile Include="OptionDescriptors\OutputFormatterDescriptor.cs" />
<Compile Include="OptionDescriptors\ValueProviderFactoryDescriptor.cs" />
<Compile Include="OptionDescriptors\ViewEngineDescriptor.cs" />
<Compile Include="OptionDescriptors\ValueProviderFactoryDescriptorExtensions.cs" />
<Compile Include="OptionDescriptors\ViewEngineDescriptorExtensions.cs" />
<Compile Include="ExpiringFileInfoCache.cs" />
<Compile Include="Extensions\ViewEngineDescriptorExtensions.cs" />
<Compile Include="IExpiringFileInfoCache.cs" />
<Compile Include="Formatters\OutputFormatterContext.cs" />
<Compile Include="Formatters\JsonOutputFormatter.cs" />
<Compile Include="Formatters\OutputFormatter.cs" />
<Compile Include="Formatters\OutputFormatterDescriptor.cs" />
<Compile Include="OutputFormatterDescriptorExtensions.cs" />
<Compile Include="ReflectedActionDescriptor.cs" />
<Compile Include="ReflectedActionDescriptorProvider.cs" />
<Compile Include="ReflectedModelBuilder\IReflectedApplicationModelConvention.cs" />
@ -155,7 +165,6 @@
<Compile Include="ParameterBindingInfo.cs" />
<Compile Include="ParameterBinding\ActionBindingContext.cs" />
<Compile Include="ParameterBinding\DefaultActionBindingContextProvider.cs" />
<Compile Include="ParameterBinding\DefaultModelBindersProvider.cs" />
<Compile Include="ParameterBinding\FromBodyAttribute.cs" />
<Compile Include="ParameterBinding\IActionBindingContextProvider.cs" />
<Compile Include="ParameterDescriptor.cs" />
@ -166,7 +175,6 @@
<Compile Include="ReflectedActionInvokerProvider.cs" />
<Compile Include="ReflectedModelBuilder\ReflectedParameterModel.cs" />
<Compile Include="Rendering\CompositeViewEngine.cs" />
<Compile Include="Rendering\DefaultViewEngineProvider.cs" />
<Compile Include="Rendering\DynamicViewData.cs" />
<Compile Include="Rendering\Expressions\CachedExpressionCompiler.cs" />
<Compile Include="Rendering\Expressions\ExpressionHelper.cs" />
@ -214,7 +222,6 @@
<Compile Include="Rendering\SelectListGroup.cs" />
<Compile Include="Rendering\SelectListItem.cs" />
<Compile Include="Rendering\UnobtrusiveValidationAttributesGenerator.cs" />
<Compile Include="Rendering\ViewEngineDescriptor.cs" />
<Compile Include="Rendering\RazorViewEngineOptions.cs" />
<Compile Include="Rendering\ViewEngineResult.cs" />
<Compile Include="RouteAttribute.cs" />

View File

@ -4,9 +4,8 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.Core;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.OptionDescriptors;
using Microsoft.AspNet.Mvc.ReflectedModelBuilder;
using Microsoft.AspNet.Mvc.Rendering;
namespace Microsoft.AspNet.Mvc
{
@ -23,7 +22,7 @@ namespace Microsoft.AspNet.Mvc
ApplicationModelConventions = new List<IReflectedApplicationModelConvention>();
ModelBinders = new List<ModelBinderDescriptor>();
ViewEngines = new List<ViewEngineDescriptor>();
ValueProviderFactories = new List<IValueProviderFactory>();
ValueProviderFactories = new List<ValueProviderFactoryDescriptor>();
OutputFormatters = new List<OutputFormatterDescriptor>();
}
@ -86,9 +85,9 @@ namespace Microsoft.AspNet.Mvc
public List<ViewEngineDescriptor> ViewEngines { get; private set; }
/// <summary>
/// Gets a list of <see cref="IValueProviderFactory"/> used by this application.
/// Gets a list of descriptors that represent <see cref="IValueProviderFactory"/> used by this application.
/// </summary>
public List<IValueProviderFactory> ValueProviderFactories { get; private set; }
public List<ValueProviderFactoryDescriptor> ValueProviderFactories { get; private set; }
public List<IReflectedApplicationModelConvention> ApplicationModelConventions { get; private set; }
}

View File

@ -0,0 +1,39 @@
// 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.DependencyInjection;
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="typeActivator">An <see cref="ITypeActivator"/> instance used to instantiate types.</param>
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
/// service collection.</param>
public DefaultModelBindersProvider(
IOptionsAccessor<MvcOptions> optionsAccessor,
ITypeActivator typeActivator,
IServiceProvider serviceProvider)
: base(optionsAccessor.Options.ModelBinders, typeActivator, serviceProvider)
{
}
/// <inheritdoc />
public IReadOnlyList<IModelBinder> ModelBinders
{
get
{
return Options;
}
}
}
}

View File

@ -0,0 +1,40 @@
// 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.DependencyInjection;
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="typeActivator">An <see cref="ITypeActivator"/> instance used to instantiate types.</param>
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
/// service collection.</param>
public DefaultValueProviderFactoryProvider(
IOptionsAccessor<MvcOptions> optionsAccessor,
ITypeActivator typeActivator,
IServiceProvider serviceProvider)
: base(optionsAccessor.Options.ValueProviderFactories, typeActivator, serviceProvider)
{
}
/// <inheritdoc />
public IReadOnlyList<IValueProviderFactory> ValueProviderFactories
{
get
{
return Options;
}
}
}
}

View File

@ -0,0 +1,39 @@
// 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.DependencyInjection;
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="typeActivator">An <see cref="ITypeActivator"/> instance used to instantiate types.</param>
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
/// service collection.</param>
public DefaultViewEngineProvider(
IOptionsAccessor<MvcOptions> optionsAccessor,
ITypeActivator typeActivator,
IServiceProvider serviceProvider)
: base(optionsAccessor.Options.ViewEngines, typeActivator, serviceProvider)
{
}
/// <inheritdoc />
public IReadOnlyList<IViewEngine> ViewEngines
{
get
{
return Options;
}
}
}
}

View File

@ -0,0 +1,32 @@
// 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;
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"/>.
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"/>.
public ModelBinderDescriptor([NotNull] IModelBinder binder)
: base(binder)
{
}
}
}

View File

@ -3,8 +3,10 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.OptionDescriptors;
namespace Microsoft.AspNet.Mvc.ModelBinding
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Extension methods for adding model binders to a collection.

View File

@ -0,0 +1,61 @@
// 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;
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>
{
/// <summary>
/// Creates a new instance of <see cref="OptionDescriptor{TOption}"/>.
/// </summary>
/// <param name="type">A type that represents <typeparamref name="TOption"/>.
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 cref="TOption"/> that the descriptor represents.</param>
public OptionDescriptor([NotNull] TOption option)
{
Instance = option;
OptionType = option.GetType();
}
/// <summary>
/// Gets the type of the <typeparamref cref="TOption"/> described by this
/// <see cref="OptionDescriptor{TOption}"/>.
/// </summary>
public Type OptionType
{
get;
private set;
}
/// <summary>
/// Gets the instance of <typeparamref cref="TOption"/> described by this
/// <see cref="OptionDescriptor{TOption}"/>.
/// </summary>
public TOption Instance
{
get;
private set;
}
}
}

View File

@ -0,0 +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 Microsoft.Framework.DependencyInjection;
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 readonly ITypeActivator _typeActivator;
private readonly IServiceProvider _serviceProvider;
public OptionDescriptorBasedProvider(
[NotNull] IEnumerable<OptionDescriptor<TOption>> optionDescriptors,
[NotNull] ITypeActivator typeActivator,
[NotNull] IServiceProvider serviceProvider)
{
_optionDescriptors = optionDescriptors;
_typeActivator = typeActivator;
_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 = (TOption)_typeActivator.CreateInstance(_serviceProvider,
descriptor.OptionType);
}
result.Add(instance);
}
return result;
}
}
}
}

View File

@ -0,0 +1,33 @@
// 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 an <see cref="OutputFormatter"/>.
/// </summary>
public class OutputFormatterDescriptor : OptionDescriptor<OutputFormatter>
{
/// <summary>
/// Creates a new instance of <see cref="OutputFormatterDescriptor"/>.
/// </summary>
/// <param name="type">A <see cref="OutputFormatter/> 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="OutputFormatter"/>
/// that the descriptor represents.</param>
public OutputFormatterDescriptor([NotNull] OutputFormatter outputFormatter)
: base(outputFormatter)
{
}
}
}

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.OptionDescriptors;
namespace Microsoft.AspNet.Mvc
{
@ -18,7 +19,7 @@ namespace Microsoft.AspNet.Mvc
/// <param name="outputFormatterType">Type representing an <see cref="OutputFormatter"/>.</param>
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
public static OutputFormatterDescriptor Add([NotNull] this IList<OutputFormatterDescriptor> descriptors,
[NotNull] Type outputFormatterType)
[NotNull] Type outputFormatterType)
{
var descriptor = new OutputFormatterDescriptor(outputFormatterType);
descriptors.Add(descriptor);
@ -32,8 +33,8 @@ namespace Microsoft.AspNet.Mvc
/// <param name="outputFormatterType">Type representing an <see cref="OutputFormatter"/>.</param>
/// <returns>OutputFormatterDescriptor representing the inserted instance.</returns>
public static OutputFormatterDescriptor Insert([NotNull] this IList<OutputFormatterDescriptor> descriptors,
int index,
[NotNull] Type outputFormatterType)
int index,
[NotNull] Type outputFormatterType)
{
if (index < 0 || index > descriptors.Count)
{
@ -52,7 +53,7 @@ namespace Microsoft.AspNet.Mvc
/// <param name="outputFormatter">An <see cref="OutputFormatter"/> instance.</param>
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
public static OutputFormatterDescriptor Add([NotNull] this IList<OutputFormatterDescriptor> descriptors,
[NotNull] OutputFormatter outputFormatter)
[NotNull] OutputFormatter outputFormatter)
{
var descriptor = new OutputFormatterDescriptor(outputFormatter);
descriptors.Add(descriptor);
@ -66,8 +67,8 @@ namespace Microsoft.AspNet.Mvc
/// <param name="outputFormatter">An <see cref="OutputFormatter"/> instance.</param>
/// <returns>OutputFormatterDescriptor representing the added instance.</returns>
public static OutputFormatterDescriptor Insert([NotNull] this IList<OutputFormatterDescriptor> descriptors,
int index,
[NotNull] OutputFormatter outputFormatter)
int index,
[NotNull] OutputFormatter outputFormatter)
{
if (index < 0 || index > descriptors.Count)
{

View File

@ -0,0 +1,33 @@
// 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;
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)
{
}
}
}

View File

@ -0,0 +1,88 @@
// 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;
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;
}
}
}

View File

@ -0,0 +1,32 @@
// 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;
namespace Microsoft.AspNet.Mvc.OptionDescriptors
{
/// <summary>
/// Encapsulates information that describes an <see cref="IViewEngine"/>.
/// </summary>
public class ViewEngineDescriptor : OptionDescriptor<IViewEngine>
{
/// <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)
{
}
/// <summary>
/// Creates a new instance of <see cref="ViewEngineDescriptor"/> using the specified type.
/// </summary>
/// <param name="viewEngine">An instance of <see cref="IViewEngine"/> that the descriptor represents.</param>
public ViewEngineDescriptor([NotNull] IViewEngine viewEngine)
: base(viewEngine)
{
}
}
}

View File

@ -3,12 +3,13 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.OptionDescriptors;
using Microsoft.AspNet.Mvc.Rendering;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Extension methods for adding model binders to a collection.
/// Extension methods for adding view engines to a descriptor collection.
/// </summary>
public static class ViewEngineDescriptorExtensions
{

View File

@ -3,10 +3,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Mvc
{
@ -14,20 +12,20 @@ namespace Microsoft.AspNet.Mvc
{
private readonly IModelMetadataProvider _modelMetadataProvider;
private readonly ICompositeModelBinder _compositeModelBinder;
private readonly IEnumerable<IValueProviderFactory> _valueProviderFactories;
private readonly IValueProviderFactory _compositeValueProviderFactory;
private readonly IInputFormatterProvider _inputFormatterProvider;
private readonly IEnumerable<IModelValidatorProvider> _validatorProviders;
private Tuple<ActionContext, ActionBindingContext> _bindingContext;
public DefaultActionBindingContextProvider(IModelMetadataProvider modelMetadataProvider,
ICompositeModelBinder compositeModelBinder,
IOptionsAccessor<MvcOptions> mvcOptionsAccessor,
ICompositeValueProviderFactory compositeValueProviderFactory,
IInputFormatterProvider inputFormatterProvider,
IEnumerable<IModelValidatorProvider> validatorProviders)
{
_modelMetadataProvider = modelMetadataProvider;
_compositeModelBinder = compositeModelBinder;
_valueProviderFactories = mvcOptionsAccessor.Options.ValueProviderFactories;
_compositeValueProviderFactory = compositeValueProviderFactory;
_inputFormatterProvider = inputFormatterProvider;
_validatorProviders = validatorProviders;
}
@ -46,14 +44,13 @@ namespace Microsoft.AspNet.Mvc
actionContext.HttpContext,
actionContext.RouteData.Values);
var valueProviders = _valueProviderFactories.Select(factory => factory.GetValueProvider(factoryContext))
.Where(vp => vp != null);
var valueProvider = _compositeValueProviderFactory.GetValueProvider(factoryContext);
var context = new ActionBindingContext(
actionContext,
_modelMetadataProvider,
_compositeModelBinder,
new CompositeValueProvider(valueProviders),
valueProvider,
_inputFormatterProvider,
_validatorProviders);

View File

@ -1,57 +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.Framework.DependencyInjection;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Mvc
{
/// <inheritdoc />
public class DefaultModelBindersProvider : IModelBindersProvider
{
private readonly List<ModelBinderDescriptor> _descriptors;
private readonly ITypeActivator _typeActivator;
private readonly IServiceProvider _serviceProvider;
/// <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="typeActivator">An <see cref="ITypeActivator"/> instance used to instantiate types.</param>
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
/// service collection.</param>
public DefaultModelBindersProvider(IOptionsAccessor<MvcOptions> options,
ITypeActivator typeActivator,
IServiceProvider serviceProvider)
{
_descriptors = options.Options.ModelBinders;
_typeActivator = typeActivator;
_serviceProvider = serviceProvider;
}
/// <inheritdoc />
public IReadOnlyList<IModelBinder> ModelBinders
{
get
{
var binders = new List<IModelBinder>();
foreach (var descriptor in _descriptors)
{
var binder = descriptor.ModelBinder;
if (binder == null)
{
binder = (IModelBinder)_typeActivator.CreateInstance(_serviceProvider,
descriptor.ModelBinderType);
}
binders.Add(binder);
}
return binders;
}
}
}
}

View File

@ -1,50 +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.OptionsModel;
namespace Microsoft.AspNet.Mvc.Rendering
{
/// <inheritdoc />
public class DefaultViewEngineProvider : IViewEngineProvider
{
private readonly IList<ViewEngineDescriptor> _descriptors;
private readonly ITypeActivator _typeActivator;
private readonly IServiceProvider _serviceProvider;
public DefaultViewEngineProvider(
ITypeActivator typeActivator,
IServiceProvider serviceProvider,
IOptionsAccessor<MvcOptions> options)
{
_typeActivator = typeActivator;
_serviceProvider = serviceProvider;
_descriptors = options.Options.ViewEngines;
}
/// <inheritdoc />
public IReadOnlyList<IViewEngine> ViewEngines
{
get
{
var viewEngines = new List<IViewEngine>(_descriptors.Count);
foreach (var descriptor in _descriptors)
{
var viewEngine = descriptor.ViewEngine;
if (viewEngine == null)
{
viewEngine = (IViewEngine)_typeActivator.CreateInstance(_serviceProvider,
descriptor.ViewEngineType);
}
viewEngines.Add(viewEngine);
}
return viewEngines;
}
}
}
}

View File

@ -1,58 +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;
namespace Microsoft.AspNet.Mvc.Rendering
{
/// <summary>
/// Encapsulates information that describes an <see cref="IViewEngine"/>.
/// </summary>
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)
{
var viewEngineType = typeof(IViewEngine);
if (!viewEngineType.IsAssignableFrom(type))
{
var message = Resources.FormatTypeMustDeriveFromType(type.FullName, viewEngineType.FullName);
throw new ArgumentException(message, "type");
}
ViewEngineType = type;
}
/// <summary>
/// Creates a new instance of <see cref="ViewEngineDescriptor"/>.
/// </summary>
/// <param name="viewEngine">An instance of <see cref="IViewEngine"/> that the descriptor represents.</param>
public ViewEngineDescriptor([NotNull] IViewEngine viewEngine)
{
ViewEngine = viewEngine;
ViewEngineType = viewEngine.GetType();
}
/// <summary>
/// Gets the type of the <see cref="IViewEngine"/>.
/// </summary>
public Type ViewEngineType
{
get;
private set;
}
/// <summary>
/// Gets the instance of the <see cref="IViewEngine"/>.
/// </summary>
public IViewEngine ViewEngine
{
get;
private set;
}
}
}

View File

@ -16,14 +16,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </remarks>
public class CompositeModelBinder : ICompositeModelBinder
{
private readonly IModelBindersProvider _modelBindersProvider;
private readonly IModelBinderProvider _modelBindersProvider;
private IReadOnlyList<IModelBinder> _binders;
/// <summary>
/// Initializes a new instance of the CompositeModelBinder class.
/// </summary>
/// <param name="modelBindersProvider">Provides a collection of <see cref="IModelBinder"/> instances.</param>
public CompositeModelBinder(IModelBindersProvider modelBindersProvider)
public CompositeModelBinder(IModelBinderProvider modelBindersProvider)
{
_modelBindersProvider = modelBindersProvider;
}

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <summary>
/// Provides an activated collection of <see cref="IModelBinder"/> instances.
/// </summary>
public interface IModelBindersProvider
public interface IModelBinderProvider
{
/// <summary>
/// Gets a collection of activated ModelBinders instances.

View File

@ -1,57 +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.ModelBinding
{
/// <summary>
/// Encapsulates information that describes an <see cref="IModelBinder"/>.
/// </summary>
public class ModelBinderDescriptor
{
/// <summary>
/// Creates a new instance of <see cref="ModelBinderDescriptor"/>.
/// </summary>
/// <param name="modelBinderType">A <see cref="IModelBinder/> type that the descriptor represents.</param>
public ModelBinderDescriptor([NotNull] Type modelBinderType)
{
var binderType = typeof(IModelBinder);
if (!binderType.IsAssignableFrom(modelBinderType))
{
var message = Resources.FormatTypeMustDeriveFromType(modelBinderType, binderType.FullName);
throw new ArgumentException(message, "modelBinderType");
}
ModelBinderType = modelBinderType;
}
/// <summary>
/// Creates a new instance of <see cref="ModelBinderDescriptor"/>.
/// </summary>
/// <param name="modelBinder">An instance of <see cref="IModelBinder"/> that the descriptor represents.</param>
public ModelBinderDescriptor([NotNull] IModelBinder modelBinder)
{
ModelBinder = modelBinder;
ModelBinderType = modelBinder.GetType();
}
/// <summary>
/// Gets the type of the <see cref="IModelBinder"/>.
/// </summary>
public Type ModelBinderType
{
get;
private set;
}
/// <summary>
/// Gets the instance of the <see cref="IModelBinder"/>.
/// </summary>
public IModelBinder ModelBinder
{
get;
private set;
}
}
}

View File

@ -35,10 +35,8 @@
<Compile Include="Binders\GenericModelBinder.cs" />
<Compile Include="Binders\ICompositeModelBinder.cs" />
<Compile Include="Binders\IModelBinder.cs" />
<Compile Include="Binders\IModelBindersProvider.cs" />
<Compile Include="Binders\IModelBinderProvider.cs" />
<Compile Include="Binders\KeyValuePairModelBinder.cs" />
<Compile Include="Binders\ModelBinderDescriptor.cs" />
<Compile Include="Binders\ModelBinderDescriptorExtensions.cs" />
<Compile Include="Binders\MutableObjectModelBinder.cs" />
<Compile Include="Binders\TypeConverterModelBinder.cs" />
<Compile Include="Binders\TypeMatchModelBinder.cs" />
@ -110,11 +108,14 @@
<Compile Include="Validation\RequiredMemberModelValidator.cs" />
<Compile Include="Validation\ValidatableObjectAdapter.cs" />
<Compile Include="ValueProviders\CompositeValueProvider.cs" />
<Compile Include="ValueProviders\CompositeValueProviderFactory.cs" />
<Compile Include="ValueProviders\DictionaryBasedValueProvider.cs" />
<Compile Include="ValueProviders\ElementalValueProvider.cs" />
<Compile Include="ValueProviders\FormValueProviderFactory.cs" />
<Compile Include="ValueProviders\ICompositeValueProviderFactory.cs" />
<Compile Include="ValueProviders\IEnumerableValueProvider.cs" />
<Compile Include="ValueProviders\IValueProviderFactory.cs" />
<Compile Include="ValueProviders\IValueProviderFactoryProvider.cs" />
<Compile Include="ValueProviders\IValueProviders.cs" />
<Compile Include="ValueProviders\QueryStringValueProviderFactory.cs" />
<Compile Include="ValueProviders\ReadableStringCollectionValueProvider.cs" />

View File

@ -0,0 +1,28 @@
// 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;
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);
}
}
}

View File

@ -0,0 +1,12 @@
// 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.
namespace Microsoft.AspNet.Mvc.ModelBinding
{
/// <summary>
/// Represents an aggregate of <see cref="IValueProviderFactory"/>.
/// </summary>
public interface ICompositeValueProviderFactory : IValueProviderFactory
{
}
}

View File

@ -0,0 +1,15 @@
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; }
}
}

View File

@ -38,8 +38,8 @@ namespace Microsoft.AspNet.Mvc
options.ValueProviderFactories.Add(new FormValueProviderFactory());
// Set up OutputFormatters
options.OutputFormatters.Add(new OutputFormatterDescriptor(
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), indent: false)));
options.OutputFormatters.Add(
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), indent: false));
}
}
}

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using Microsoft.AspNet.Mvc.Core;
using Microsoft.AspNet.Mvc.Filters;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.OptionDescriptors;
using Microsoft.AspNet.Mvc.Razor;
using Microsoft.AspNet.Mvc.Razor.Compilation;
using Microsoft.AspNet.Mvc.Rendering;
@ -63,8 +64,10 @@ namespace Microsoft.AspNet.Mvc
yield return describe.Transient<IInputFormatter, JsonInputFormatter>();
yield return describe.Transient<IInputFormatterProvider, TempInputFormatterProvider>();
yield return describe.Transient<IModelBindersProvider, DefaultModelBindersProvider>();
yield return describe.Transient<ICompositeModelBinder, CompositeModelBinder>();
yield return describe.Transient<IModelBinderProvider, DefaultModelBindersProvider>();
yield return describe.Scoped<ICompositeModelBinder, CompositeModelBinder>();
yield return describe.Transient<IValueProviderFactoryProvider, DefaultValueProviderFactoryProvider>();
yield return describe.Scoped<ICompositeValueProviderFactory, CompositeValueProviderFactory>();
yield return describe.Transient<INestedProvider<FilterProviderContext>, DefaultFilterProvider>();

View File

@ -30,11 +30,19 @@
<Compile Include="ActionResults\RedirectResultTest.cs" />
<Compile Include="DefaultActionDiscoveryConventionsActionSelectionTests.cs" />
<Compile Include="AntiXsrf\AntiForgeryOptionsTests.cs" />
<Compile Include="OptionDescriptors\DefaultModelBindersProviderTest.cs" />
<Compile Include="OptionDescriptors\DefaultValueProviderFactoryProviderTest.cs" />
<Compile Include="OptionDescriptors\DefaultViewEngineProviderTest.cs" />
<Compile Include="OptionDescriptors\ModelBinderDescriptorExtensionsTest.cs" />
<Compile Include="OptionDescriptors\ModelBinderDescriptorTest.cs" />
<Compile Include="OptionDescriptors\OutputFormatterDescriptorExtensionTest.cs" />
<Compile Include="OptionDescriptors\OutputFormatterDescriptorTest.cs" />
<Compile Include="OptionDescriptors\ValueProviderFactoryDescriptorTest.cs" />
<Compile Include="OptionDescriptors\ViewEngineDescriptorTest.cs" />
<Compile Include="OptionDescriptors\ValueProviderFactoryDescriptorExtensionsTest.cs" />
<Compile Include="OptionDescriptors\ViewEngineDscriptorExtensionsTest.cs" />
<Compile Include="ExpiringFileInfoCacheTest.cs" />
<Compile Include="DefaultActionDiscoveryConventionsTests.cs" />
<Compile Include="Extensions\ViewEngineDscriptorExtensionsTest.cs" />
<Compile Include="Formatters\OutputFormatterDescriptorExtensionTest.cs" />
<Compile Include="Formatters\OutputFormatterDescriptorTest.cs" />
<Compile Include="Formatters\OutputFormatterTests.cs" />
<Compile Include="ReflectedModelBuilder\ReflectedParameterModelTests.cs" />
<Compile Include="ReflectedModelBuilder\ReflectedActionModelTests.cs" />
@ -61,7 +69,6 @@
<Compile Include="DefaultControllerFactoryTest.cs" />
<Compile Include="Filters\ResultFilterAttributeTest.cs" />
<Compile Include="JsonResultTest.cs" />
<Compile Include="ParameterBinding\DefaultModelBindersProviderTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PropertyHelperTest.cs" />
<Compile Include="ReflectedActionDescriptorProviderTests.cs" />
@ -77,8 +84,6 @@
<Compile Include="Rendering\ViewContextTests.cs" />
<Compile Include="Rendering\ViewDataOfTTest.cs" />
<Compile Include="KnownRouteValueConstraintTests.cs" />
<Compile Include="Rendering\ViewEngineDescriptorTest.cs" />
<Compile Include="Rendering\DefaultViewEngineProviderTest.cs" />
<Compile Include="Routing\AttributeRoutePrecedenceTests.cs" />
<Compile Include="Routing\AttributeRouteTemplateTests.cs" />
<Compile Include="StaticControllerAssemblyProvider.cs" />
@ -91,4 +96,4 @@
<Compile Include="ViewComponentTests.cs" />
</ItemGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
</Project>

View File

@ -1,15 +1,15 @@
// 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.
#if NET45
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.Core
namespace Microsoft.AspNet.Mvc.OptionDescriptors
{
public class DefaultModelBindersProviderTest
{
@ -17,21 +17,20 @@ namespace Microsoft.AspNet.Mvc.Core
public void DefaultModelBindersProvider_ProvidesInstancesOfModelBinders()
{
// Arrange
var service = Mock.Of<ITestService>();
var binder = new TypeMatchModelBinder();
var options = new MvcOptions();
options.ModelBinders.Clear();
options.ModelBinders.Add(binder);
options.ModelBinders.Add(typeof(GenericModelBinder));
options.ModelBinders.Add(typeof(TestModelBinder));
var optionsAccessor = new Mock<IOptionsAccessor<MvcOptions>>();
optionsAccessor.SetupGet(o => o.Options)
.Returns(options);
var activator = new Mock<ITypeActivator>();
var serviceProvider = Mock.Of<IServiceProvider>();
activator.Setup(a => a.CreateInstance(serviceProvider, typeof(GenericModelBinder)))
.Returns(new GenericModelBinder(serviceProvider, activator.Object))
.Verifiable();
var activator = new TypeActivator();
var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(p => p.GetService(typeof(ITestService)))
.Returns(service);
var provider = new DefaultModelBindersProvider(optionsAccessor.Object, activator.Object, serviceProvider);
var provider = new DefaultModelBindersProvider(optionsAccessor.Object, activator, serviceProvider.Object);
// Act
var binders = provider.ModelBinders;
@ -39,8 +38,27 @@ namespace Microsoft.AspNet.Mvc.Core
// Assert
Assert.Equal(2, binders.Count);
Assert.Same(binder, binders[0]);
Assert.IsType<GenericModelBinder>(binders[1]);
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<bool> BindModelAsync(ModelBindingContext bindingContext)
{
throw new NotImplementedException();
}
}
public interface ITestService
{
}
}
}
#endif

View File

@ -0,0 +1,65 @@
// 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.DependencyInjection;
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 typeActivator = new TypeActivator();
var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(p => p.GetService(typeof(ITestService)))
.Returns(service);
var options = new MvcOptions();
options.ValueProviderFactories.Add(valueProviderFactory);
options.ValueProviderFactories.Add(type);
var accessor = new Mock<IOptionsAccessor<MvcOptions>>();
accessor.SetupGet(a => a.Options)
.Returns(options);
var provider = new DefaultValueProviderFactoryProvider(accessor.Object,
typeActivator,
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
{
}
}
}

View File

@ -1,15 +1,14 @@
// 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.
#if NET45
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.OptionsModel;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc.Rendering
namespace Microsoft.AspNet.Mvc.OptionDescriptors
{
public class DefaultViewEngineProviderTest
{
@ -30,7 +29,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
var accessor = new Mock<IOptionsAccessor<MvcOptions>>();
accessor.SetupGet(a => a.Options)
.Returns(options);
var provider = new DefaultViewEngineProvider(typeActivator, serviceProvider.Object, accessor.Object);
var provider = new DefaultViewEngineProvider(accessor.Object, typeActivator, serviceProvider.Object);
// Act
var result = provider.ViewEngines;
@ -67,4 +66,3 @@ namespace Microsoft.AspNet.Mvc.Rendering
}
}
}
#endif

View File

@ -1,13 +1,14 @@
// 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.
#if NET45
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.OptionDescriptors;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding
namespace Microsoft.AspNet.Mvc
{
public class ModelBinderDescriptorExtensionTest
{
@ -62,11 +63,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Assert
Assert.Equal(4, collection.Count);
Assert.Equal(binder1, collection[0].ModelBinder);
Assert.Equal(binder2, collection[1].ModelBinder);
Assert.Equal(type2, collection[2].ModelBinderType);
Assert.Equal(type1, collection[3].ModelBinderType);
Assert.Equal(binder1, collection[0].Instance);
Assert.Equal(binder2, collection[1].Instance);
Assert.Equal(type2, collection[2].OptionType);
Assert.Equal(type1, collection[3].OptionType);
}
}
}
#endif

View File

@ -0,0 +1,62 @@
// 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<bool> BindModelAsync(ModelBindingContext bindingContext)
{
throw new NotImplementedException();
}
}
}
}

View File

@ -1,9 +1,9 @@
// 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.
#if NET45
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.OptionDescriptors;
using Moq;
using Xunit;
@ -24,7 +24,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
};
// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>("index",
Assert.Throws<ArgumentOutOfRangeException>("index",
() => collection.Insert(index, typeof(OutputFormatter)));
}
@ -63,11 +63,10 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.Equal(4, collection.Count);
Assert.Equal(formatter1, collection[0].OutputFormatter);
Assert.Equal(formatter2, collection[1].OutputFormatter);
Assert.Equal(type2, collection[2].OutputFormatterType);
Assert.Equal(type1, collection[3].OutputFormatterType);
Assert.Equal(formatter1, collection[0].Instance);
Assert.Equal(formatter2, collection[1].Instance);
Assert.Equal(type2, collection[2].OptionType);
Assert.Equal(type1, collection[3].OptionType);
}
}
}
#endif

View File

@ -1,10 +1,11 @@
// 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 Microsoft.AspNet.Mvc.OptionDescriptors;
using Microsoft.AspNet.Testing;
using Xunit;
namespace Microsoft.AspNet.Mvc.Core.Test
namespace Microsoft.AspNet.Mvc.Core
{
public class OutputFormatterDescriptorTest
{
@ -18,7 +19,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
var type = typeof(string);
// Act & Assert
ExceptionAssert.ThrowsArgument(() => new OutputFormatterDescriptor(type), "outputFormatterType", expected);
ExceptionAssert.ThrowsArgument(() => new OutputFormatterDescriptor(type), "type", expected);
}
}
}

View File

@ -0,0 +1,75 @@
// 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 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);
}
private class TestValueProviderFactory : IValueProviderFactory
{
public IValueProvider GetValueProvider(ValueProviderFactoryContext context)
{
throw new NotImplementedException();
}
}
}
}

View File

@ -0,0 +1,62 @@
// 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();
}
}
}
}

View File

@ -2,11 +2,11 @@
// 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.AspNet.Testing;
using Xunit;
namespace Microsoft.AspNet.Mvc.Rendering
namespace Microsoft.AspNet.Mvc.OptionDescriptors
{
public class ViewEngineDescriptorTest
{
@ -33,8 +33,8 @@ namespace Microsoft.AspNet.Mvc.Rendering
var descriptor = new ViewEngineDescriptor(type);
// Assert
Assert.Equal(type, descriptor.ViewEngineType);
Assert.Null(descriptor.ViewEngine);
Assert.Equal(type, descriptor.OptionType);
Assert.Null(descriptor.Instance);
}
[Fact]
@ -47,8 +47,8 @@ namespace Microsoft.AspNet.Mvc.Rendering
var descriptor = new ViewEngineDescriptor(viewEngine);
// Assert
Assert.Same(viewEngine, descriptor.ViewEngine);
Assert.Equal(viewEngine.GetType(), descriptor.ViewEngineType);
Assert.Same(viewEngine, descriptor.Instance);
Assert.Equal(viewEngine.GetType(), descriptor.OptionType);
}
private class TestViewEngine : IViewEngine

View File

@ -1,13 +1,14 @@
// 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.
#if NET45
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.OptionDescriptors;
using Microsoft.AspNet.Mvc.Rendering;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc.Rendering
namespace Microsoft.AspNet.Mvc
{
public class ViewEngineDescriptorExtensionTest
{
@ -58,8 +59,8 @@ namespace Microsoft.AspNet.Mvc.Rendering
// Assert
Assert.Equal(2, collection.Count);
Assert.IsType<TestViewEngine>(collection[0].ViewEngine);
Assert.Same(viewEngine, collection[0].ViewEngine);
Assert.IsType<TestViewEngine>(collection[0].Instance);
Assert.Same(viewEngine, collection[0].Instance);
}
private class TestViewEngine : IViewEngine
@ -76,4 +77,3 @@ namespace Microsoft.AspNet.Mvc.Rendering
}
}
}
#endif

View File

@ -299,7 +299,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
new TypeConverterModelBinder(),
new MutableObjectModelBinder()
};
var binderProviders = new Mock<IModelBindersProvider>();
var binderProviders = new Mock<IModelBinderProvider>();
binderProviders.SetupGet(p => p.ModelBinders)
.Returns(binders);
var binder = new CompositeModelBinder(binderProviders.Object);
@ -308,7 +308,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
private static CompositeModelBinder CreateCompositeBinder(IModelBinder mockIntBinder)
{
var binderProvider = new Mock<IModelBindersProvider>();
var binderProvider = new Mock<IModelBinderProvider>();
binderProvider.SetupGet(p => p.ModelBinders)
.Returns(new[] { mockIntBinder });

View File

@ -50,7 +50,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
public async Task BindModel_SubBindingSucceeds()
{
// Arrange
var binderProviders = new Mock<IModelBindersProvider>();
var binderProviders = new Mock<IModelBinderProvider>();
binderProviders.SetupGet(b => b.ModelBinders)
.Returns(new[] { CreateStringBinder(), CreateIntBinder() });
var innerBinder = new CompositeModelBinder(binderProviders.Object);

View File

@ -1,24 +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.Testing;
using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
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), "modelBinderType", expected);
}
}
}

View File

@ -29,8 +29,6 @@
<Compile Include="Binders\CompositeModelBinderTest.cs" />
<Compile Include="Binders\DictionaryModelBinderTest.cs" />
<Compile Include="Binders\KeyValuePairModelBinderTest.cs" />
<Compile Include="Binders\ModelBinderDescriptorExtensionsTest.cs" />
<Compile Include="Binders\ModelBinderDescriptorTest.cs" />
<Compile Include="Binders\ModelBindingContextTest.cs" />
<Compile Include="Binders\MutableObjectModelBinderTest.cs" />
<Compile Include="Binders\TypeConverterModelBinderTest.cs" />

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.Equal(1, mvcOptions.ViewEngines.Count);
Assert.Equal(typeof(RazorViewEngine), mvcOptions.ViewEngines[0].ViewEngineType);
Assert.Equal(typeof(RazorViewEngine), mvcOptions.ViewEngines[0].OptionType);
}
[Fact]
@ -36,11 +36,11 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.Equal(5, mvcOptions.ModelBinders.Count);
Assert.Equal(typeof(TypeConverterModelBinder), mvcOptions.ModelBinders[0].ModelBinderType);
Assert.Equal(typeof(TypeMatchModelBinder), mvcOptions.ModelBinders[1].ModelBinderType);
Assert.Equal(typeof(GenericModelBinder), mvcOptions.ModelBinders[2].ModelBinderType);
Assert.Equal(typeof(MutableObjectModelBinder), mvcOptions.ModelBinders[3].ModelBinderType);
Assert.Equal(typeof(ComplexModelDtoModelBinder), mvcOptions.ModelBinders[4].ModelBinderType);
Assert.Equal(typeof(TypeConverterModelBinder), mvcOptions.ModelBinders[0].OptionType);
Assert.Equal(typeof(TypeMatchModelBinder), mvcOptions.ModelBinders[1].OptionType);
Assert.Equal(typeof(GenericModelBinder), mvcOptions.ModelBinders[2].OptionType);
Assert.Equal(typeof(MutableObjectModelBinder), mvcOptions.ModelBinders[3].OptionType);
Assert.Equal(typeof(ComplexModelDtoModelBinder), mvcOptions.ModelBinders[4].OptionType);
}
[Fact]
@ -56,11 +56,11 @@ namespace Microsoft.AspNet.Mvc
// Assert
var valueProviders = mvcOptions.ValueProviderFactories;
Assert.Equal(3, valueProviders.Count);
Assert.IsType<RouteValueValueProviderFactory>(valueProviders[0]);
Assert.IsType<QueryStringValueProviderFactory>(valueProviders[1]);
Assert.IsType<FormValueProviderFactory>(valueProviders[2]);
Assert.IsType<RouteValueValueProviderFactory>(valueProviders[0].Instance);
Assert.IsType<QueryStringValueProviderFactory>(valueProviders[1].Instance);
Assert.IsType<FormValueProviderFactory>(valueProviders[2].Instance);
}
[Fact]
public void Setup_SetsUpOutputFormatters()
{
@ -73,7 +73,7 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.Equal(1, mvcOptions.OutputFormatters.Count);
Assert.IsType<JsonOutputFormatter>(mvcOptions.OutputFormatters[0].OutputFormatter);
Assert.IsType<JsonOutputFormatter>(mvcOptions.OutputFormatters[0].Instance);
}
}
}