diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/OutputFormatterDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/OutputFormatterDescriptor.cs deleted file mode 100644 index 9880a34570..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/Formatters/OutputFormatterDescriptor.cs +++ /dev/null @@ -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 -{ - /// - /// Encapsulates information that describes an . - /// - public class OutputFormatterDescriptor - { - /// - /// Creates a new instance of . - /// - /// A - /// Creates a new instance of . - /// - /// An instance of - /// that the descriptor represents. - public OutputFormatterDescriptor([NotNull] OutputFormatter outputFormatter) - { - OutputFormatter = outputFormatter; - OutputFormatterType = outputFormatter.GetType(); - } - - /// - /// Gets the type of the . - /// - public Type OutputFormatterType - { - get; - private set; - } - - /// - /// Gets the instance of the . - /// - public OutputFormatter OutputFormatter - { - get; - private set; - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj b/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj index 031afde33d..5b4e537161 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj +++ b/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj @@ -29,14 +29,24 @@ + + + + + + + + + + + + + - - - @@ -155,7 +165,6 @@ - @@ -166,7 +175,6 @@ - @@ -214,7 +222,6 @@ - diff --git a/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs b/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs index c4ad9da725..6ea920b921 100644 --- a/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs @@ -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(); ModelBinders = new List(); ViewEngines = new List(); - ValueProviderFactories = new List(); + ValueProviderFactories = new List(); OutputFormatters = new List(); } @@ -86,9 +85,9 @@ namespace Microsoft.AspNet.Mvc public List ViewEngines { get; private set; } /// - /// Gets a list of used by this application. + /// Gets a list of descriptors that represent used by this application. /// - public List ValueProviderFactories { get; private set; } + public List ValueProviderFactories { get; private set; } public List ApplicationModelConventions { get; private set; } } diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultModelBinderProvider.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultModelBinderProvider.cs new file mode 100644 index 0000000000..86ca59e69e --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultModelBinderProvider.cs @@ -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 +{ + /// + public class DefaultModelBindersProvider : OptionDescriptorBasedProvider, IModelBinderProvider + { + /// + /// Initializes a new instance of the DefaultModelBindersProvider class. + /// + /// An accessor to the configured for this application. + /// An instance used to instantiate types. + /// A instance that retrieves services from the + /// service collection. + public DefaultModelBindersProvider( + IOptionsAccessor optionsAccessor, + ITypeActivator typeActivator, + IServiceProvider serviceProvider) + : base(optionsAccessor.Options.ModelBinders, typeActivator, serviceProvider) + { + } + + /// + public IReadOnlyList ModelBinders + { + get + { + return Options; + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultValueProviderFactoryProvider.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultValueProviderFactoryProvider.cs new file mode 100644 index 0000000000..a9c9efab10 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultValueProviderFactoryProvider.cs @@ -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 +{ + /// + public class DefaultValueProviderFactoryProvider : + OptionDescriptorBasedProvider, IValueProviderFactoryProvider + { + /// + /// Initializes a new instance of the class. + /// + /// An accessor to the configured for this application. + /// An instance used to instantiate types. + /// A instance that retrieves services from the + /// service collection. + public DefaultValueProviderFactoryProvider( + IOptionsAccessor optionsAccessor, + ITypeActivator typeActivator, + IServiceProvider serviceProvider) + : base(optionsAccessor.Options.ValueProviderFactories, typeActivator, serviceProvider) + { + } + + /// + public IReadOnlyList ValueProviderFactories + { + get + { + return Options; + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultViewEngineProvider.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultViewEngineProvider.cs new file mode 100644 index 0000000000..87ad8c1bed --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultViewEngineProvider.cs @@ -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 +{ + /// + public class DefaultViewEngineProvider : OptionDescriptorBasedProvider, IViewEngineProvider + { + /// + /// Initializes a new instance of the class. + /// + /// An accessor to the configured for this application. + /// An instance used to instantiate types. + /// A instance that retrieves services from the + /// service collection. + public DefaultViewEngineProvider( + IOptionsAccessor optionsAccessor, + ITypeActivator typeActivator, + IServiceProvider serviceProvider) + : base(optionsAccessor.Options.ViewEngines, typeActivator, serviceProvider) + { + } + + /// + public IReadOnlyList ViewEngines + { + get + { + return Options; + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelBinderDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelBinderDescriptor.cs new file mode 100644 index 0000000000..01c156919e --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelBinderDescriptor.cs @@ -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 +{ + /// + /// Encapsulates information that describes an . + /// + public class ModelBinderDescriptor : OptionDescriptor + { + /// + /// Creates a new instance of . + /// + /// A type that represents a . + public ModelBinderDescriptor([NotNull] Type type) + : base(type) + { + } + + /// + /// Creates a new instance of with the specified instance. + /// + /// An instance of . + public ModelBinderDescriptor([NotNull] IModelBinder binder) + : base(binder) + { + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/ModelBinderDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelBinderDescriptorExtensions.cs similarity index 96% rename from src/Microsoft.AspNet.Mvc.ModelBinding/Binders/ModelBinderDescriptorExtensions.cs rename to src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelBinderDescriptorExtensions.cs index 8fe9a02883..5ef0d6c8ee 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/ModelBinderDescriptorExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelBinderDescriptorExtensions.cs @@ -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 { /// /// Extension methods for adding model binders to a collection. diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptor.cs new file mode 100644 index 0000000000..be9a01f68a --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptor.cs @@ -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 +{ + /// + /// Encapsulates information that describes a option on . + /// + /// The type of the option. + public class OptionDescriptor + { + /// + /// Creates a new instance of . + /// + /// A type that represents . + 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; + } + + /// + /// Creates a new instance of with the specified instance. + /// + /// An instance of that the descriptor represents. + public OptionDescriptor([NotNull] TOption option) + { + Instance = option; + OptionType = option.GetType(); + } + + /// + /// Gets the type of the described by this + /// . + /// + public Type OptionType + { + get; + private set; + } + + /// + /// Gets the instance of described by this + /// . + /// + public TOption Instance + { + get; + private set; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptorBasedProvider.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptorBasedProvider.cs new file mode 100644 index 0000000000..984763521d --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptorBasedProvider.cs @@ -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 +{ + /// + /// Provides a default implementation for instantiating options from a sequence of + /// . + /// + /// The type of the option. + public abstract class OptionDescriptorBasedProvider + { + private readonly IEnumerable> _optionDescriptors; + private readonly ITypeActivator _typeActivator; + private readonly IServiceProvider _serviceProvider; + + public OptionDescriptorBasedProvider( + [NotNull] IEnumerable> optionDescriptors, + [NotNull] ITypeActivator typeActivator, + [NotNull] IServiceProvider serviceProvider) + { + _optionDescriptors = optionDescriptors; + _typeActivator = typeActivator; + _serviceProvider = serviceProvider; + } + + /// + /// Gets an activated sequence of instance. + /// + protected IReadOnlyList Options + { + get + { + var result = new List(); + foreach (var descriptor in _optionDescriptors) + { + var instance = descriptor.Instance; + if (instance == null) + { + instance = (TOption)_typeActivator.CreateInstance(_serviceProvider, + descriptor.OptionType); + } + + result.Add(instance); + } + + return result; + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OutputFormatterDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OutputFormatterDescriptor.cs new file mode 100644 index 0000000000..fff1e4bb4d --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OutputFormatterDescriptor.cs @@ -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 +{ + /// + /// Encapsulates information that describes an . + /// + public class OutputFormatterDescriptor : OptionDescriptor + { + /// + /// Creates a new instance of . + /// + /// A . + /// + /// An instance of + /// that the descriptor represents. + public OutputFormatterDescriptor([NotNull] OutputFormatter outputFormatter) + : base(outputFormatter) + { + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OutputFormatterDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OutputFormatterDescriptorExtensions.cs similarity index 86% rename from src/Microsoft.AspNet.Mvc.Core/OutputFormatterDescriptorExtensions.cs rename to src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OutputFormatterDescriptorExtensions.cs index 0781587ca6..f8d0b6edfd 100644 --- a/src/Microsoft.AspNet.Mvc.Core/OutputFormatterDescriptorExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OutputFormatterDescriptorExtensions.cs @@ -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 /// Type representing an . /// OutputFormatterDescriptor representing the added instance. public static OutputFormatterDescriptor Add([NotNull] this IList descriptors, - [NotNull] Type outputFormatterType) + [NotNull] Type outputFormatterType) { var descriptor = new OutputFormatterDescriptor(outputFormatterType); descriptors.Add(descriptor); @@ -32,8 +33,8 @@ namespace Microsoft.AspNet.Mvc /// Type representing an . /// OutputFormatterDescriptor representing the inserted instance. public static OutputFormatterDescriptor Insert([NotNull] this IList 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 /// An instance. /// OutputFormatterDescriptor representing the added instance. public static OutputFormatterDescriptor Add([NotNull] this IList descriptors, - [NotNull] OutputFormatter outputFormatter) + [NotNull] OutputFormatter outputFormatter) { var descriptor = new OutputFormatterDescriptor(outputFormatter); descriptors.Add(descriptor); @@ -66,8 +67,8 @@ namespace Microsoft.AspNet.Mvc /// An instance. /// OutputFormatterDescriptor representing the added instance. public static OutputFormatterDescriptor Insert([NotNull] this IList descriptors, - int index, - [NotNull] OutputFormatter outputFormatter) + int index, + [NotNull] OutputFormatter outputFormatter) { if (index < 0 || index > descriptors.Count) { diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValueProviderFactoryDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValueProviderFactoryDescriptor.cs new file mode 100644 index 0000000000..68a9b07430 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValueProviderFactoryDescriptor.cs @@ -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 +{ + /// + /// Encapsulates information that describes an . + /// + public class ValueProviderFactoryDescriptor : OptionDescriptor + { + /// + /// Creates a new instance of . + /// + /// The using the specified type. + /// + /// An instance of + /// that the descriptor represents. + public ValueProviderFactoryDescriptor([NotNull] IValueProviderFactory valueProviderFactory) + : base(valueProviderFactory) + { + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValueProviderFactoryDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValueProviderFactoryDescriptorExtensions.cs new file mode 100644 index 0000000000..d1731743d2 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValueProviderFactoryDescriptorExtensions.cs @@ -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 +{ + /// + /// Extension methods for adding to a descriptor collection. + /// + public static class ValueProviderFactoryDescriptorExtensions + { + /// + /// Adds a type representing a to a descriptor collection. + /// + /// A list of ValueProviderFactoryDescriptors + /// Type representing an . + /// ValueProviderFactoryDescriptor representing the added instance. + public static ValueProviderFactoryDescriptor Add( + [NotNull] this IList descriptors, + [NotNull] Type valueProviderType) + { + var descriptor = new ValueProviderFactoryDescriptor(valueProviderType); + descriptors.Add(descriptor); + return descriptor; + } + + /// + /// Inserts a type representing a to a descriptor collection. + /// + /// A list of ValueProviderFactoryDescriptors + /// Type representing an . + /// ValueProviderFactoryDescriptor representing the inserted instance. + public static ValueProviderFactoryDescriptor Insert( + [NotNull] this IList 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; + } + + /// + /// Adds an to a descriptor collection. + /// + /// A list of ValueProviderFactoryDescriptors + /// An instance. + /// ValueProviderFactoryDescriptor representing the added instance. + public static ValueProviderFactoryDescriptor Add( + [NotNull] this IList descriptors, + [NotNull] IValueProviderFactory valueProviderFactory) + { + var descriptor = new ValueProviderFactoryDescriptor(valueProviderFactory); + descriptors.Add(descriptor); + return descriptor; + } + + /// + /// Insert an to a descriptor collection. + /// + /// A list of ValueProviderFactoryDescriptors + /// An instance. + /// ValueProviderFactoryDescriptor representing the added instance. + public static ValueProviderFactoryDescriptor Insert( + [NotNull] this IList 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; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptor.cs new file mode 100644 index 0000000000..4954606eb6 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptor.cs @@ -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 +{ + /// + /// Encapsulates information that describes an . + /// + public class ViewEngineDescriptor : OptionDescriptor + { + /// + /// Creates a new instance of . + /// + /// The using the specified type. + /// + /// An instance of that the descriptor represents. + public ViewEngineDescriptor([NotNull] IViewEngine viewEngine) + : base(viewEngine) + { + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/Extensions/ViewEngineDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptorExtensions.cs similarity index 96% rename from src/Microsoft.AspNet.Mvc.Core/Extensions/ViewEngineDescriptorExtensions.cs rename to src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptorExtensions.cs index a7ccf21e3b..73e885b917 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Extensions/ViewEngineDescriptorExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptorExtensions.cs @@ -3,12 +3,13 @@ using System; using System.Collections.Generic; +using Microsoft.AspNet.Mvc.OptionDescriptors; using Microsoft.AspNet.Mvc.Rendering; namespace Microsoft.AspNet.Mvc { /// - /// Extension methods for adding model binders to a collection. + /// Extension methods for adding view engines to a descriptor collection. /// public static class ViewEngineDescriptorExtensions { diff --git a/src/Microsoft.AspNet.Mvc.Core/ParameterBinding/DefaultActionBindingContextProvider.cs b/src/Microsoft.AspNet.Mvc.Core/ParameterBinding/DefaultActionBindingContextProvider.cs index 1b903f85a3..9600fcbde3 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ParameterBinding/DefaultActionBindingContextProvider.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ParameterBinding/DefaultActionBindingContextProvider.cs @@ -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 _valueProviderFactories; + private readonly IValueProviderFactory _compositeValueProviderFactory; private readonly IInputFormatterProvider _inputFormatterProvider; private readonly IEnumerable _validatorProviders; private Tuple _bindingContext; public DefaultActionBindingContextProvider(IModelMetadataProvider modelMetadataProvider, ICompositeModelBinder compositeModelBinder, - IOptionsAccessor mvcOptionsAccessor, + ICompositeValueProviderFactory compositeValueProviderFactory, IInputFormatterProvider inputFormatterProvider, IEnumerable 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); diff --git a/src/Microsoft.AspNet.Mvc.Core/ParameterBinding/DefaultModelBindersProvider.cs b/src/Microsoft.AspNet.Mvc.Core/ParameterBinding/DefaultModelBindersProvider.cs deleted file mode 100644 index d5bb068a60..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/ParameterBinding/DefaultModelBindersProvider.cs +++ /dev/null @@ -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 -{ - /// - public class DefaultModelBindersProvider : IModelBindersProvider - { - private readonly List _descriptors; - private readonly ITypeActivator _typeActivator; - private readonly IServiceProvider _serviceProvider; - - /// - /// Initializes a new instance of the DefaultModelBindersProvider class. - /// - /// An accessor to the configured for this application. - /// An instance used to instantiate types. - /// A instance that retrieves services from the - /// service collection. - public DefaultModelBindersProvider(IOptionsAccessor options, - ITypeActivator typeActivator, - IServiceProvider serviceProvider) - { - _descriptors = options.Options.ModelBinders; - _typeActivator = typeActivator; - _serviceProvider = serviceProvider; - } - - /// - public IReadOnlyList ModelBinders - { - get - { - var binders = new List(); - foreach (var descriptor in _descriptors) - { - var binder = descriptor.ModelBinder; - if (binder == null) - { - binder = (IModelBinder)_typeActivator.CreateInstance(_serviceProvider, - descriptor.ModelBinderType); - } - - binders.Add(binder); - } - return binders; - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/DefaultViewEngineProvider.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/DefaultViewEngineProvider.cs deleted file mode 100644 index fc5802c232..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/DefaultViewEngineProvider.cs +++ /dev/null @@ -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 -{ - /// - public class DefaultViewEngineProvider : IViewEngineProvider - { - private readonly IList _descriptors; - private readonly ITypeActivator _typeActivator; - private readonly IServiceProvider _serviceProvider; - - public DefaultViewEngineProvider( - ITypeActivator typeActivator, - IServiceProvider serviceProvider, - IOptionsAccessor options) - { - _typeActivator = typeActivator; - _serviceProvider = serviceProvider; - _descriptors = options.Options.ViewEngines; - } - - /// - public IReadOnlyList ViewEngines - { - get - { - var viewEngines = new List(_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; - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/ViewEngineDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/ViewEngineDescriptor.cs deleted file mode 100644 index b7b9973ba2..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/ViewEngineDescriptor.cs +++ /dev/null @@ -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 -{ - /// - /// Encapsulates information that describes an . - /// - public class ViewEngineDescriptor - { - /// - /// Creates a new instance of . - /// - /// The - /// Creates a new instance of . - /// - /// An instance of that the descriptor represents. - public ViewEngineDescriptor([NotNull] IViewEngine viewEngine) - { - ViewEngine = viewEngine; - ViewEngineType = viewEngine.GetType(); - } - - /// - /// Gets the type of the . - /// - public Type ViewEngineType - { - get; - private set; - } - - /// - /// Gets the instance of the . - /// - public IViewEngine ViewEngine - { - get; - private set; - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/CompositeModelBinder.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/CompositeModelBinder.cs index 206e5f6ae2..e79b2e7e9b 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/CompositeModelBinder.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/CompositeModelBinder.cs @@ -16,14 +16,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding /// public class CompositeModelBinder : ICompositeModelBinder { - private readonly IModelBindersProvider _modelBindersProvider; + private readonly IModelBinderProvider _modelBindersProvider; private IReadOnlyList _binders; /// /// Initializes a new instance of the CompositeModelBinder class. /// /// Provides a collection of instances. - public CompositeModelBinder(IModelBindersProvider modelBindersProvider) + public CompositeModelBinder(IModelBinderProvider modelBindersProvider) { _modelBindersProvider = modelBindersProvider; } diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/IModelBindersProvider.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/IModelBinderProvider.cs similarity index 92% rename from src/Microsoft.AspNet.Mvc.ModelBinding/Binders/IModelBindersProvider.cs rename to src/Microsoft.AspNet.Mvc.ModelBinding/Binders/IModelBinderProvider.cs index d9d4faf172..ce8de82f0b 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/IModelBindersProvider.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/IModelBinderProvider.cs @@ -8,7 +8,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding /// /// Provides an activated collection of instances. /// - public interface IModelBindersProvider + public interface IModelBinderProvider { /// /// Gets a collection of activated ModelBinders instances. diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/ModelBinderDescriptor.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/ModelBinderDescriptor.cs deleted file mode 100644 index e6b75cab8a..0000000000 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/ModelBinderDescriptor.cs +++ /dev/null @@ -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 -{ - /// - /// Encapsulates information that describes an . - /// - public class ModelBinderDescriptor - { - /// - /// Creates a new instance of . - /// - /// A - /// Creates a new instance of . - /// - /// An instance of that the descriptor represents. - public ModelBinderDescriptor([NotNull] IModelBinder modelBinder) - { - ModelBinder = modelBinder; - ModelBinderType = modelBinder.GetType(); - } - - /// - /// Gets the type of the . - /// - public Type ModelBinderType - { - get; - private set; - } - - /// - /// Gets the instance of the . - /// - public IModelBinder ModelBinder - { - get; - private set; - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Microsoft.AspNet.Mvc.ModelBinding.kproj b/src/Microsoft.AspNet.Mvc.ModelBinding/Microsoft.AspNet.Mvc.ModelBinding.kproj index b029128842..2aecac0d86 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Microsoft.AspNet.Mvc.ModelBinding.kproj +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Microsoft.AspNet.Mvc.ModelBinding.kproj @@ -35,10 +35,8 @@ - + - - @@ -110,11 +108,14 @@ + + + diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/CompositeValueProviderFactory.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/CompositeValueProviderFactory.cs new file mode 100644 index 0000000000..8c8904c0a5 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/CompositeValueProviderFactory.cs @@ -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 +{ + /// + public class CompositeValueProviderFactory : ICompositeValueProviderFactory + { + private readonly IReadOnlyList _valueProviderFactories; + + public CompositeValueProviderFactory(IValueProviderFactoryProvider valueProviderFactoryProvider) + { + _valueProviderFactories = valueProviderFactoryProvider.ValueProviderFactories; + } + + /// + public IValueProvider GetValueProvider([NotNull] ValueProviderFactoryContext context) + { + var valueProviders = _valueProviderFactories.Select(factory => factory.GetValueProvider(context)) + .Where(vp => vp != null); + + return new CompositeValueProvider(valueProviders); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/ICompositeValueProviderFactory.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/ICompositeValueProviderFactory.cs new file mode 100644 index 0000000000..f1fcb49b56 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/ICompositeValueProviderFactory.cs @@ -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 +{ + /// + /// Represents an aggregate of . + /// + public interface ICompositeValueProviderFactory : IValueProviderFactory + { + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/IValueProviderFactoryProvider.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/IValueProviderFactoryProvider.cs new file mode 100644 index 0000000000..e8ff72925c --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/IValueProviderFactoryProvider.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace Microsoft.AspNet.Mvc.ModelBinding +{ + /// + /// Provides an activated collection of instances. + /// + public interface IValueProviderFactoryProvider + { + /// + /// Gets a collection of activated IValueProviderFactory instances. + /// + IReadOnlyList ValueProviderFactories { get; } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs b/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs index 014d3bc92b..cded969302 100644 --- a/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs +++ b/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs @@ -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)); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc/MvcServices.cs b/src/Microsoft.AspNet.Mvc/MvcServices.cs index 40088f4cf6..724ea7050d 100644 --- a/src/Microsoft.AspNet.Mvc/MvcServices.cs +++ b/src/Microsoft.AspNet.Mvc/MvcServices.cs @@ -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(); yield return describe.Transient(); - yield return describe.Transient(); - yield return describe.Transient(); + yield return describe.Transient(); + yield return describe.Scoped(); + yield return describe.Transient(); + yield return describe.Scoped(); yield return describe.Transient, DefaultFilterProvider>(); diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Microsoft.AspNet.Mvc.Core.Test.kproj b/test/Microsoft.AspNet.Mvc.Core.Test/Microsoft.AspNet.Mvc.Core.Test.kproj index 169e49f440..8e1dd889bb 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Microsoft.AspNet.Mvc.Core.Test.kproj +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Microsoft.AspNet.Mvc.Core.Test.kproj @@ -30,11 +30,19 @@ + + + + + + + + + + + - - - @@ -61,7 +69,6 @@ - @@ -77,8 +84,6 @@ - - @@ -91,4 +96,4 @@ - + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ParameterBinding/DefaultModelBindersProviderTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultModelBindersProviderTest.cs similarity index 50% rename from test/Microsoft.AspNet.Mvc.Core.Test/ParameterBinding/DefaultModelBindersProviderTest.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultModelBindersProviderTest.cs index e514c8e9b9..2aa8d38e5e 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ParameterBinding/DefaultModelBindersProviderTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultModelBindersProviderTest.cs @@ -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(); 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>(); optionsAccessor.SetupGet(o => o.Options) .Returns(options); - var activator = new Mock(); - var serviceProvider = Mock.Of(); - activator.Setup(a => a.CreateInstance(serviceProvider, typeof(GenericModelBinder))) - .Returns(new GenericModelBinder(serviceProvider, activator.Object)) - .Verifiable(); + var activator = new TypeActivator(); + var serviceProvider = new Mock(); + 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(binders[1]); + var testModelBinder = Assert.IsType(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 BindModelAsync(ModelBindingContext bindingContext) + { + throw new NotImplementedException(); + } + } + + public interface ITestService + { } } } -#endif \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValueProviderFactoryProviderTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValueProviderFactoryProviderTest.cs new file mode 100644 index 0000000000..82d0913c4a --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValueProviderFactoryProviderTest.cs @@ -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(); + var valueProviderFactory = Mock.Of(); + var type = typeof(TestValueProviderFactory); + var typeActivator = new TypeActivator(); + var serviceProvider = new Mock(); + 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>(); + 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(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 + { + } + } +} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/DefaultViewEngineProviderTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultViewEngineProviderTest.cs similarity index 91% rename from test/Microsoft.AspNet.Mvc.Core.Test/Rendering/DefaultViewEngineProviderTest.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultViewEngineProviderTest.cs index 0c53deb6ec..88356fe7d9 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/DefaultViewEngineProviderTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultViewEngineProviderTest.cs @@ -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>(); 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 \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/ModelBinderDescriptorExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorExtensionsTest.cs similarity index 85% rename from test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/ModelBinderDescriptorExtensionsTest.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorExtensionsTest.cs index 6b59a94831..930ce2a981 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/ModelBinderDescriptorExtensionsTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorExtensionsTest.cs @@ -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 \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorTest.cs new file mode 100644 index 0000000000..8d1dc7c4dd --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorTest.cs @@ -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 BindModelAsync(ModelBindingContext bindingContext) + { + throw new NotImplementedException(); + } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/OutputFormatterDescriptorExtensionTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorExtensionTest.cs similarity index 86% rename from test/Microsoft.AspNet.Mvc.Core.Test/Formatters/OutputFormatterDescriptorExtensionTest.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorExtensionTest.cs index 70987c82d7..a157638efe 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/OutputFormatterDescriptorExtensionTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorExtensionTest.cs @@ -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("index", + Assert.Throws("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 \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/OutputFormatterDescriptorTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorTest.cs similarity index 84% rename from test/Microsoft.AspNet.Mvc.Core.Test/Formatters/OutputFormatterDescriptorTest.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorTest.cs index c2bfc9bf22..3bf86d7a81 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Formatters/OutputFormatterDescriptorTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorTest.cs @@ -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); } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValueProviderFactoryDescriptorExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValueProviderFactoryDescriptorExtensionsTest.cs new file mode 100644 index 0000000000..c555af233f --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValueProviderFactoryDescriptorExtensionsTest.cs @@ -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 + { + new ValueProviderFactoryDescriptor(Mock.Of()), + new ValueProviderFactoryDescriptor(Mock.Of()) + }; + + // Act & Assert + Assert.Throws("index", + () => collection.Insert(index, typeof(IValueProviderFactory))); + } + + [Theory] + [InlineData(-2)] + [InlineData(3)] + public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index) + { + // Arrange + var collection = new List + { + new ValueProviderFactoryDescriptor(Mock.Of()), + new ValueProviderFactoryDescriptor(Mock.Of()) + }; + var valueProviderFactory = Mock.Of(); + + // Act & Assert + Assert.Throws("index", () => collection.Insert(index, valueProviderFactory)); + } + + [InlineData] + public void ValueProviderFactoryDescriptors_AddsTypesAndInstances() + { + // Arrange + var valueProviderFactory = Mock.Of(); + var type = typeof(TestValueProviderFactory); + var collection = new List(); + + // Act + collection.Add(valueProviderFactory); + collection.Insert(0, type); + + // Assert + Assert.Equal(2, collection.Count); + Assert.IsType(collection[0].Instance); + Assert.Same(valueProviderFactory, collection[0].Instance); + } + + private class TestValueProviderFactory : IValueProviderFactory + { + public IValueProvider GetValueProvider(ValueProviderFactoryContext context) + { + throw new NotImplementedException(); + } + } + } +} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValueProviderFactoryDescriptorTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValueProviderFactoryDescriptorTest.cs new file mode 100644 index 0000000000..4c86802a0f --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValueProviderFactoryDescriptorTest.cs @@ -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(); + } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/ViewEngineDescriptorTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDescriptorTest.cs similarity index 84% rename from test/Microsoft.AspNet.Mvc.Core.Test/Rendering/ViewEngineDescriptorTest.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDescriptorTest.cs index 5af7f9afb2..1582a7e43d 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/ViewEngineDescriptorTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDescriptorTest.cs @@ -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 diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Extensions/ViewEngineDscriptorExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDscriptorExtensionsTest.cs similarity index 90% rename from test/Microsoft.AspNet.Mvc.Core.Test/Extensions/ViewEngineDscriptorExtensionsTest.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDscriptorExtensionsTest.cs index 79ce0f366b..72c1c213d3 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Extensions/ViewEngineDscriptorExtensionsTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDscriptorExtensionsTest.cs @@ -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(collection[0].ViewEngine); - Assert.Same(viewEngine, collection[0].ViewEngine); + Assert.IsType(collection[0].Instance); + Assert.Same(viewEngine, collection[0].Instance); } private class TestViewEngine : IViewEngine @@ -76,4 +77,3 @@ namespace Microsoft.AspNet.Mvc.Rendering } } } -#endif \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/CompositeModelBinderTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/CompositeModelBinderTest.cs index 3afa72fdae..bd8a8184f4 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/CompositeModelBinderTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/CompositeModelBinderTest.cs @@ -299,7 +299,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test new TypeConverterModelBinder(), new MutableObjectModelBinder() }; - var binderProviders = new Mock(); + var binderProviders = new Mock(); 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(); + var binderProvider = new Mock(); binderProvider.SetupGet(p => p.ModelBinders) .Returns(new[] { mockIntBinder }); diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/KeyValuePairModelBinderTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/KeyValuePairModelBinderTest.cs index c3f8f9087b..ec1f9386a3 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/KeyValuePairModelBinderTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/KeyValuePairModelBinderTest.cs @@ -50,7 +50,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test public async Task BindModel_SubBindingSucceeds() { // Arrange - var binderProviders = new Mock(); + var binderProviders = new Mock(); binderProviders.SetupGet(b => b.ModelBinders) .Returns(new[] { CreateStringBinder(), CreateIntBinder() }); var innerBinder = new CompositeModelBinder(binderProviders.Object); diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/ModelBinderDescriptorTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/ModelBinderDescriptorTest.cs deleted file mode 100644 index e3e5f3ce25..0000000000 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/ModelBinderDescriptorTest.cs +++ /dev/null @@ -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); - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Microsoft.AspNet.Mvc.ModelBinding.Test.kproj b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Microsoft.AspNet.Mvc.ModelBinding.Test.kproj index 59df67f936..2d634be717 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Microsoft.AspNet.Mvc.ModelBinding.Test.kproj +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Microsoft.AspNet.Mvc.ModelBinding.Test.kproj @@ -29,8 +29,6 @@ - - diff --git a/test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs b/test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs index 9942e9670f..86f5a04781 100644 --- a/test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs +++ b/test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs @@ -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(valueProviders[0]); - Assert.IsType(valueProviders[1]); - Assert.IsType(valueProviders[2]); + Assert.IsType(valueProviders[0].Instance); + Assert.IsType(valueProviders[1].Instance); + Assert.IsType(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(mvcOptions.OutputFormatters[0].OutputFormatter); + Assert.IsType(mvcOptions.OutputFormatters[0].Instance); } } } \ No newline at end of file