From 1b8fa1c8ec70f4dcc6492a91e0ee312a57ddf896 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 2 Apr 2015 17:04:56 -0700 Subject: [PATCH] * Simplify MvcOptions * Remove facades for accessing Options and pass options to the invoker Fixes #2266 Fixes #2269 --- .../ActionResults/JsonResult.cs | 4 +- .../ActionResults/ObjectResult.cs | 10 +- .../ControllerActionInvoker.cs | 16 +- .../ControllerActionInvokerProvider.cs | 30 +- .../DefaultControllerActionArgumentBinder.cs | 6 +- ...DefaultValidationExcludeFiltersProvider.cs | 37 --- .../DefaultApiDescriptionProvider.cs | 14 +- .../ExcludeTypeValidationFilterExtensions.cs | 43 +++ .../FilterActionInvoker.cs | 37 ++- .../DefaultInputFormattersProvider.cs | 38 --- .../DefaultOutputFormattersProvider.cs | 39 --- .../HttpNoContentOutputFormatter.cs | 2 - .../HttpNotAcceptableOutputFormatter.cs | 2 - .../Formatters/IInputFormattersProvider.cs | 18 -- .../Formatters/IJsonOutputFormatter.cs | 2 +- .../Formatters/IOutputFormattersProvider.cs | 18 -- .../ModelBinders/BodyModelBinder.cs | 32 +-- src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs | 52 ++-- .../DefaultModelBindersProvider.cs | 39 --- .../DefaultModelValidatorProviderProvider.cs | 37 --- .../DefaultValueProviderFactoryProvider.cs | 40 --- .../DefaultViewEngineProvider.cs | 39 --- .../ExcludeValidationDescriptor.cs | 36 --- .../OptionDescriptors/IOptionDescriptor.cs | 26 -- .../InputFormatterDescriptor.cs | 34 --- .../InputFormatterDescriptorExtensions.cs | 84 ------ .../ModelBinderDescriptor.cs | 33 --- .../ModelBinderDescriptorExtensions.cs | 103 ------- .../ModelValidatorProviderDescriptor.cs | 33 --- ...elValidatorProviderDescriptorExtensions.cs | 95 ------- .../OptionDescriptors/OptionDescriptor.cs | 62 ----- .../OptionDescriptorBasedProvider.cs | 63 ----- .../OptionDescriptorExtensions.cs | 75 ----- .../OutputFormatterDescriptor.cs | 34 --- .../OutputFormatterDescriptorExtensions.cs | 102 ------- .../ValidationExcludeFiltersExtensions.cs | 59 ---- .../ValueProviderFactoryDescriptor.cs | 34 --- ...alueProviderFactoryDescriptorExtensions.cs | 107 ------- .../ViewEngineDescriptorExtensions.cs | 85 ------ .../ViewEngine/CompositeViewEngine.cs | 39 ++- .../ViewEngine/IViewEngineProvider.cs | 18 -- .../ViewEngineDescriptor.cs | 26 +- .../ViewEngineDescriptorExtensions.cs | 140 ++++++++++ .../BinderMetadata/ModelBinderAttribute.cs | 8 +- .../Binders/BinderTypeBasedModelBinder.cs | 20 +- .../Binders/IModelBinderProvider.cs | 18 -- .../Binders}/ServicesModelBinder.cs | 0 .../Properties/Resources.Designer.cs | 12 +- .../Resources.resx | 4 +- .../Validation/DefaultObjectValidator.cs | 34 +-- .../DefaultTypeBasedExcludeFilter.cs | 3 +- .../DefaultTypeNameBasedExcludeFilter.cs | 3 +- .../IModelValidatorProviderProvider.cs | 18 -- .../IValidationExcludeFiltersProvider.cs | 18 -- .../CompositeValueProviderFactory.cs | 29 -- .../IValueProviderFactoryProvider.cs | 18 -- .../DefaultViewLocationExpanderProvider.cs | 38 --- .../IViewLocationExpanderProvider.cs | 18 -- .../ViewLocationExpanderDescriptor.cs | 35 --- ...iewLocationExpanderDescriptorExtensions.cs | 94 ------- .../Properties/Resources.Designer.cs | 8 +- .../RazorViewEngine.cs | 9 +- .../RazorViewEngineOptions.cs | 10 +- src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs | 2 +- src/Microsoft.AspNet.Mvc/MvcServices.cs | 21 +- .../CreatedAtActionResultTests.cs | 25 +- .../CreatedAtRouteResultTests.cs | 25 +- .../ActionResults/CreatedResultTests.cs | 27 +- .../HttpNotFoundObjectResultTest.cs | 36 +-- .../ActionResults/ObjectResultTests.cs | 38 +-- .../BodyModelBinderTests.cs | 33 +-- .../ControllerActionInvokerTest.cs | 65 ++--- .../ControllerTests.cs | 2 +- .../DefaultControllerFactoryTest.cs | 2 +- .../DefaultApiDescriptionProviderTest.cs | 25 +- ...ludeTypeValidationFilterExtensionsTests.cs | 41 +++ .../JsonResultTest.cs | 25 +- .../MockModelBinderProvider.cs | 14 - .../MockModelValidatorProviderProvider.cs | 17 -- .../MockValueProviderFactoryProvider.cs | 17 -- .../DefaultModelBindersProviderTest.cs | 66 ----- ...ltValidationExcludeFiltersProviderTests.cs | 152 ---------- .../DefaultValidationProviderProviderTest.cs | 65 ----- ...DefaultValueProviderFactoryProviderTest.cs | 64 ----- .../DefaultViewEngineProviderTest.cs | 67 ----- .../ExcludeValidationDescriptorTests.cs | 62 ----- .../InputFormatterDescriptorExtensionTest.cs | 72 ----- .../InputFormatterDescriptorTest.cs | 68 ----- .../ModelBinderDescriptorExtensionsTest.cs | 90 ------ .../ModelBinderDescriptorTest.cs | 62 ----- .../ModelValidatorProviderDescriptor.cs | 63 ----- ...lidatorProviderDescriptorExtensionsTest.cs | 74 ----- .../OptionDescriptorExtensionsTest.cs | 262 +----------------- .../OutputFormatterDescriptorExtensionTest.cs | 91 ------ .../OutputFormatterDescriptorTest.cs | 70 ----- ...ValidationExcludeFiltersExtensionsTests.cs | 33 --- ...ProviderFactoryDescriptorExtensionsTest.cs | 93 ------- .../ValueProviderFactoryDescriptorTest.cs | 62 ----- .../ViewEngineDescriptorExtensionsTest.cs | 137 ++++++++- .../ViewEngineDescriptorTest.cs | 8 +- .../ModelBindingHelperTest.cs | 18 +- .../ViewEngine/CompositeViewEngineTest.cs | 107 ++++--- .../ModelBindingModelBinderAttributeTest.cs | 18 +- ...nderTypeBasedModelBinderModelBinderTest.cs | 53 +--- .../Metadata/ModelBinderAttributeTest.cs | 5 +- .../Validation/DefaultObjectValidatorTests.cs | 47 ++-- ...DefaultViewLocationExpanderProviderTest.cs | 71 ----- ...ocationExpanderDescriptorExtensionsTest.cs | 80 ------ .../ViewLocationExpanderDescriptorTest.cs | 68 ----- .../RazorViewEngineTest.cs | 37 ++- .../MvcOptionsSetupTest.cs | 84 +++--- .../BadRequestErrorMessageResultTest.cs | 18 +- .../ActionResults/ExceptionResultTest.cs | 19 +- .../InvalidModelStateResultTest.cs | 18 +- .../NegotiatedContentResultTest.cs | 18 +- .../OkNegotiatedContentResultTest.cs | 19 +- .../FallbackOnTypeBasedMatchController.cs | 11 +- .../ModelBinderAttribute_ProductController.cs | 28 -- test/WebSites/ModelBindingWebSite/Startup.cs | 2 +- 119 files changed, 872 insertions(+), 4165 deletions(-) delete mode 100644 src/Microsoft.AspNet.Mvc.Core/DefaultValidationExcludeFiltersProvider.cs create mode 100644 src/Microsoft.AspNet.Mvc.Core/ExcludeTypeValidationFilterExtensions.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultInputFormattersProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultOutputFormattersProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/Formatters/IInputFormattersProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/Formatters/IOutputFormattersProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultModelBindersProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultModelValidatorProviderProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultValueProviderFactoryProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultViewEngineProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ExcludeValidationDescriptor.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/IOptionDescriptor.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/InputFormatterDescriptor.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/InputFormatterDescriptorExtensions.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelBinderDescriptor.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelBinderDescriptorExtensions.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelValidatorProviderDescriptor.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelValidatorProviderDescriptorExtensions.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptor.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptorBasedProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptorExtensions.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OutputFormatterDescriptor.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OutputFormatterDescriptorExtensions.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValidationExcludeFiltersExtensions.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValueProviderFactoryDescriptor.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValueProviderFactoryDescriptorExtensions.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptorExtensions.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Core/Rendering/ViewEngine/IViewEngineProvider.cs rename src/Microsoft.AspNet.Mvc.Core/{OptionDescriptors => }/ViewEngineDescriptor.cs (54%) create mode 100644 src/Microsoft.AspNet.Mvc.Core/ViewEngineDescriptorExtensions.cs delete mode 100644 src/Microsoft.AspNet.Mvc.ModelBinding/Binders/IModelBinderProvider.cs rename src/{Microsoft.AspNet.Mvc.Core/ModelBinders => Microsoft.AspNet.Mvc.ModelBinding/Binders}/ServicesModelBinder.cs (100%) rename src/{Microsoft.AspNet.Mvc.Core/Formatters => Microsoft.AspNet.Mvc.ModelBinding/Validation}/DefaultTypeBasedExcludeFilter.cs (93%) rename src/{Microsoft.AspNet.Mvc.Core/Formatters => Microsoft.AspNet.Mvc.ModelBinding/Validation}/DefaultTypeNameBasedExcludeFilter.cs (95%) delete mode 100644 src/Microsoft.AspNet.Mvc.ModelBinding/Validation/IModelValidatorProviderProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.ModelBinding/Validation/IValidationExcludeFiltersProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/CompositeValueProviderFactory.cs delete mode 100644 src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/IValueProviderFactoryProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/DefaultViewLocationExpanderProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/IViewLocationExpanderProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/ViewLocationExpanderDescriptor.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/ViewLocationExpanderDescriptorExtensions.cs rename src/Microsoft.AspNet.Mvc.Razor/{OptionDescriptors => }/RazorViewEngineOptions.cs (73%) create mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/ExcludeTypeValidationFilterExtensionsTests.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/MockModelBinderProvider.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/MockModelValidatorProviderProvider.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/MockValueProviderFactoryProvider.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultModelBindersProviderTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValidationExcludeFiltersProviderTests.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValidationProviderProviderTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValueProviderFactoryProviderTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultViewEngineProviderTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ExcludeValidationDescriptorTests.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/InputFormatterDescriptorExtensionTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/InputFormatterDescriptorTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorExtensionsTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelValidatorProviderDescriptor.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelValidatorProviderDescriptorExtensionsTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorExtensionTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValidationExcludeFiltersExtensionsTests.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValueProviderFactoryDescriptorExtensionsTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValueProviderFactoryDescriptorTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Razor.Test/OptionDescriptors/DefaultViewLocationExpanderProviderTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Razor.Test/OptionDescriptors/ViewLocationExpanderDescriptorExtensionsTest.cs delete mode 100644 test/Microsoft.AspNet.Mvc.Razor.Test/OptionDescriptors/ViewLocationExpanderDescriptorTest.cs diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs index bf7afa4b91..f3ccae757c 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.Internal; +using Microsoft.Framework.OptionsModel; using Microsoft.Net.Http.Headers; namespace Microsoft.AspNet.Mvc @@ -115,7 +116,8 @@ private IOutputFormatter SelectFormatter(ObjectResult objectResult, OutputFormat .ActionContext .HttpContext .RequestServices - .GetRequiredService() + .GetRequiredService>() + .Options .OutputFormatters .OfType() .ToArray(); diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ObjectResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ObjectResult.cs index fabce23584..135f94161d 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ObjectResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ObjectResult.cs @@ -273,10 +273,12 @@ private IEnumerable GetDefaultFormatters(ActionContext context IEnumerable formatters = null; if (Formatters == null || Formatters.Count == 0) { - formatters = context.HttpContext - .RequestServices - .GetRequiredService() - .OutputFormatters; + formatters = context + .HttpContext + .RequestServices + .GetRequiredService>() + .Options + .OutputFormatters; } else { diff --git a/src/Microsoft.AspNet.Mvc.Core/ControllerActionInvoker.cs b/src/Microsoft.AspNet.Mvc.Core/ControllerActionInvoker.cs index 391b8ff3c0..efa26b64b4 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ControllerActionInvoker.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ControllerActionInvoker.cs @@ -23,20 +23,20 @@ public ControllerActionInvoker( [NotNull] IReadOnlyList filterProviders, [NotNull] IControllerFactory controllerFactory, [NotNull] ControllerActionDescriptor descriptor, - [NotNull] IInputFormattersProvider inputFormatterProvider, + [NotNull] IReadOnlyList inputFormatters, [NotNull] IControllerActionArgumentBinder controllerActionArgumentBinder, - [NotNull] IModelBinderProvider modelBinderProvider, - [NotNull] IModelValidatorProviderProvider modelValidatorProviderProvider, - [NotNull] IValueProviderFactoryProvider valueProviderFactoryProvider, + [NotNull] IReadOnlyList modelBinders, + [NotNull] IReadOnlyList modelValidatorProviders, + [NotNull] IReadOnlyList valueProviderFactories, [NotNull] IScopedInstance actionBindingContextAccessor, [NotNull] ITempDataDictionary tempData) : base( actionContext, filterProviders, - inputFormatterProvider, - modelBinderProvider, - modelValidatorProviderProvider, - valueProviderFactoryProvider, + inputFormatters, + modelBinders, + modelValidatorProviders, + valueProviderFactories, actionBindingContextAccessor) { _descriptor = descriptor; diff --git a/src/Microsoft.AspNet.Mvc.Core/ControllerActionInvokerProvider.cs b/src/Microsoft.AspNet.Mvc.Core/ControllerActionInvokerProvider.cs index 2e2497808d..d99a8881ff 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ControllerActionInvokerProvider.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ControllerActionInvokerProvider.cs @@ -6,6 +6,7 @@ using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding.Validation; using Microsoft.Framework.Internal; +using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Mvc.Core { @@ -14,31 +15,28 @@ public class ControllerActionInvokerProvider : IActionInvokerProvider private readonly IControllerActionArgumentBinder _argumentBinder; private readonly IControllerFactory _controllerFactory; private readonly IFilterProvider[] _filterProviders; - private readonly IInputFormattersProvider _inputFormattersProvider; - private readonly IModelBinderProvider _modelBinderProvider; - private readonly IModelValidatorProviderProvider _modelValidationProviderProvider; - private readonly IValueProviderFactoryProvider _valueProviderFactoryProvider; + private readonly IReadOnlyList _inputFormatters; + private readonly IReadOnlyList _modelBinders; + private readonly IReadOnlyList _modelValidatorProviders; + private readonly IReadOnlyList _valueProviderFactories; private readonly IScopedInstance _actionBindingContextAccessor; private readonly ITempDataDictionary _tempData; public ControllerActionInvokerProvider( IControllerFactory controllerFactory, - IInputFormattersProvider inputFormattersProvider, IEnumerable filterProviders, IControllerActionArgumentBinder argumentBinder, - IModelBinderProvider modelBinderProvider, - IModelValidatorProviderProvider modelValidationProviderProvider, - IValueProviderFactoryProvider valueProviderFactoryProvider, + IOptions optionsAccessor, IScopedInstance actionBindingContextAccessor, ITempDataDictionary tempData) { _controllerFactory = controllerFactory; - _inputFormattersProvider = inputFormattersProvider; _filterProviders = filterProviders.OrderBy(item => item.Order).ToArray(); _argumentBinder = argumentBinder; - _modelBinderProvider = modelBinderProvider; - _modelValidationProviderProvider = modelValidationProviderProvider; - _valueProviderFactoryProvider = valueProviderFactoryProvider; + _inputFormatters = optionsAccessor.Options.InputFormatters.ToArray(); + _modelBinders = optionsAccessor.Options.ModelBinders.ToArray(); + _modelValidatorProviders = optionsAccessor.Options.ModelValidatorProviders.ToArray(); + _valueProviderFactories = optionsAccessor.Options.ValueProviderFactories.ToArray(); _actionBindingContextAccessor = actionBindingContextAccessor; _tempData = tempData; } @@ -60,11 +58,11 @@ public void OnProvidersExecuting([NotNull] ActionInvokerProviderContext context) _filterProviders, _controllerFactory, actionDescriptor, - _inputFormattersProvider, + _inputFormatters, _argumentBinder, - _modelBinderProvider, - _modelValidationProviderProvider, - _valueProviderFactoryProvider, + _modelBinders, + _modelValidatorProviders, + _valueProviderFactories, _actionBindingContextAccessor, _tempData); } diff --git a/src/Microsoft.AspNet.Mvc.Core/DefaultControllerActionArgumentBinder.cs b/src/Microsoft.AspNet.Mvc.Core/DefaultControllerActionArgumentBinder.cs index fe58ab5523..93b3b51743 100644 --- a/src/Microsoft.AspNet.Mvc.Core/DefaultControllerActionArgumentBinder.cs +++ b/src/Microsoft.AspNet.Mvc.Core/DefaultControllerActionArgumentBinder.cs @@ -4,11 +4,9 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNet.Mvc.Core; using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Mvc.ModelBinding.Metadata; using Microsoft.AspNet.Mvc.ModelBinding.Validation; using Microsoft.Framework.Internal; using Microsoft.Framework.OptionsModel; @@ -37,7 +35,7 @@ public DefaultControllerActionArgumentBinder( public async Task> BindActionArgumentsAsync( ActionContext actionContext, - ActionBindingContext actionBindingContext, + ActionBindingContext actionBindingContext, object controller) { var actionDescriptor = actionContext.ActionDescriptor as ControllerActionDescriptor; @@ -66,7 +64,7 @@ await PopulateArgumentsAsync( actionArguments, actionDescriptor.Parameters); return actionArguments; - } + } private void ActivateProperties(object controller, Type containerType, Dictionary properties) { diff --git a/src/Microsoft.AspNet.Mvc.Core/DefaultValidationExcludeFiltersProvider.cs b/src/Microsoft.AspNet.Mvc.Core/DefaultValidationExcludeFiltersProvider.cs deleted file mode 100644 index 4d8a6816e3..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/DefaultValidationExcludeFiltersProvider.cs +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; -using Microsoft.Framework.OptionsModel; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - public class DefaultValidationExcludeFiltersProvider - : OptionDescriptorBasedProvider, IValidationExcludeFiltersProvider - { - /// - /// Initializes a new instance of the class. - /// - /// An accessor to the configured for this application. - /// The cache. - /// The . - public DefaultValidationExcludeFiltersProvider(IOptions optionsAccessor, - ITypeActivatorCache typeActivatorCache, - IServiceProvider serviceProvider) - : base(optionsAccessor.Options.ValidationExcludeFilters, typeActivatorCache, serviceProvider) - { - } - - /// - public IReadOnlyList ExcludeFilters - { - get - { - return Options; - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/Description/DefaultApiDescriptionProvider.cs b/src/Microsoft.AspNet.Mvc.Core/Description/DefaultApiDescriptionProvider.cs index 31139637fa..852c2b87f8 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Description/DefaultApiDescriptionProvider.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Description/DefaultApiDescriptionProvider.cs @@ -11,6 +11,7 @@ using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing.Template; using Microsoft.Framework.Internal; +using Microsoft.Framework.OptionsModel; using Microsoft.Net.Http.Headers; namespace Microsoft.AspNet.Mvc.Description @@ -21,21 +22,23 @@ namespace Microsoft.AspNet.Mvc.Description /// public class DefaultApiDescriptionProvider : IApiDescriptionProvider { - private readonly IOutputFormattersProvider _formattersProvider; + private readonly IList _outputFormatters; private readonly IModelMetadataProvider _modelMetadataProvider; private readonly IInlineConstraintResolver _constraintResolver; /// /// Creates a new instance of . /// - /// The . + /// The accessor for . + /// The used for resolving inline + /// constraints. /// The . public DefaultApiDescriptionProvider( - IOutputFormattersProvider formattersProvider, + IOptions optionsAccessor, IInlineConstraintResolver constraintResolver, IModelMetadataProvider modelMetadataProvider) { - _formattersProvider = formattersProvider; + _outputFormatters = optionsAccessor.Options.OutputFormatters; _constraintResolver = constraintResolver; _modelMetadataProvider = modelMetadataProvider; } @@ -316,10 +319,9 @@ private IReadOnlyList GetResponseFormats( contentTypes.Add(null); } - var formatters = _formattersProvider.OutputFormatters; foreach (var contentType in contentTypes) { - foreach (var formatter in formatters) + foreach (var formatter in _outputFormatters) { var responseFormatMetadataProvider = formatter as IApiResponseFormatMetadataProvider; if (responseFormatMetadataProvider != null) diff --git a/src/Microsoft.AspNet.Mvc.Core/ExcludeTypeValidationFilterExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/ExcludeTypeValidationFilterExtensions.cs new file mode 100644 index 0000000000..f80fc6da4a --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/ExcludeTypeValidationFilterExtensions.cs @@ -0,0 +1,43 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using Microsoft.AspNet.Mvc.ModelBinding.Validation; + +namespace Microsoft.AspNet.Mvc +{ + /// + /// Extensions for . + /// + public static class ExcludeTypeValidationFilterExtensions + { + /// + /// Adds a descriptor to the specified that excludes the properties of + /// the specified and its derived types from validaton. + /// + /// A list of which are used to + /// get a collection of exclude filters to be applied for filtering model properties during validation. + /// + /// which should be excluded from validation. + public static void Add(this IList excludeTypeValidationFilters, Type type) + { + var typeBasedExcludeFilter = new DefaultTypeBasedExcludeFilter(type); + excludeTypeValidationFilters.Add(typeBasedExcludeFilter); + } + + /// + /// Adds a descriptor to the specified that excludes the properties of + /// the type specified and its derived types from validaton. + /// + /// A list of which are used to + /// get a collection of exclude filters to be applied for filtering model properties during validation. + /// + /// Full name of the type which should be excluded from validation. + public static void Add(this IList excludeTypeValidationFilters, string typeFullName) + { + var filter = new DefaultTypeNameBasedExcludeFilter(typeFullName); + excludeTypeValidationFilters.Add(filter); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/FilterActionInvoker.cs b/src/Microsoft.AspNet.Mvc.Core/FilterActionInvoker.cs index de38bde056..1f01eb1d14 100644 --- a/src/Microsoft.AspNet.Mvc.Core/FilterActionInvoker.cs +++ b/src/Microsoft.AspNet.Mvc.Core/FilterActionInvoker.cs @@ -16,10 +16,11 @@ namespace Microsoft.AspNet.Mvc.Core public abstract class FilterActionInvoker : IActionInvoker { private readonly IReadOnlyList _filterProviders; - private readonly IInputFormattersProvider _inputFormatterProvider; - private readonly IModelBinderProvider _modelBinderProvider; - private readonly IModelValidatorProviderProvider _modelValidatorProviderProvider; - private readonly IValueProviderFactoryProvider _valueProviderFactoryProvider; + private readonly IReadOnlyList _inputFormatters; + private readonly IReadOnlyList _modelBinders; + private readonly IReadOnlyList _modelValidatorProviders; + private readonly IReadOnlyList _valueProviderFactories; + private readonly IScopedInstance _actionBindingContextAccessor; private IFilter[] _filters; @@ -41,19 +42,19 @@ public abstract class FilterActionInvoker : IActionInvoker public FilterActionInvoker( [NotNull] ActionContext actionContext, [NotNull] IReadOnlyList filterProviders, - [NotNull] IInputFormattersProvider inputFormatterProvider, - [NotNull] IModelBinderProvider modelBinderProvider, - [NotNull] IModelValidatorProviderProvider modelValidatorProviderProvider, - [NotNull] IValueProviderFactoryProvider valueProviderFactoryProvider, + [NotNull] IReadOnlyList inputFormatters, + [NotNull] IReadOnlyList modelBinders, + [NotNull] IReadOnlyList modelValidatorProviders, + [NotNull] IReadOnlyList valueProviderFactories, [NotNull] IScopedInstance actionBindingContextAccessor) { ActionContext = actionContext; _filterProviders = filterProviders; - _inputFormatterProvider = inputFormatterProvider; - _modelBinderProvider = modelBinderProvider; - _modelValidatorProviderProvider = modelValidatorProviderProvider; - _valueProviderFactoryProvider = valueProviderFactoryProvider; + _inputFormatters = inputFormatters; + _modelBinders = modelBinders; + _modelValidatorProviders = modelValidatorProviders; + _valueProviderFactories = valueProviderFactories; _actionBindingContextAccessor = actionBindingContextAccessor; } @@ -206,14 +207,10 @@ private async Task InvokeAllResourceFiltersAsync() var context = new ResourceExecutingContext(ActionContext, _filters); - context.InputFormatters = new List(_inputFormatterProvider.InputFormatters); - context.ModelBinders = new List(_modelBinderProvider.ModelBinders); - - context.ValidatorProviders = new List( - _modelValidatorProviderProvider.ModelValidatorProviders); - - context.ValueProviderFactories = new List( - _valueProviderFactoryProvider.ValueProviderFactories); + context.InputFormatters = new List(_inputFormatters); + context.ModelBinders = new List(_modelBinders); + context.ValidatorProviders = new List(_modelValidatorProviders); + context.ValueProviderFactories = new List(_valueProviderFactories); _resourceExecutingContext = context; await InvokeResourceFilterAsync(); diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultInputFormattersProvider.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultInputFormattersProvider.cs deleted file mode 100644 index 6b5e9b4d31..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultInputFormattersProvider.cs +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.Framework.OptionsModel; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - public class DefaultInputFormattersProvider - : OptionDescriptorBasedProvider, IInputFormattersProvider - { - /// - /// Initializes a new instance of the DefaultInputFormattersProvider class. - /// - /// An accessor to the configured for this application. - /// As instance that creates an instance - /// of type . - /// A instance that retrieves services from the - /// service collection. - public DefaultInputFormattersProvider(IOptions optionsAccessor, - ITypeActivatorCache typeActivatorCache, - IServiceProvider serviceProvider) - : base(optionsAccessor.Options.InputFormatters, typeActivatorCache, serviceProvider) - { - } - - /// - public IReadOnlyList InputFormatters - { - get - { - return Options; - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultOutputFormattersProvider.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultOutputFormattersProvider.cs deleted file mode 100644 index 39df20771a..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultOutputFormattersProvider.cs +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.Framework.OptionsModel; - -namespace Microsoft.AspNet.Mvc -{ - /// - public class DefaultOutputFormattersProvider - : OptionDescriptorBasedProvider, IOutputFormattersProvider - { - /// - /// Initializes a new instance of the DefaultOutputFormattersProvider class. - /// - /// An accessor to the configured for this application. - /// As instance that creates an instance - /// of type . - /// A instance that retrieves services from the - /// service collection. - public DefaultOutputFormattersProvider(IOptions optionsAccessor, - ITypeActivatorCache typeActivatorCache, - IServiceProvider serviceProvider) - : base(optionsAccessor.Options.OutputFormatters, typeActivatorCache, serviceProvider) - { - } - - /// - public IReadOnlyList OutputFormatters - { - get - { - return Options; - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/HttpNoContentOutputFormatter.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/HttpNoContentOutputFormatter.cs index 4b9f752f2a..5e0f2a52da 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Formatters/HttpNoContentOutputFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Formatters/HttpNoContentOutputFormatter.cs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; -using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNet.WebUtilities; using Microsoft.Net.Http.Headers; diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/HttpNotAcceptableOutputFormatter.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/HttpNotAcceptableOutputFormatter.cs index 298f6f2516..fda0195214 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Formatters/HttpNotAcceptableOutputFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Formatters/HttpNotAcceptableOutputFormatter.cs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; -using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNet.WebUtilities; using Microsoft.Net.Http.Headers; diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/IInputFormattersProvider.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/IInputFormattersProvider.cs deleted file mode 100644 index 384bb0b878..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/Formatters/IInputFormattersProvider.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// Provides an activated collection of instances. - /// - public interface IInputFormattersProvider - { - /// - /// Gets a collection of activated InputFormatter instances. - /// - IReadOnlyList InputFormatters { get; } - } -} diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/IJsonOutputFormatter.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/IJsonOutputFormatter.cs index cc07f4c437..10516814c8 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Formatters/IJsonOutputFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Formatters/IJsonOutputFormatter.cs @@ -8,7 +8,7 @@ namespace Microsoft.AspNet.Mvc /// /// /// The class filter the collection of - /// and use only those which implement + /// and use only those which implement /// . /// /// To create a custom formatter that can be used by , derive from diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/IOutputFormattersProvider.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/IOutputFormattersProvider.cs deleted file mode 100644 index ef0ac4b848..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/Formatters/IOutputFormattersProvider.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// Provides an activated collection of instances. - /// - public interface IOutputFormattersProvider - { - /// - /// Gets a collection of activated OutputFormatter instances. - /// - IReadOnlyList OutputFormatters { get; } - } -} diff --git a/src/Microsoft.AspNet.Mvc.Core/ModelBinders/BodyModelBinder.cs b/src/Microsoft.AspNet.Mvc.Core/ModelBinders/BodyModelBinder.cs index 0d9d7aa7da..fdb1cf6c78 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ModelBinders/BodyModelBinder.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ModelBinders/BodyModelBinder.cs @@ -6,8 +6,6 @@ using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNet.Mvc.Core; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.Internal; @@ -19,39 +17,25 @@ namespace Microsoft.AspNet.Mvc.ModelBinding /// public class BodyModelBinder : BindingSourceModelBinder { - private readonly ActionContext _actionContext; - private readonly IScopedInstance _bindingContext; - private readonly IInputFormatterSelector _formatterSelector; - private readonly IValidationExcludeFiltersProvider _bodyValidationExcludeFiltersProvider; - /// /// Creates a new . /// - /// An accessor to the . - /// An accessor to the . - /// The . - /// - /// The . - /// - public BodyModelBinder([NotNull] IScopedInstance context, - [NotNull] IScopedInstance bindingContext, - [NotNull] IInputFormatterSelector selector, - [NotNull] IValidationExcludeFiltersProvider bodyValidationExcludeFiltersProvider) + public BodyModelBinder() : base(BindingSource.Body) { - _actionContext = context.Value; - _bindingContext = bindingContext; - _formatterSelector = selector; - _bodyValidationExcludeFiltersProvider = bodyValidationExcludeFiltersProvider; } /// protected async override Task BindModelCoreAsync([NotNull] ModelBindingContext bindingContext) { - var formatters = _bindingContext.Value.InputFormatters; + var requestServices = bindingContext.OperationBindingContext.HttpContext.RequestServices; + + var formatterSelector = requestServices.GetRequiredService(); + var actionContext = requestServices.GetRequiredService>().Value; + var formatters = requestServices.GetRequiredService>().Value.InputFormatters; - var formatterContext = new InputFormatterContext(_actionContext, bindingContext.ModelType); - var formatter = _formatterSelector.SelectFormatter(formatters.ToList(), formatterContext); + var formatterContext = new InputFormatterContext(actionContext, bindingContext.ModelType); + var formatter = formatterSelector.SelectFormatter(formatters.ToList(), formatterContext); if (formatter == null) { diff --git a/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs b/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs index 86a4a7f8c4..581da93a87 100644 --- a/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/MvcOptions.cs @@ -5,9 +5,9 @@ using System.Collections.Generic; using Microsoft.AspNet.Mvc.ApplicationModels; using Microsoft.AspNet.Mvc.Core; -using Microsoft.AspNet.Mvc.OptionDescriptors; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding.Metadata; +using Microsoft.AspNet.Mvc.ModelBinding.Validation; namespace Microsoft.AspNet.Mvc { @@ -22,16 +22,16 @@ public class MvcOptions public MvcOptions() { Conventions = new List(); - ModelBinders = new List(); + ModelBinders = new List(); ViewEngines = new List(); - ValueProviderFactories = new List(); - OutputFormatters = new List(); - InputFormatters = new List(); + ValueProviderFactories = new List(); + OutputFormatters = new List(); + InputFormatters = new List(); Filters = new List(); FormatterMappings = new FormatterMappings(); - ValidationExcludeFilters = new List(); + ValidationExcludeFilters = new List(); ModelMetadataDetailsProviders = new List(); - ModelValidatorProviders = new List(); + ModelValidatorProviders = new List(); CacheProfiles = new Dictionary(StringComparer.OrdinalIgnoreCase); } @@ -68,25 +68,23 @@ public AntiForgeryOptions AntiForgeryOptions /// Gets a list of which are used to construct filters that /// apply to all actions. /// - public ICollection Filters { get; private set; } + public IList Filters { get; } /// - /// Gets a list of the which are used to construct - /// a list of by . + /// Gets a list of s that are used by this application. /// - public IList OutputFormatters { get; } + public IList OutputFormatters { get; } /// - /// Gets a list of the which are used to construct - /// a list of by . + /// Gets a list of s that are used by this application. /// - public IList InputFormatters { get; } + public IList InputFormatters { get; } /// - /// Gets a list of which are used to construct a list - /// of exclude filters by . + /// Gets a list of s that are used by this application. /// - public IList ValidationExcludeFilters { get; } + public IList ValidationExcludeFilters { get; } + = new List(); /// /// Gets or sets the maximum number of validation errors that are allowed by this application before further @@ -107,17 +105,14 @@ public int MaxModelValidationErrors } /// - /// Get a list of the used by the - /// Gets a list of the used by the - /// . + /// Gets a s used by this application. /// - public IList ModelBinders { get; } + public IList ModelBinders { get; } /// - /// Gets a list of the s used by - /// . + /// Gets a s used by this application. /// - public IList ModelValidatorProviders { get; } + public IList ModelValidatorProviders { get; } /// /// Gets a list of descriptors that represent used @@ -126,13 +121,12 @@ public int MaxModelValidationErrors public IList ViewEngines { get; } /// - /// Gets a list of descriptors that represent - /// used by this application. + /// Gets a used by this application. /// - public IList ValueProviderFactories { get; } + public IList ValueProviderFactories { get; } /// - /// Gets a list of instances that will be applied to + /// Gets a instances that will be applied to /// the when discovering actions. /// public IList Conventions { get; } @@ -150,7 +144,7 @@ public int MaxModelValidationErrors public IDictionary CacheProfiles { get; } /// - /// Gets a list of instances that will be used to + /// Gets a instances that will be used to /// create instances. /// /// diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultModelBindersProvider.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultModelBindersProvider.cs deleted file mode 100644 index c61c9a00f5..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultModelBindersProvider.cs +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.Framework.OptionsModel; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - public class DefaultModelBindersProvider : OptionDescriptorBasedProvider, IModelBinderProvider - { - /// - /// Initializes a new instance of the DefaultModelBindersProvider class. - /// - /// An accessor to the configured for this application. - /// As instance that creates an instance - /// of type . - /// A instance that retrieves services from the - /// service collection. - public DefaultModelBindersProvider( - IOptions optionsAccessor, - ITypeActivatorCache typeActivatorCache, - IServiceProvider serviceProvider) - : base(optionsAccessor.Options.ModelBinders, typeActivatorCache, serviceProvider) - { - } - - /// - public IReadOnlyList ModelBinders - { - get - { - return Options; - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultModelValidatorProviderProvider.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultModelValidatorProviderProvider.cs deleted file mode 100644 index dd26fe75f2..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultModelValidatorProviderProvider.cs +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; -using Microsoft.Framework.OptionsModel; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - public class DefaultModelValidatorProviderProvider : OptionDescriptorBasedProvider, - IModelValidatorProviderProvider - { - /// - /// Initializes a new instance of the class. - /// - /// An accessor to the configured for this application. - /// As instance that creates an instance - /// of type . - /// A instance that retrieves services from the - /// service collection. - public DefaultModelValidatorProviderProvider( - IOptions optionsAccessor, - ITypeActivatorCache typeActivatorCache, - IServiceProvider serviceProvider) - : base(optionsAccessor.Options.ModelValidatorProviders, typeActivatorCache, serviceProvider) - { - } - - /// - public IReadOnlyList ModelValidatorProviders - { - 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 deleted file mode 100644 index d31393664a..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultValueProviderFactoryProvider.cs +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.Framework.OptionsModel; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - public class DefaultValueProviderFactoryProvider : - OptionDescriptorBasedProvider, IValueProviderFactoryProvider - { - /// - /// Initializes a new instance of the class. - /// - /// An accessor to the configured for this application. - /// As instance that creates - /// an instance of type . - /// A instance that retrieves services from the - /// service collection. - public DefaultValueProviderFactoryProvider( - IOptions optionsAccessor, - ITypeActivatorCache typeActivatorCache, - IServiceProvider serviceProvider) - : base(optionsAccessor.Options.ValueProviderFactories, typeActivatorCache, 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 deleted file mode 100644 index 19e6fce394..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/DefaultViewEngineProvider.cs +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.Rendering; -using Microsoft.Framework.OptionsModel; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - public class DefaultViewEngineProvider : OptionDescriptorBasedProvider, IViewEngineProvider - { - /// - /// Initializes a new instance of the class. - /// - /// An accessor to the configured for this application. - /// As instance that creates - /// an instance of type . - /// A instance that retrieves services from the - /// service collection. - public DefaultViewEngineProvider( - IOptions optionsAccessor, - ITypeActivatorCache typeActivatorCache, - IServiceProvider serviceProvider) - : base(optionsAccessor.Options.ViewEngines, typeActivatorCache, serviceProvider) - { - } - - /// - public IReadOnlyList ViewEngines - { - get - { - return Options; - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ExcludeValidationDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ExcludeValidationDescriptor.cs deleted file mode 100644 index 9bdaf87dc8..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ExcludeValidationDescriptor.cs +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - /// Encapsulates information that describes an . - /// - public class ExcludeValidationDescriptor : OptionDescriptor - { - /// - /// Creates a new instance of . - /// - /// - /// A type that the descriptor represents. - /// - public ExcludeValidationDescriptor([NotNull] Type type) - : base(type) - { - } - - /// - /// Creates a new instance of . - /// - /// An instance of - /// that the descriptor represents. - public ExcludeValidationDescriptor([NotNull] IExcludeTypeValidationFilter validationFilter) - : base(validationFilter) - { - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/IOptionDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/IOptionDescriptor.cs deleted file mode 100644 index 1258976432..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/IOptionDescriptor.cs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - /// Encapsulates information that describes a option on . - /// - /// The type of the option. - public interface IOptionDescriptor - { - /// - /// Gets the type of the described by this - /// . - /// - Type OptionType { get; } - - /// - /// Gets the instance of described by this - /// . - /// - TOption Instance { get; } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/InputFormatterDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/InputFormatterDescriptor.cs deleted file mode 100644 index 35f11b3d3b..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/InputFormatterDescriptor.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - /// Encapsulates information that describes an . - /// - public class InputFormatterDescriptor : OptionDescriptor - { - /// - /// Creates a new instance of . - /// - /// A type that the descriptor represents. - /// - public InputFormatterDescriptor([NotNull] Type type) - : base(type) - { - } - - /// - /// Creates a new instance of . - /// - /// An instance of - /// that the descriptor represents. - public InputFormatterDescriptor([NotNull] IInputFormatter inputFormatter) - : base(inputFormatter) - { - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/InputFormatterDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/InputFormatterDescriptorExtensions.cs deleted file mode 100644 index 66037775d7..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/InputFormatterDescriptorExtensions.cs +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// Extension methods for adding Input formatters to a collection. - /// - public static class InputFormatterDescriptorExtensions - { - /// - /// Adds a type representing a to a descriptor collection. - /// - /// A list of InputFormatterDescriptors - /// Type representing an . - /// InputFormatterDescriptor representing the added instance. - public static InputFormatterDescriptor Add([NotNull] this IList descriptors, - [NotNull] Type inputFormatterType) - { - var descriptor = new InputFormatterDescriptor(inputFormatterType); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Inserts a type representing a to a descriptor collection. - /// - /// A list of InputFormatterDescriptors - /// Type representing an . - /// InputFormatterDescriptor representing the inserted instance. - public static InputFormatterDescriptor Insert([NotNull] this IList descriptors, - int index, - [NotNull] Type inputFormatterType) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException("index"); - } - - var descriptor = new InputFormatterDescriptor(inputFormatterType); - descriptors.Insert(index, descriptor); - return descriptor; - } - - /// - /// Adds an to a descriptor collection. - /// - /// A list of InputFormatterDescriptors - /// An instance. - /// InputFormatterDescriptor representing the added instance. - public static InputFormatterDescriptor Add([NotNull] this IList descriptors, - [NotNull] IInputFormatter inputFormatter) - { - var descriptor = new InputFormatterDescriptor(inputFormatter); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Insert an to a descriptor collection. - /// - /// A list of InputFormatterDescriptors - /// An instance. - /// InputFormatterDescriptor representing the added instance. - public static InputFormatterDescriptor Insert([NotNull] this IList descriptors, - int index, - [NotNull] IInputFormatter inputFormatter) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException("index"); - } - - var descriptor = new InputFormatterDescriptor(inputFormatter); - descriptors.Insert(index, descriptor); - return descriptor; - } - } -} \ 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 deleted file mode 100644 index d8d3777eae..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelBinderDescriptor.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - /// 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.Core/OptionDescriptors/ModelBinderDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelBinderDescriptorExtensions.cs deleted file mode 100644 index ee692f12ba..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelBinderDescriptorExtensions.cs +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// Extension methods for adding model binders to a collection. - /// - public static class ModelBinderDescriptorExtensions - { - /// - /// Adds a type representing a to a descriptor collection. - /// - /// A list of ModelBinderDescriptors - /// Type representing an . - /// ModelBinderDescriptor representing the added instance. - public static ModelBinderDescriptor Add([NotNull] this IList descriptors, - [NotNull] Type modelBinderType) - { - var descriptor = new ModelBinderDescriptor(modelBinderType); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Inserts a type representing a to a descriptor collection. - /// - /// A list of ModelBinderDescriptors - /// Type representing an . - /// ModelBinderDescriptor representing the inserted instance. - public static ModelBinderDescriptor Insert([NotNull] this IList descriptors, - int index, - [NotNull] Type modelBinderType) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException("index"); - } - - var descriptor = new ModelBinderDescriptor(modelBinderType); - descriptors.Insert(index, descriptor); - return descriptor; - } - - /// - /// Adds an to a descriptor collection. - /// - /// A list of ModelBinderDescriptors - /// An instance. - /// ModelBinderDescriptor representing the added instance. - public static ModelBinderDescriptor Add([NotNull] this IList descriptors, - [NotNull] IModelBinder modelBinder) - { - var descriptor = new ModelBinderDescriptor(modelBinder); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Insert an to a descriptor collection. - /// - /// A list of ModelBinderDescriptors - /// An instance. - /// ModelBinderDescriptor representing the added instance. - public static ModelBinderDescriptor Insert([NotNull] this IList descriptors, - int index, - [NotNull] IModelBinder modelBinder) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException("index"); - } - - var descriptor = new ModelBinderDescriptor(modelBinder); - descriptors.Insert(index, descriptor); - return descriptor; - } - - /// - /// Removes instances of from a descriptor collection - /// where the type exactly matches . - /// - /// A type that implements . - /// A list of ModelBinderDescriptors. - public static void RemoveTypesOf([NotNull] this IList descriptors) - where TInstance : class, IModelBinder - { - for (var i = descriptors.Count - 1; i >= 0; i--) - { - if (descriptors[i].OptionType == typeof(TInstance)) - { - descriptors.RemoveAt(i); - } - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelValidatorProviderDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelValidatorProviderDescriptor.cs deleted file mode 100644 index ca1ef139fa..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelValidatorProviderDescriptor.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - /// Encapsulates information that describes an . - /// - public class ModelValidatorProviderDescriptor : OptionDescriptor - { - /// - /// Creates a new instance of . - /// - /// A type that represents a . - public ModelValidatorProviderDescriptor([NotNull] Type type) - : base(type) - { - } - - /// - /// Creates a new instance of with the specified instance. - /// - /// An instance of . - public ModelValidatorProviderDescriptor([NotNull] IModelValidatorProvider validatorProvider) - : base(validatorProvider) - { - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelValidatorProviderDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelValidatorProviderDescriptorExtensions.cs deleted file mode 100644 index 2ee3317f8c..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ModelValidatorProviderDescriptorExtensions.cs +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// Extension methods for adding validator provider to a collection. - /// - public static class ModelValidatorProviderDescriptorExtensions - { - /// - /// Adds a type representing a to . - /// - /// A list of . - /// Type representing an - /// A representing the added instance. - public static ModelValidatorProviderDescriptor Add( - [NotNull] this IList descriptors, - [NotNull] Type modelValidatorProviderType) - { - var descriptor = new ModelValidatorProviderDescriptor(modelValidatorProviderType); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Inserts a type representing a in to at - /// the specified . - /// - /// A list of . - /// The zero-based index at which - /// should be inserted. - /// Type representing an - /// A representing the inserted instance. - public static ModelValidatorProviderDescriptor Insert( - [NotNull] this IList descriptors, - int index, - [NotNull] Type modelValidatorProviderType) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - - var descriptor = new ModelValidatorProviderDescriptor(modelValidatorProviderType); - descriptors.Insert(index, descriptor); - return descriptor; - } - - /// - /// Adds an to . - /// - /// A list of . - /// An instance. - /// A representing the added instance. - public static ModelValidatorProviderDescriptor Add( - [NotNull] this IList descriptors, - [NotNull] IModelValidatorProvider modelValidatorProvider) - { - var descriptor = new ModelValidatorProviderDescriptor(modelValidatorProvider); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Insert an in to at the specified - /// . - /// - /// A list of . - /// The zero-based index at which - /// should be inserted. - /// An instance. - /// A representing the added instance. - public static ModelValidatorProviderDescriptor Insert( - [NotNull] this IList descriptors, - int index, - [NotNull] IModelValidatorProvider modelValidatorProvider) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - - var descriptor = new ModelValidatorProviderDescriptor(modelValidatorProvider); - descriptors.Insert(index, descriptor); - return descriptor; - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptor.cs deleted file mode 100644 index a59f0ab546..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptor.cs +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.Core; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - /// Encapsulates information that describes a option on . - /// - /// The type of the option. - public class OptionDescriptor : IOptionDescriptor - { - /// - /// 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 deleted file mode 100644 index 54ffe1c761..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptorBasedProvider.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - /// 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 ITypeActivatorCache _typeActivatorCache; - private readonly IServiceProvider _serviceProvider; - - /// - /// Initializes a new instance of the class. - /// - /// An enumerable of . - /// As instance that creates an - /// instance of type . - /// A instance that retrieves services from the - /// service collection. - public OptionDescriptorBasedProvider( - [NotNull] IEnumerable> optionDescriptors, - [NotNull] ITypeActivatorCache typeActivatorCache, - [NotNull] IServiceProvider serviceProvider) - { - _optionDescriptors = optionDescriptors; - _typeActivatorCache = typeActivatorCache; - _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 = _typeActivatorCache.CreateInstance(_serviceProvider, descriptor.OptionType); - } - - result.Add(instance); - } - - return result; - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptorExtensions.cs deleted file mode 100644 index 94bcbd8c39..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OptionDescriptorExtensions.cs +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Linq; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// Extension methods for collections of option descriptors. - /// - public static class OptionDescriptorExtensions - { - /// - /// Returns the only instance of from a sequence of option descriptors. - /// - /// The type of the instance to find. - /// A sequence of . - /// The only instance of from a sequence of - /// . - /// - /// Thrown if there is not exactly one in the sequence. - public static TInstance InstanceOf( - [NotNull] this IEnumerable> descriptors) - { - var instance = descriptors - .Single(descriptor => descriptor.OptionType == typeof(TInstance) && descriptor.Instance != null) - .Instance; - return (TInstance)instance; - } - - /// - /// Returns the only instance of from a sequence of option descriptors, - /// or a default value if the sequence is empty. - /// - /// The type of the instance to find. - /// A sequence of . - /// The only instance of from a sequence of - /// , - /// or a default value if the sequence is empty. - /// - /// Thrown if there is more than one in the sequence. - public static TInstance InstanceOfOrDefault([NotNull] this - IEnumerable> descriptors) - { - var item = descriptors - .SingleOrDefault( - descriptor => descriptor.OptionType == typeof(TInstance) && descriptor.Instance != null); - var instance = default(TInstance); - if (item != null) - { - instance = (TInstance)item.Instance; - } - return instance; - } - - /// - /// Returns all the instances of from a sequence of option descriptors. - /// - /// The type of the instances to find. - /// A sequence of . - /// An IEnumerable of that contains instances from a sequence - /// of . - public static IEnumerable InstancesOf([NotNull] this - IEnumerable> descriptors) - { - var instances = descriptors - .Where(descriptor => descriptor.OptionType == typeof(TInstance) && descriptor.Instance != null) - .Select(d => (TInstance)d.Instance); - return instances; - } - } -} \ 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 deleted file mode 100644 index e600d4a7e6..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OutputFormatterDescriptor.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - /// Encapsulates information that describes an . - /// - public class OutputFormatterDescriptor : OptionDescriptor - { - /// - /// Creates a new instance of . - /// - /// A type that the descriptor represents. - /// - public OutputFormatterDescriptor([NotNull] Type type) - : base(type) - { - } - - /// - /// Creates a new instance of . - /// - /// An instance of - /// that the descriptor represents. - public OutputFormatterDescriptor([NotNull] IOutputFormatter outputFormatter) - : base(outputFormatter) - { - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OutputFormatterDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OutputFormatterDescriptorExtensions.cs deleted file mode 100644 index 8181775157..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/OutputFormatterDescriptorExtensions.cs +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// Extension methods for adding output formatters to a collection. - /// - public static class OutputFormatterDescriptorExtensions - { - /// - /// Adds a type representing a to a descriptor collection. - /// - /// A list of OutputFormatterDescriptors - /// Type representing an . - /// OutputFormatterDescriptor representing the added instance. - public static OutputFormatterDescriptor Add([NotNull] this IList descriptors, - [NotNull] Type outputFormatterType) - { - var descriptor = new OutputFormatterDescriptor(outputFormatterType); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Inserts a type representing a to a descriptor collection. - /// - /// A list of OutputFormatterDescriptors - /// Type representing an . - /// OutputFormatterDescriptor representing the inserted instance. - public static OutputFormatterDescriptor Insert([NotNull] this IList descriptors, - int index, - [NotNull] Type outputFormatterType) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException("index"); - } - - var descriptor = new OutputFormatterDescriptor(outputFormatterType); - descriptors.Insert(index, descriptor); - return descriptor; - } - - /// - /// Adds an to a descriptor collection. - /// - /// A list of OutputFormatterDescriptors - /// An instance. - /// OutputFormatterDescriptor representing the added instance. - public static OutputFormatterDescriptor Add([NotNull] this IList descriptors, - [NotNull] IOutputFormatter outputFormatter) - { - var descriptor = new OutputFormatterDescriptor(outputFormatter); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Insert an to a descriptor collection. - /// - /// A list of OutputFormatterDescriptors - /// An instance. - /// OutputFormatterDescriptor representing the added instance. - public static OutputFormatterDescriptor Insert([NotNull] this IList descriptors, - int index, - [NotNull] IOutputFormatter outputFormatter) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException("index"); - } - - var descriptor = new OutputFormatterDescriptor(outputFormatter); - descriptors.Insert(index, descriptor); - return descriptor; - } - - /// - /// Removes instances of from a descriptor collection - /// where the type exactly matches . - /// - /// A type that implements . - /// A list of OutputFormatterDescriptors. - public static void RemoveTypesOf([NotNull] this IList descriptors) - where TInstance : class, IOutputFormatter - { - for (var i = descriptors.Count - 1; i >= 0; i--) - { - if (descriptors[i].OptionType == typeof(TInstance)) - { - descriptors.RemoveAt(i); - } - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValidationExcludeFiltersExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValidationExcludeFiltersExtensions.cs deleted file mode 100644 index 0da40bf590..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValidationExcludeFiltersExtensions.cs +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; -using Microsoft.AspNet.Mvc.OptionDescriptors; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// Extensions for . - /// - public static class ValidationExcludeFiltersExtensions - { - /// - /// Adds a descriptor to the specified that excludes the properties of - /// the specified and its derived types from validaton. - /// - /// A list of which are used to - /// get a collection of exclude filters to be applied for filtering model properties during validation. - /// - /// which should be excluded from validation. - public static void Add(this IList descriptorCollection, Type type) - { - var typeBasedExcludeFilter = new DefaultTypeBasedExcludeFilter(type); - descriptorCollection.Add(new ExcludeValidationDescriptor(typeBasedExcludeFilter)); - } - - /// - /// Adds a descriptor to the specified that excludes the properties of - /// the type specified and its derived types from validaton. - /// - /// A list of which are used to - /// get a collection of exclude filters to be applied for filtering model properties during validation. - /// - /// Full name of the type which should be excluded from validation. - public static void Add(this IList descriptorCollection, string typeFullName) - { - var filter = new DefaultTypeNameBasedExcludeFilter(typeFullName); - descriptorCollection.Add(new ExcludeValidationDescriptor(filter)); - } - - /// - /// Adds a descriptor to the specified that excludes the properties of - /// the type specified and its derived types from validaton. - /// - /// A list of which are used to - /// get a collection of exclude filters to be applied for filtering model properties during validation. - /// - /// which should be excluded from validation. - /// - public static void Add(this IList descriptorCollection, - IExcludeTypeValidationFilter filter) - { - descriptorCollection.Add(new ExcludeValidationDescriptor(filter)); - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValueProviderFactoryDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValueProviderFactoryDescriptor.cs deleted file mode 100644 index 5da9b5ed83..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValueProviderFactoryDescriptor.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - /// - /// Encapsulates information that describes an . - /// - public class ValueProviderFactoryDescriptor : OptionDescriptor - { - /// - /// Creates a new instance of . - /// - /// The type that the descriptor represents. - public ValueProviderFactoryDescriptor([NotNull] Type type) - : base(type) - { - } - - /// - /// Creates a new instance of 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 deleted file mode 100644 index a274b518f3..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ValueProviderFactoryDescriptorExtensions.cs +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// 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; - } - - /// - /// Removes instances of from a descriptor collection - /// where the type exactly matches . - /// - /// A type that implements . - /// A list of ValueProviderFactoryDescriptors. - public static void RemoveTypesOf([NotNull] this IList descriptors) - where TInstance : class, IValueProviderFactory - { - for (var i = descriptors.Count - 1; i >= 0; i--) - { - if (descriptors[i].OptionType == typeof(TInstance)) - { - descriptors.RemoveAt(i); - } - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptorExtensions.cs deleted file mode 100644 index c2175a374d..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptorExtensions.cs +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.AspNet.Mvc.Rendering; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// Extension methods for adding view engines to a descriptor collection. - /// - public static class ViewEngineDescriptorExtensions - { - /// - /// Adds a type representing a to a descriptor collection. - /// - /// A list of ViewEngineDescriptors - /// Type representing an . - /// ViewEngineDescriptor representing the added instance. - public static ViewEngineDescriptor Add([NotNull] this IList descriptors, - [NotNull] Type viewEngineType) - { - var descriptor = new ViewEngineDescriptor(viewEngineType); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Inserts a type representing a to a descriptor collection. - /// - /// A list of ViewEngineDescriptors - /// Type representing an . - /// ViewEngineDescriptor representing the inserted instance. - public static ViewEngineDescriptor Insert([NotNull] this IList descriptors, - int index, - [NotNull] Type viewEngineType) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException("index"); - } - - var descriptor = new ViewEngineDescriptor(viewEngineType); - descriptors.Insert(index, descriptor); - return descriptor; - } - - /// - /// Adds an to a descriptor collection. - /// - /// A list of ViewEngineDescriptors - /// An instance. - /// ViewEngineDescriptor representing the added instance. - public static ViewEngineDescriptor Add([NotNull] this IList descriptors, - [NotNull] IViewEngine viewEngine) - { - var descriptor = new ViewEngineDescriptor(viewEngine); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Insert an to a descriptor collection. - /// - /// A list of ViewEngineDescriptors - /// An instance. - /// ViewEngineDescriptor representing the added instance. - public static ViewEngineDescriptor Insert([NotNull] this IList descriptors, - int index, - [NotNull] IViewEngine viewEngine) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException("index"); - } - - var descriptor = new ViewEngineDescriptor(viewEngine); - descriptors.Insert(index, descriptor); - return descriptor; - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/ViewEngine/CompositeViewEngine.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/ViewEngine/CompositeViewEngine.cs index f0f302dc3d..efc5caf7bb 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/ViewEngine/CompositeViewEngine.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/ViewEngine/CompositeViewEngine.cs @@ -1,24 +1,55 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using System.Linq; using Microsoft.Framework.Internal; +using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Mvc.Rendering { /// public class CompositeViewEngine : ICompositeViewEngine { - public CompositeViewEngine(IViewEngineProvider viewEngineProvider) + /// + /// Initializes a new instance of . + /// + /// The options accessor for . + /// As instance that creates + /// an instance of type . + /// A instance that retrieves services from the + /// service collection. + public CompositeViewEngine( + IOptions optionsAccessor, + ITypeActivatorCache typeActivatorCache, + IServiceProvider serviceProvider) { - ViewEngines = viewEngineProvider.ViewEngines; + var viewEngines = new List(); + foreach (var descriptor in optionsAccessor.Options.ViewEngines) + { + IViewEngine viewEngine; + if (descriptor.ViewEngine != null) + { + viewEngine = descriptor.ViewEngine; + } + else + { + viewEngine = typeActivatorCache.CreateInstance( + serviceProvider, + descriptor.ViewEngineType); + } + + viewEngines.Add(viewEngine); + } + + ViewEngines = viewEngines; } /// - /// Gets the list of ViewEngines the CompositeViewEngine delegates to. + /// Gets the list of this instance of delegates to. /// - public IReadOnlyList ViewEngines { get; private set; } + public IReadOnlyList ViewEngines { get; } /// public ViewEngineResult FindPartialView([NotNull] ActionContext context, diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/ViewEngine/IViewEngineProvider.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/ViewEngine/IViewEngineProvider.cs deleted file mode 100644 index 5fed0e6e1d..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/ViewEngine/IViewEngineProvider.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc.Rendering -{ - /// - /// Provides an activated collection of instances. - /// - public interface IViewEngineProvider - { - /// - /// Gets a collection of activated IViewEngine instances. - /// - IReadOnlyList ViewEngines { get; } - } -} diff --git a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/ViewEngineDescriptor.cs similarity index 54% rename from src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptor.cs rename to src/Microsoft.AspNet.Mvc.Core/ViewEngineDescriptor.cs index f74c123cc7..d026acf554 100644 --- a/src/Microsoft.AspNet.Mvc.Core/OptionDescriptors/ViewEngineDescriptor.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewEngineDescriptor.cs @@ -2,23 +2,30 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using Microsoft.AspNet.Mvc.Core; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.Framework.Internal; -namespace Microsoft.AspNet.Mvc.OptionDescriptors +namespace Microsoft.AspNet.Mvc { /// /// Encapsulates information that describes an . /// - public class ViewEngineDescriptor : OptionDescriptor + public class ViewEngineDescriptor { /// /// Creates a new instance of . /// /// The type that the descriptor represents. public ViewEngineDescriptor([NotNull] Type type) - : base(type) { + if (!typeof(IViewEngine).IsAssignableFrom(type)) + { + var message = Resources.FormatTypeMustDeriveFromType(type.FullName, typeof(IViewEngine).FullName); + throw new ArgumentException(message, nameof(type)); + } + + ViewEngineType = type; } /// @@ -26,8 +33,19 @@ public ViewEngineDescriptor([NotNull] Type type) /// /// An instance of that the descriptor represents. public ViewEngineDescriptor([NotNull] IViewEngine viewEngine) - : base(viewEngine) { + ViewEngine = viewEngine; + ViewEngineType = viewEngine.GetType(); } + + /// + /// Gets the type of the described by this . + /// + public Type ViewEngineType { get; } + + /// + /// Gets the instance described by this . + /// + public IViewEngine ViewEngine { get; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewEngineDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/ViewEngineDescriptorExtensions.cs new file mode 100644 index 0000000000..3584813e70 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/ViewEngineDescriptorExtensions.cs @@ -0,0 +1,140 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNet.Mvc.Rendering; +using Microsoft.Framework.Internal; + +namespace Microsoft.AspNet.Mvc +{ + /// + /// Extension methods for adding view engines to a descriptor collection. + /// + public static class ViewEngineDescriptorExtensions + { + /// + /// Adds a type representing a to a descriptor collection. + /// + /// A list of ViewEngineDescriptors + /// Type representing an . + /// ViewEngineDescriptor representing the added instance. + public static ViewEngineDescriptor Add( + [NotNull] this IList descriptors, + [NotNull] Type viewEngineType) + { + var descriptor = new ViewEngineDescriptor(viewEngineType); + descriptors.Add(descriptor); + return descriptor; + } + + /// + /// Inserts a type representing a to a descriptor collection. + /// + /// A list of ViewEngineDescriptors + /// Type representing an . + /// ViewEngineDescriptor representing the inserted instance. + public static ViewEngineDescriptor Insert( + [NotNull] this IList descriptors, + int index, + [NotNull] Type viewEngineType) + { + if (index < 0 || index > descriptors.Count) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + + var descriptor = new ViewEngineDescriptor(viewEngineType); + descriptors.Insert(index, descriptor); + return descriptor; + } + + /// + /// Adds an to a descriptor collection. + /// + /// A list of ViewEngineDescriptors + /// An instance. + /// ViewEngineDescriptor representing the added instance. + public static ViewEngineDescriptor Add( + [NotNull] this IList descriptors, + [NotNull] IViewEngine viewEngine) + { + var descriptor = new ViewEngineDescriptor(viewEngine); + descriptors.Add(descriptor); + return descriptor; + } + + /// + /// Insert an to a descriptor collection. + /// + /// A list of ViewEngineDescriptors + /// An instance. + /// ViewEngineDescriptor representing the added instance. + public static ViewEngineDescriptor Insert( + [NotNull] this IList descriptors, + int index, + [NotNull] IViewEngine viewEngine) + { + if (index < 0 || index > descriptors.Count) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + + var descriptor = new ViewEngineDescriptor(viewEngine); + descriptors.Insert(index, descriptor); + return descriptor; + } + + /// + /// Returns the only instance of from . + /// + /// The type of the instance to find. + /// The to search. + /// The only instance of in . + /// + /// Thrown if there is not exactly one in . + /// + public static TInstance InstanceOf( + [NotNull] this IList descriptors) + { + return descriptors + .Select(descriptor => descriptor.ViewEngine) + .OfType() + .Single(); + } + + /// + /// Returns the only instance of from + /// or null if the sequence is empty. + /// + /// The type of the instance to find. + /// The to search. + /// + /// Thrown if there is more than one in . + /// + public static TInstance InstanceOfOrDefault( + [NotNull] this IList descriptors) + { + return descriptors + .Select(descriptor => descriptor.ViewEngine) + .OfType() + .SingleOrDefault(); + } + + /// + /// Returns all instances of from . + /// + /// The type of the instances to find. + /// The to search. + /// An IEnumerable of that contains instances from + /// . + public static IEnumerable InstancesOf( + [NotNull] this IList descriptors) + { + return descriptors + .Select(descriptor => descriptor.ViewEngine) + .OfType(); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/BinderMetadata/ModelBinderAttribute.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/BinderMetadata/ModelBinderAttribute.cs index bd94966bdb..45770cea6c 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/BinderMetadata/ModelBinderAttribute.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/BinderMetadata/ModelBinderAttribute.cs @@ -41,14 +41,12 @@ public Type BinderType { if (value != null) { - if (!typeof(IModelBinder).IsAssignableFrom(value) && - !typeof(IModelBinderProvider).IsAssignableFrom(value)) + if (!typeof(IModelBinder).IsAssignableFrom(value)) { throw new InvalidOperationException( - Resources.FormatBinderType_MustBeIModelBinderOrIModelBinderProvider( + Resources.FormatBinderType_MustBeIModelBinder( value.FullName, - typeof(IModelBinder).FullName, - typeof(IModelBinderProvider).FullName)); + typeof(IModelBinder).FullName)); } } diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/BinderTypeBasedModelBinder.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/BinderTypeBasedModelBinder.cs index 6eb0710762..bebadda706 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/BinderTypeBasedModelBinder.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/BinderTypeBasedModelBinder.cs @@ -17,10 +17,9 @@ public class BinderTypeBasedModelBinder : IModelBinder { private readonly Func _createFactory = (t) => ActivatorUtilities.CreateFactory(t, Type.EmptyTypes); - private ConcurrentDictionary _typeActivatorCache = + private readonly ConcurrentDictionary _typeActivatorCache = new ConcurrentDictionary(); - public async Task BindModelAsync(ModelBindingContext bindingContext) { if (bindingContext.BinderType == null) @@ -37,19 +36,10 @@ public async Task BindModelAsync(ModelBindingContext binding var modelBinder = instance as IModelBinder; if (modelBinder == null) { - var modelBinderProvider = instance as IModelBinderProvider; - if (modelBinderProvider != null) - { - modelBinder = new CompositeModelBinder(modelBinderProvider.ModelBinders); - } - else - { - throw new InvalidOperationException( - Resources.FormatBinderType_MustBeIModelBinderOrIModelBinderProvider( - bindingContext.BinderType.FullName, - typeof(IModelBinder).FullName, - typeof(IModelBinderProvider).FullName)); - } + throw new InvalidOperationException( + Resources.FormatBinderType_MustBeIModelBinder( + bindingContext.BinderType.FullName, + typeof(IModelBinder).FullName)); } var result = await modelBinder.BindModelAsync(bindingContext); diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/IModelBinderProvider.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/IModelBinderProvider.cs deleted file mode 100644 index ce8de82f0b..0000000000 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/IModelBinderProvider.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc.ModelBinding -{ - /// - /// Provides an activated collection of instances. - /// - public interface IModelBinderProvider - { - /// - /// Gets a collection of activated ModelBinders instances. - /// - IReadOnlyList ModelBinders { get; } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/ModelBinders/ServicesModelBinder.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/ServicesModelBinder.cs similarity index 100% rename from src/Microsoft.AspNet.Mvc.Core/ModelBinders/ServicesModelBinder.cs rename to src/Microsoft.AspNet.Mvc.ModelBinding/Binders/ServicesModelBinder.cs diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Properties/Resources.Designer.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Properties/Resources.Designer.cs index b4bd26fbbb..44ed02e8f1 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Properties/Resources.Designer.cs @@ -459,19 +459,19 @@ internal static string FormatModelStateDictionary_MaxModelStateErrors() } /// - /// The type '{0}' must implement either '{1}' or '{2}' to be used as a model binder. + /// The type '{0}' must implement '{1}' to be used as a model binder. /// - internal static string BinderType_MustBeIModelBinderOrIModelBinderProvider + internal static string BinderType_MustBeIModelBinder { - get { return GetString("BinderType_MustBeIModelBinderOrIModelBinderProvider"); } + get { return GetString("BinderType_MustBeIModelBinder"); } } /// - /// The type '{0}' must implement either '{1}' or '{2}' to be used as a model binder. + /// The type '{0}' must implement '{1}' to be used as a model binder. /// - internal static string FormatBinderType_MustBeIModelBinderOrIModelBinderProvider(object p0, object p1, object p2) + internal static string FormatBinderType_MustBeIModelBinder(object p0, object p1) { - return string.Format(CultureInfo.CurrentCulture, GetString("BinderType_MustBeIModelBinderOrIModelBinderProvider"), p0, p1, p2); + return string.Format(CultureInfo.CurrentCulture, GetString("BinderType_MustBeIModelBinder"), p0, p1); } /// diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Resources.resx b/src/Microsoft.AspNet.Mvc.ModelBinding/Resources.resx index d1104cd28e..0654d7794a 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Resources.resx +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Resources.resx @@ -201,8 +201,8 @@ The maximum number of allowed model errors has been reached. - - The type '{0}' must implement either '{1}' or '{2}' to be used as a model binder. + + The type '{0}' must implement '{1}' to be used as a model binder. The value '{0}' is not valid for {1}. diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/DefaultObjectValidator.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/DefaultObjectValidator.cs index 8f46da0fe5..f54ecadba8 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/DefaultObjectValidator.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/DefaultObjectValidator.cs @@ -17,15 +17,21 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation /// public class DefaultObjectValidator : IObjectModelValidator { - private readonly IValidationExcludeFiltersProvider _excludeFilterProvider; + private readonly IList _excludeFilters; private readonly IModelMetadataProvider _modelMetadataProvider; - + + /// + /// Initializes a new instance of . + /// + /// s that determine + /// types to exclude from validation. + /// The . public DefaultObjectValidator( - IValidationExcludeFiltersProvider excludeFilterProvider, - IModelMetadataProvider modelMetadataProvider) + [NotNull] IList excludeFilters, + [NotNull] IModelMetadataProvider modelMetadataProvider) { - _excludeFilterProvider = excludeFilterProvider; _modelMetadataProvider = modelMetadataProvider; + _excludeFilters = excludeFilters; } /// @@ -39,13 +45,13 @@ public void Validate([NotNull] ModelValidationContext modelValidationContext) ValidateNonVisitedNodeAndChildren( modelValidationContext.RootPrefix, - validationContext, + validationContext, validators: null); } private bool ValidateNonVisitedNodeAndChildren( string modelKey, - ValidationContext validationContext, + ValidationContext validationContext, IList validators) { var modelValidationContext = validationContext.ModelValidationContext; @@ -98,7 +104,7 @@ private bool ValidateNonVisitedNodeAndChildren( // We don't need to recursively traverse the graph for types that shouldn't be validated var modelType = modelExplorer.Model.GetType(); - if (IsTypeExcludedFromValidation(_excludeFilterProvider.ExcludeFilters, modelType)) + if (IsTypeExcludedFromValidation(_excludeFilters, modelType)) { var result = ShallowValidate(modelKey, modelExplorer, validationContext, validators); MarkPropertiesAsSkipped(modelKey, modelExplorer.Metadata, validationContext); @@ -146,7 +152,7 @@ private void MarkPropertiesAsSkipped(string currentModelKey, ModelMetadata metad if (fieldValidationState != ModelValidationState.Unvalidated) { return; - } + } foreach (var childMetadata in metadata.Properties) { @@ -182,7 +188,7 @@ private bool ValidateProperties( var propertyBindingName = propertyMetadata.BinderModelName ?? propertyMetadata.PropertyName; var childKey = ModelBindingHelper.CreatePropertyModelName(currentModelKey, propertyBindingName); if (!ValidateNonVisitedNodeAndChildren( - childKey, + childKey, propertyValidationContext, validators: null)) { @@ -300,14 +306,8 @@ private static bool ShallowValidate( return isValid; } - private bool IsTypeExcludedFromValidation(IReadOnlyList filters, Type type) + private bool IsTypeExcludedFromValidation(IList filters, Type type) { - // This can be set to null in ModelBinding scenarios which does not flow through this path. - if (filters == null) - { - return false; - } - return filters.Any(filter => filter.IsTypeExcluded(type)); } diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultTypeBasedExcludeFilter.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/DefaultTypeBasedExcludeFilter.cs similarity index 93% rename from src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultTypeBasedExcludeFilter.cs rename to src/Microsoft.AspNet.Mvc.ModelBinding/Validation/DefaultTypeBasedExcludeFilter.cs index 86dac927c5..b8d50e03c4 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultTypeBasedExcludeFilter.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/DefaultTypeBasedExcludeFilter.cs @@ -2,10 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; using Microsoft.Framework.Internal; -namespace Microsoft.AspNet.Mvc +namespace Microsoft.AspNet.Mvc.ModelBinding.Validation { /// /// Provides an implementation of which can filter diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultTypeNameBasedExcludeFilter.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/DefaultTypeNameBasedExcludeFilter.cs similarity index 95% rename from src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultTypeNameBasedExcludeFilter.cs rename to src/Microsoft.AspNet.Mvc.ModelBinding/Validation/DefaultTypeNameBasedExcludeFilter.cs index 25be7d9272..cccb27a847 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Formatters/DefaultTypeNameBasedExcludeFilter.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/DefaultTypeNameBasedExcludeFilter.cs @@ -2,10 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; using Microsoft.Framework.Internal; -namespace Microsoft.AspNet.Mvc +namespace Microsoft.AspNet.Mvc.ModelBinding.Validation { /// /// Provides an implementation of which can filter diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/IModelValidatorProviderProvider.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/IModelValidatorProviderProvider.cs deleted file mode 100644 index da0ebb9ba9..0000000000 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/IModelValidatorProviderProvider.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc.ModelBinding.Validation -{ - /// - /// Provides an activated collection of instances. - /// - public interface IModelValidatorProviderProvider - { - /// - /// Gets a collection of activated instances. - /// - IReadOnlyList ModelValidatorProviders { get; } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/IValidationExcludeFiltersProvider.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/IValidationExcludeFiltersProvider.cs deleted file mode 100644 index 8874a76cd9..0000000000 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/IValidationExcludeFiltersProvider.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc.ModelBinding.Validation -{ - /// - /// Provides an activated collection of instances. - /// - public interface IValidationExcludeFiltersProvider - { - /// - /// Gets a collection of activated instances. - /// - IReadOnlyList ExcludeFilters { get; } - } -} diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/CompositeValueProviderFactory.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/CompositeValueProviderFactory.cs deleted file mode 100644 index 3ad9cafaa7..0000000000 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/CompositeValueProviderFactory.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Linq; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc.ModelBinding -{ - /// - 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/IValueProviderFactoryProvider.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/IValueProviderFactoryProvider.cs deleted file mode 100644 index 93cc387d94..0000000000 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/IValueProviderFactoryProvider.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc.ModelBinding -{ - /// - /// 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.Razor/OptionDescriptors/DefaultViewLocationExpanderProvider.cs b/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/DefaultViewLocationExpanderProvider.cs deleted file mode 100644 index 6db8a46f48..0000000000 --- a/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/DefaultViewLocationExpanderProvider.cs +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.OptionsModel; - -namespace Microsoft.AspNet.Mvc.Razor.OptionDescriptors -{ - /// - public class DefaultViewLocationExpanderProvider : - OptionDescriptorBasedProvider, IViewLocationExpanderProvider - { - /// - /// Initializes a new instance of the class. - /// - /// An accessor to the configured for this application. - /// A instance that creates a new instance - /// of type . - /// A instance that retrieves services from the - /// service collection. - public DefaultViewLocationExpanderProvider( - IOptions optionsAccessor, - ITypeActivatorCache typeActivatorCache, - IServiceProvider serviceProvider) - : base(optionsAccessor.Options.ViewLocationExpanders, typeActivatorCache, serviceProvider) - { - } - - /// - public IReadOnlyList ViewLocationExpanders - { - get { return Options; } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/IViewLocationExpanderProvider.cs b/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/IViewLocationExpanderProvider.cs deleted file mode 100644 index e788225028..0000000000 --- a/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/IViewLocationExpanderProvider.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc.Razor.OptionDescriptors -{ - /// - /// Provides an activated collection of instances. - /// - public interface IViewLocationExpanderProvider - { - /// - /// Gets a collection of activated instances. - /// - IReadOnlyList ViewLocationExpanders { get; } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/ViewLocationExpanderDescriptor.cs b/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/ViewLocationExpanderDescriptor.cs deleted file mode 100644 index 4feffc337d..0000000000 --- a/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/ViewLocationExpanderDescriptor.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc.Razor.OptionDescriptors -{ - /// - /// Encapsulates information that describes an . - /// - public class ViewLocationExpanderDescriptor : OptionDescriptor - { - /// - /// Creates a new instance of . - /// - /// A type that the descriptor represents. - /// - public ViewLocationExpanderDescriptor([NotNull] Type type) - : base(type) - { - } - - /// - /// Creates a new instance of . - /// - /// An instance of - /// that the descriptor represents. - public ViewLocationExpanderDescriptor([NotNull] IViewLocationExpander viewLocationExpander) - : base(viewLocationExpander) - { - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/ViewLocationExpanderDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/ViewLocationExpanderDescriptorExtensions.cs deleted file mode 100644 index 7763324812..0000000000 --- a/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/ViewLocationExpanderDescriptorExtensions.cs +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.Razor.OptionDescriptors; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc.Razor -{ - /// - /// Extension methods for adding view location expanders to a collection. - /// - public static class ViewLocationExpanderDescriptorExtensions - { - /// - /// Adds a type representing a to . - /// - /// A list of . - /// Type representing an - /// A representing the added instance. - public static ViewLocationExpanderDescriptor Add( - [NotNull] this IList descriptors, - [NotNull] Type viewLocationExpanderType) - { - var descriptor = new ViewLocationExpanderDescriptor(viewLocationExpanderType); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Inserts a type representing a in to at - /// the specified . - /// - /// A list of . - /// The zero-based index at which - /// should be inserted. - /// Type representing an - /// A representing the inserted instance. - public static ViewLocationExpanderDescriptor Insert( - [NotNull] this IList descriptors, - int index, - [NotNull] Type viewLocationExpanderType) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - - var descriptor = new ViewLocationExpanderDescriptor(viewLocationExpanderType); - descriptors.Insert(index, descriptor); - return descriptor; - } - - /// - /// Adds an to . - /// - /// A list of . - /// An instance. - /// A representing the added instance. - public static ViewLocationExpanderDescriptor Add( - [NotNull] this IList descriptors, - [NotNull] IViewLocationExpander viewLocationExpander) - { - var descriptor = new ViewLocationExpanderDescriptor(viewLocationExpander); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Insert an in to at the specified - /// . - /// - /// A list of . - /// The zero-based index at which - /// should be inserted. - /// An instance. - /// A representing the added instance. - public static ViewLocationExpanderDescriptor Insert( - [NotNull] this IList descriptors, - int index, - [NotNull] IViewLocationExpander viewLocationExpander) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - - var descriptor = new ViewLocationExpanderDescriptor(viewLocationExpander); - descriptors.Insert(index, descriptor); - return descriptor; - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs b/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs index fee66c9588..8a4691f31e 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs @@ -219,7 +219,7 @@ internal static string FormatRazorPage_MethodCannotBeCalled(object p0) } /// - /// {0} has not been called for the page '{1}'. + /// {0} has not been called for the page at '{1}'. /// internal static string RenderBodyNotCalled { @@ -227,7 +227,7 @@ internal static string RenderBodyNotCalled } /// - /// {0} has not been called for the page '{1}'. + /// {0} has not been called for the page at '{1}'. /// internal static string FormatRenderBodyNotCalled(object p0, object p1) { @@ -283,7 +283,7 @@ internal static string FormatSectionNotDefined(object p0) } /// - /// The following sections have been defined but have not been rendered for the page '{0}': '{1}'. + /// The following sections have been defined but have not been rendered by the page at '{0}': '{1}'. /// internal static string SectionsNotRendered { @@ -291,7 +291,7 @@ internal static string SectionsNotRendered } /// - /// The following sections have been defined but have not been rendered for the page '{0}': '{1}'. + /// The following sections have been defined but have not been rendered by the page at '{0}': '{1}'. /// internal static string FormatSectionsNotRendered(object p0, object p1) { diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorViewEngine.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorViewEngine.cs index b6dc4c0381..75317a0444 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorViewEngine.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorViewEngine.cs @@ -6,10 +6,9 @@ using System.Diagnostics; using System.Globalization; using System.Linq; -using Microsoft.AspNet.Mvc.Razor.Internal; -using Microsoft.AspNet.Mvc.Razor.OptionDescriptors; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.Framework.Internal; +using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Mvc.Razor { @@ -41,7 +40,7 @@ public class RazorViewEngine : IRazorViewEngine private readonly IRazorPageFactory _pageFactory; private readonly IRazorViewFactory _viewFactory; - private readonly IReadOnlyList _viewLocationExpanders; + private readonly IList _viewLocationExpanders; private readonly IViewLocationCache _viewLocationCache; /// @@ -50,12 +49,12 @@ public class RazorViewEngine : IRazorViewEngine /// The page factory used for creating instances. public RazorViewEngine(IRazorPageFactory pageFactory, IRazorViewFactory viewFactory, - IViewLocationExpanderProvider viewLocationExpanderProvider, + IOptions optionsAccessor, IViewLocationCache viewLocationCache) { _pageFactory = pageFactory; _viewFactory = viewFactory; - _viewLocationExpanders = viewLocationExpanderProvider.ViewLocationExpanders; + _viewLocationExpanders = optionsAccessor.Options.ViewLocationExpanders; _viewLocationCache = viewLocationCache; } diff --git a/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/RazorViewEngineOptions.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorViewEngineOptions.cs similarity index 73% rename from src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/RazorViewEngineOptions.cs rename to src/Microsoft.AspNet.Mvc.Razor/RazorViewEngineOptions.cs index e906a8b483..6a505a24fa 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/RazorViewEngineOptions.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorViewEngineOptions.cs @@ -4,23 +4,21 @@ using System; using System.Collections.Generic; using Microsoft.AspNet.FileProviders; -using Microsoft.AspNet.Mvc.Razor.OptionDescriptors; namespace Microsoft.AspNet.Mvc.Razor { /// - /// Provides programmatic configuration for the default . + /// Provides programmatic configuration for the . /// public class RazorViewEngineOptions { private IFileProvider _fileProvider; /// - /// Get a of descriptors for s used by this - /// application. + /// Get a used by the . /// - public IList ViewLocationExpanders { get; } - = new List(); + public IList ViewLocationExpanders { get; } + = new List(); /// /// Gets or sets the used by to locate Razor files on diff --git a/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs b/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs index 8423865cfb..e057662157 100644 --- a/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs +++ b/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs @@ -32,7 +32,7 @@ public static void ConfigureMvc(MvcOptions options) // Set up ModelBinding options.ModelBinders.Add(new BinderTypeBasedModelBinder()); options.ModelBinders.Add(new ServicesModelBinder()); - options.ModelBinders.Add(typeof(BodyModelBinder)); + options.ModelBinders.Add(new BodyModelBinder()); options.ModelBinders.Add(new HeaderModelBinder()); options.ModelBinders.Add(new TypeConverterModelBinder()); options.ModelBinders.Add(new TypeMatchModelBinder()); diff --git a/src/Microsoft.AspNet.Mvc/MvcServices.cs b/src/Microsoft.AspNet.Mvc/MvcServices.cs index 6682f2164f..23002b7c7a 100644 --- a/src/Microsoft.AspNet.Mvc/MvcServices.cs +++ b/src/Microsoft.AspNet.Mvc/MvcServices.cs @@ -10,11 +10,9 @@ using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding.Metadata; using Microsoft.AspNet.Mvc.ModelBinding.Validation; -using Microsoft.AspNet.Mvc.OptionDescriptors; using Microsoft.AspNet.Mvc.Razor; using Microsoft.AspNet.Mvc.Razor.Compilation; using Microsoft.AspNet.Mvc.Razor.Directives; -using Microsoft.AspNet.Mvc.Razor.OptionDescriptors; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Mvc.Routing; using Microsoft.AspNet.Mvc.ViewComponents; @@ -63,7 +61,12 @@ public static IServiceCollection GetDefaultServices() services.AddSingleton(); services.AddSingleton(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(serviceProvider => + { + var options = serviceProvider.GetRequiredService>().Options; + var modelMetadataProvider = serviceProvider.GetRequiredService(); + return new DefaultObjectValidator(options.ValidationExcludeFilters, modelMetadataProvider); + }); services.AddTransient(); @@ -90,24 +93,14 @@ public static IServiceCollection GetDefaultServices() }); services.AddTransient(); - services.AddScoped(); - - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); services.AddInstance(new JsonOutputFormatter()); - services.AddTransient(); - services.AddTransient(); - // Razor, Views and runtime compilation // The provider is inexpensive to initialize and provides ViewEngines that may require request // specific services. services.AddScoped(); - services.AddTransient(); - // Transient since the IViewLocationExpanders returned by the instance is cached by view engines. - services.AddTransient(); + // Caches view locations that are valid for the lifetime of the application. services.AddSingleton(); services.AddSingleton(serviceProvider => diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedAtActionResultTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedAtActionResultTests.cs index 0214712d41..26ba66f262 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedAtActionResultTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedAtActionResultTests.cs @@ -92,13 +92,13 @@ private static HttpContext GetHttpContext() httpContext.Request.PathBase = new PathString(""); httpContext.Response.Body = new MemoryStream(); var services = new Mock(); - services.Setup(p => p.GetService(typeof(IOutputFormattersProvider))).Returns(new TestOutputFormatterProvider()); httpContext.RequestServices = services.Object; - var options = new Mock>(); - options.SetupGet(o => o.Options) - .Returns(new MvcOptions()); + + var optionsAccessor = new MockMvcOptionsAccessor(); + optionsAccessor.Options.OutputFormatters.Add(new StringOutputFormatter()); + optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter()); services.Setup(p => p.GetService(typeof(IOptions))) - .Returns(options.Object); + .Returns(optionsAccessor); return httpContext; } @@ -110,20 +110,5 @@ private static IUrlHelper GetMockUrlHelper(string returnValue) return urlHelper.Object; } - - private class TestOutputFormatterProvider : IOutputFormattersProvider - { - public IReadOnlyList OutputFormatters - { - get - { - return new List() - { - new StringOutputFormatter(), - new JsonOutputFormatter() - }; - } - } - } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedAtRouteResultTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedAtRouteResultTests.cs index c6a258bc6a..b3362aab46 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedAtRouteResultTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedAtRouteResultTests.cs @@ -99,15 +99,13 @@ private static HttpContext GetHttpContext() httpContext.Setup(o => o.Request) .Returns(request); - var options = new Mock>(); - options.SetupGet(o => o.Options) - .Returns(new MvcOptions()); + var optionsAccessor = new MockMvcOptionsAccessor(); + optionsAccessor.Options.OutputFormatters.Add(new StringOutputFormatter()); + optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter()); httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions))) - .Returns(options.Object); + .Returns(optionsAccessor); httpContext.Setup(o => o.Response) .Returns(response); - httpContext.Setup(o => o.RequestServices.GetService(typeof(IOutputFormattersProvider))) - .Returns(new TestOutputFormatterProvider()); return httpContext.Object; } @@ -119,20 +117,5 @@ private static IUrlHelper GetMockUrlHelper(string returnValue) return urlHelper.Object; } - - private class TestOutputFormatterProvider : IOutputFormattersProvider - { - public IReadOnlyList OutputFormatters - { - get - { - return new List() - { - new StringOutputFormatter(), - new JsonOutputFormatter() - }; - } - } - } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedResultTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedResultTests.cs index 023141ca17..9e990914b7 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedResultTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedResultTests.cs @@ -87,29 +87,12 @@ private static HttpContext GetHttpContext() .Returns(request); httpContext.Setup(o => o.Response) .Returns(response); - httpContext.Setup(o => o.RequestServices.GetService(typeof(IOutputFormattersProvider))) - .Returns(new TestOutputFormatterProvider()); - var options = new Mock>(); - options.SetupGet(o => o.Options) - .Returns(new MvcOptions()); - httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions))) - .Returns(options.Object); + var optionsAccessor = new MockMvcOptionsAccessor(); + optionsAccessor.Options.OutputFormatters.Add(new StringOutputFormatter()); + optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter()); + httpContext.Setup(p => p.RequestServices.GetService(typeof(IOptions))) + .Returns(optionsAccessor); return httpContext.Object; } - - private class TestOutputFormatterProvider : IOutputFormattersProvider - { - public IReadOnlyList OutputFormatters - { - get - { - return new List() - { - new StringOutputFormatter(), - new JsonOutputFormatter() - }; - } - } - } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/HttpNotFoundObjectResultTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/HttpNotFoundObjectResultTest.cs index a43f508abe..408f854346 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/HttpNotFoundObjectResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/HttpNotFoundObjectResultTest.cs @@ -71,7 +71,6 @@ private static ActionContext CreateMockActionContext( string requestAcceptCharsetHeader = "", bool respectBrowserAcceptHeader = false) { - var formatters = new IOutputFormatter[] { new StringOutputFormatter(), new JsonOutputFormatter() }; var httpContext = new Mock(); if (response != null) { @@ -89,17 +88,12 @@ private static ActionContext CreateMockActionContext( httpContext.Setup(o => o.Request).Returns(request); httpContext.Setup(o => o.RequestServices).Returns(GetServiceProvider()); - httpContext.Setup(o => o.RequestServices.GetService(typeof(IOutputFormattersProvider))) - .Returns(new TestOutputFormatterProvider(formatters)); - - var options = new Mock>(); - options.SetupGet(o => o.Options) - .Returns(new MvcOptions() - { - RespectBrowserAcceptHeader = respectBrowserAcceptHeader - }); + var optionsAccessor = new MockMvcOptionsAccessor(); + optionsAccessor.Options.OutputFormatters.Add(new StringOutputFormatter()); + optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter()); + optionsAccessor.Options.RespectBrowserAcceptHeader = respectBrowserAcceptHeader; httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions))) - .Returns(options.Object); + .Returns(optionsAccessor); return new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor()); } @@ -113,26 +107,8 @@ private static IServiceProvider GetServiceProvider() optionsAccessor.SetupGet(o => o.Options).Returns(options); var serviceCollection = new ServiceCollection(); - serviceCollection.AddInstance>(optionsAccessor.Object); + serviceCollection.AddInstance(optionsAccessor.Object); return serviceCollection.BuildServiceProvider(); } - - private class TestOutputFormatterProvider : IOutputFormattersProvider - { - private readonly IEnumerable _formatters; - - public TestOutputFormatterProvider(IEnumerable formatters) - { - _formatters = formatters; - } - - public IReadOnlyList OutputFormatters - { - get - { - return _formatters.ToList(); - } - } - } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ObjectResultTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ObjectResultTests.cs index ef886a8e37..94510857ab 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ObjectResultTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ObjectResultTests.cs @@ -874,17 +874,15 @@ private static ActionContext CreateMockActionContext( httpContext.Setup(o => o.Request).Returns(request); httpContext.Setup(o => o.RequestServices).Returns(GetServiceProvider()); - httpContext.Setup(o => o.RequestServices.GetService(typeof(IOutputFormattersProvider))) - .Returns(new TestOutputFormatterProvider(outputFormatters)); - - var options = new Mock>(); - options.SetupGet(o => o.Options) - .Returns(new MvcOptions() - { - RespectBrowserAcceptHeader = respectBrowserAcceptHeader - }); + + var optionsAccessor = new MockMvcOptionsAccessor(); + foreach (var formatter in outputFormatters) + { + optionsAccessor.Options.OutputFormatters.Add(formatter); + } + optionsAccessor.Options.RespectBrowserAcceptHeader = respectBrowserAcceptHeader; httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions))) - .Returns(options.Object); + .Returns(optionsAccessor); return new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor()); } @@ -920,7 +918,7 @@ private static IServiceProvider GetServiceProvider() optionsAccessor.SetupGet(o => o.Options).Returns(options); var serviceCollection = new ServiceCollection(); - serviceCollection.AddInstance>(optionsAccessor.Object); + serviceCollection.AddInstance(optionsAccessor.Object); return serviceCollection.BuildServiceProvider(); } @@ -937,24 +935,6 @@ public virtual Task WriteAsync(OutputFormatterContext context) } } - private class TestOutputFormatterProvider : IOutputFormattersProvider - { - private readonly IEnumerable _formatters; - - public TestOutputFormatterProvider(IEnumerable formatters) - { - _formatters = formatters; - } - - public IReadOnlyList OutputFormatters - { - get - { - return _formatters.ToList(); - } - } - } - public class Person { public string Name { get; set; } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/BodyModelBinderTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/BodyModelBinderTests.cs index 1cbcfb3762..e7d9ffb909 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/BodyModelBinderTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/BodyModelBinderTests.cs @@ -9,8 +9,8 @@ using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Core; using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; using Microsoft.AspNet.Routing; +using Microsoft.Framework.DependencyInjection; using Microsoft.Net.Http.Headers; using Moq; using Xunit; @@ -27,13 +27,14 @@ public async Task BindModel_CallsSelectedInputFormatterOnce() mockInputFormatter.Setup(o => o.ReadAsync(It.IsAny())) .Returns(Task.FromResult(new Person())) .Verifiable(); + var inputFormatter = mockInputFormatter.Object; var provider = new TestModelMetadataProvider(); provider.ForType().BindingDetails(d => d.BindingSource = BindingSource.Body); - var bindingContext = GetBindingContext(typeof(Person), metadataProvider: provider); + var bindingContext = GetBindingContext(typeof(Person), inputFormatter, metadataProvider: provider); - var binder = GetBodyBinder(mockInputFormatter.Object); + var binder = new BodyModelBinder(); // Act var binderResult = await binder.BindModelAsync(bindingContext); @@ -199,6 +200,7 @@ private static ModelBindingContext GetBindingContext( { httpContext = new DefaultHttpContext(); } + UpdateServiceProvider(httpContext, inputFormatter); if (metadataProvider == null) { @@ -207,7 +209,7 @@ private static ModelBindingContext GetBindingContext( var operationBindingContext = new OperationBindingContext { - ModelBinder = GetBodyBinder(httpContext, inputFormatter), + ModelBinder = new BodyModelBinder(), MetadataProvider = metadataProvider, HttpContext = httpContext, }; @@ -225,14 +227,9 @@ private static ModelBindingContext GetBindingContext( return bindingContext; } - private static BodyModelBinder GetBodyBinder(IInputFormatter inputFormatter) + private static void UpdateServiceProvider(HttpContext httpContext, IInputFormatter inputFormatter) { - return GetBodyBinder(new DefaultHttpContext(), inputFormatter); - } - - private static BodyModelBinder GetBodyBinder(HttpContext httpContext, IInputFormatter inputFormatter) - { - var actionContext = CreateActionContext(httpContext); + var serviceProvider = new ServiceCollection(); var inputFormatterSelector = new Mock(); inputFormatterSelector .Setup(o => o.SelectFormatter( @@ -240,9 +237,7 @@ private static BodyModelBinder GetBodyBinder(HttpContext httpContext, IInputForm It.IsAny())) .Returns(inputFormatter); - var bodyValidationPredicatesProvider = new Mock(); - bodyValidationPredicatesProvider.SetupGet(o => o.ExcludeFilters) - .Returns(new List()); + serviceProvider.AddInstance(inputFormatterSelector.Object); var bindingContext = new ActionBindingContext() { @@ -253,14 +248,10 @@ private static BodyModelBinder GetBodyBinder(HttpContext httpContext, IInputForm { Value = bindingContext, }; + serviceProvider.AddInstance>(bindingContextAccessor); + serviceProvider.AddInstance(CreateActionContext(httpContext)); - var binder = new BodyModelBinder( - actionContext, - bindingContextAccessor, - inputFormatterSelector.Object, - bodyValidationPredicatesProvider.Object); - - return binder; + httpContext.RequestServices = serviceProvider.BuildServiceProvider(); } private static IScopedInstance CreateActionContext(HttpContext context) diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerActionInvokerTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerActionInvokerTest.cs index 9728174cdd..90635e5f4b 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerActionInvokerTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerActionInvokerTest.cs @@ -1991,26 +1991,22 @@ private TestControllerActionInvoker CreateInvoker( var httpContext = new Mock(MockBehavior.Loose); var httpRequest = new DefaultHttpContext().Request; var httpResponse = new DefaultHttpContext().Response; - var mockFormattersProvider = new Mock(); - mockFormattersProvider.SetupGet(o => o.OutputFormatters) - .Returns( - new List() - { - new JsonOutputFormatter() - }); + httpContext.SetupGet(c => c.Request).Returns(httpRequest); httpContext.SetupGet(c => c.Response).Returns(httpResponse); - httpContext.Setup(o => o.RequestServices.GetService(typeof(IOutputFormattersProvider))) - .Returns(mockFormattersProvider.Object); httpContext.Setup(o => o.RequestServices.GetService(typeof(ITempDataDictionary))) .Returns(tempData); httpResponse.Body = new MemoryStream(); - var options = new Mock>(); - options.SetupGet(o => o.Options) - .Returns(new MvcOptions()); + var options = new MvcOptions(); + options.OutputFormatters.Add(new JsonOutputFormatter()); + + var optionsAccessor = new Mock>(); + optionsAccessor.SetupGet(o => o.Options) + .Returns(options); + httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions))) - .Returns(options.Object); + .Returns(optionsAccessor.Object); var actionContext = new ActionContext( httpContext: httpContext.Object, @@ -2035,22 +2031,16 @@ private TestControllerActionInvoker CreateInvoker( .Returns(DefaultOrder.DefaultFrameworkSortOrder); - var inputFormattersProvider = new Mock(); - inputFormattersProvider.SetupGet(o => o.InputFormatters) - .Returns(new List()); - var excludeFilterProvider = new Mock(); - excludeFilterProvider.SetupGet(o => o.ExcludeFilters) - .Returns(new List()); var invoker = new TestControllerActionInvoker( actionContext, new[] { filterProvider.Object }, new MockControllerFactory(this), actionDescriptor, - inputFormattersProvider.Object, + new InputFormatter[0], Mock.Of(), - new MockModelBinderProvider(), - new MockModelValidatorProviderProvider(), - new MockValueProviderFactoryProvider(), + new IModelBinder[0], + new IModelValidatorProvider[0], + new IValueProviderFactory[0], new MockScopedInstance(), tempData); @@ -2095,23 +2085,20 @@ public async Task Invoke_UsesDefaultValuesIfNotBound() var controllerFactory = new Mock(); controllerFactory.Setup(c => c.CreateController(It.IsAny())) .Returns(new TestController()); - var inputFormattersProvider = new Mock(); - inputFormattersProvider.SetupGet(o => o.InputFormatters) - .Returns(new List()); var metadataProvider = new EmptyModelMetadataProvider(); var invoker = new ControllerActionInvoker( actionContext, new List(), controllerFactory.Object, actionDescriptor, - inputFormattersProvider.Object, + new IInputFormatter[0], new DefaultControllerActionArgumentBinder( metadataProvider, - new DefaultObjectValidator(Mock.Of(), metadataProvider), + new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider), new MockMvcOptionsAccessor()), - new MockModelBinderProvider() { ModelBinders = new List() { binder.Object } }, - new MockModelValidatorProviderProvider(), - new MockValueProviderFactoryProvider(), + new IModelBinder[] { binder.Object }, + new IModelValidatorProvider[0], + new IValueProviderFactory[0], new MockScopedInstance(), Mock.Of()); @@ -2205,11 +2192,11 @@ public TestControllerActionInvoker( IFilterProvider[] filterProvider, MockControllerFactory controllerFactory, ControllerActionDescriptor descriptor, - IInputFormattersProvider inputFormattersProvider, + IReadOnlyList inputFormatters, IControllerActionArgumentBinder controllerActionArgumentBinder, - IModelBinderProvider modelBinderProvider, - IModelValidatorProviderProvider modelValidatorProviderProvider, - IValueProviderFactoryProvider valueProviderFactoryProvider, + IReadOnlyList modelBinders, + IReadOnlyList modelValidatorProviders, + IReadOnlyList valueProviderFactories, IScopedInstance actionBindingContext, ITempDataDictionary tempData) : base( @@ -2217,11 +2204,11 @@ public TestControllerActionInvoker( filterProvider, controllerFactory, descriptor, - inputFormattersProvider, + inputFormatters, controllerActionArgumentBinder, - modelBinderProvider, - modelValidatorProviderProvider, - valueProviderFactoryProvider, + modelBinders, + modelValidatorProviders, + valueProviderFactories, actionBindingContext, tempData) { diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs index b92c3a665c..5b4a9b5f99 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs @@ -1681,7 +1681,7 @@ private static Controller GetController(IModelBinder binder, IValueProvider prov MetadataProvider = metadataProvider, ViewData = viewData, TempData = tempData, - ObjectValidator = new DefaultObjectValidator(Mock.Of(), metadataProvider) + ObjectValidator = new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider) }; } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerFactoryTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerFactoryTest.cs index 76c64c29b7..8e9a5b192b 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerFactoryTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerFactoryTest.cs @@ -226,7 +226,7 @@ private IServiceProvider GetServices() services.Setup(s => s.GetService(typeof(TestService))) .Returns(new TestService()); services.Setup(s => s.GetService(typeof(IObjectModelValidator))) - .Returns(new DefaultObjectValidator(Mock.Of(), metadataProvider)); + .Returns(new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider)); services .Setup(s => s.GetService(typeof(IScopedInstance))) .Returns(new MockScopedInstance()); diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Description/DefaultApiDescriptionProviderTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Description/DefaultApiDescriptionProviderTest.cs index 207b71bb17..70d660a378 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Description/DefaultApiDescriptionProviderTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Description/DefaultApiDescriptionProviderTest.cs @@ -12,6 +12,7 @@ using Microsoft.AspNet.Mvc.Routing; using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing.Constraints; +using Microsoft.Framework.OptionsModel; using Microsoft.Net.Http.Headers; using Moq; using Xunit; @@ -911,14 +912,14 @@ public void GetApiDescription_WithControllerProperties_Merges_ParameterDescripti // Act var descriptions = GetApiDescriptions(action); - // Assert + // Assert var description = Assert.Single(descriptions); Assert.Equal(5, description.ParameterDescriptions.Count); var name = Assert.Single(description.ParameterDescriptions, p => p.Name == "name"); Assert.Same(BindingSource.Query, name.Source); Assert.Equal(typeof(string), name.Type); - + var id = Assert.Single(description.ParameterDescriptions, p => p.Name == "Id"); Assert.Same(BindingSource.Path, id.Source); Assert.Equal(typeof(int), id.Type); @@ -947,8 +948,15 @@ private IReadOnlyList GetApiDescriptions( { var context = new ApiDescriptionProviderContext(new ActionDescriptor[] { action }); - var formattersProvider = new Mock(MockBehavior.Strict); - formattersProvider.Setup(fp => fp.OutputFormatters).Returns(formatters); + var options = new MvcOptions(); + foreach (var formatter in formatters) + { + options.OutputFormatters.Add(formatter); + } + + var optionsAccessor = new Mock>(); + optionsAccessor.SetupGet(o => o.Options) + .Returns(options); var constraintResolver = new Mock(); constraintResolver.Setup(c => c.ResolveConstraint("int")) @@ -957,7 +965,7 @@ private IReadOnlyList GetApiDescriptions( var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var provider = new DefaultApiDescriptionProvider( - formattersProvider.Object, + optionsAccessor.Object, constraintResolver.Object, modelMetadataProvider); @@ -1118,7 +1126,7 @@ private void AcceptsId_Header([FromHeader] int id) { } - private void AcceptsFormatters_Services([FromServices] IOutputFormattersProvider formatters) + private void AcceptsFormatters_Services([FromServices] ITestService tempDataProvider) { } @@ -1345,5 +1353,10 @@ public void SetContentTypes(IList contentTypes) } } } + + private interface ITestService + { + + } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ExcludeTypeValidationFilterExtensionsTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ExcludeTypeValidationFilterExtensionsTests.cs new file mode 100644 index 0000000000..ce9302ded6 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ExcludeTypeValidationFilterExtensionsTests.cs @@ -0,0 +1,41 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; +using Microsoft.AspNet.Mvc.ModelBinding.Validation; +using Xunit; + +namespace Microsoft.AspNet.Mvc +{ + public class ExcludeTypeValidationFilterExtensionsTests + { + [Fact] + public void InputFormatterDescriptors_AddsTypesAndTypeNames() + { + // Arrange + var type1 = typeof(BaseType); + var collection = new List(); + + // Act + collection.Add(type1); + collection.Add(type1.FullName); + + // Assert + Assert.Collection(collection, + first => + { + var filter = Assert.IsType(first); + Assert.Equal(type1, filter.ExcludedType); + }, + second => + { + var filter = Assert.IsType(second); + Assert.Equal(type1.FullName, filter.ExcludedTypeName); + }); + } + + private class BaseType + { + } + } +} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/JsonResultTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/JsonResultTest.cs index f0254eb1a7..7480117151 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/JsonResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/JsonResultTest.cs @@ -176,7 +176,7 @@ public async Task ExecuteResultAsync_UsesGlobalFormatter_IfNoFormatterIsConfigur } private HttpContext GetHttpContext( - IReadOnlyList optionsFormatters = null, + IReadOnlyList outputFormatters = null, bool enableFallback = false) { var httpContext = new DefaultHttpContext(); @@ -185,20 +185,21 @@ private HttpContext GetHttpContext( var services = new Mock(MockBehavior.Strict); httpContext.RequestServices = services.Object; - var mockFormattersProvider = new Mock(); - mockFormattersProvider - .SetupGet(o => o.OutputFormatters) - .Returns(optionsFormatters ?? new List()); + var options = new MvcOptions(); + if (outputFormatters != null) + { + foreach (var formatter in outputFormatters) + { + options.OutputFormatters.Add(formatter); + } + } - services - .Setup(s => s.GetService(typeof(IOutputFormattersProvider))) - .Returns(mockFormattersProvider.Object); + var optionsAccessor = new Mock>(); + optionsAccessor.SetupGet(o => o.Options) + .Returns(options); - var options = new Mock>(); - options.SetupGet(o => o.Options) - .Returns(new MvcOptions()); services.Setup(s => s.GetService(typeof(IOptions))) - .Returns(options.Object); + .Returns(optionsAccessor.Object); // This is the ultimate fallback, it will be used if none of the formatters from options // work. diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/MockModelBinderProvider.cs b/test/Microsoft.AspNet.Mvc.Core.Test/MockModelBinderProvider.cs deleted file mode 100644 index 75f223686b..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/MockModelBinderProvider.cs +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc.ModelBinding -{ - public class MockModelBinderProvider : IModelBinderProvider - { - public List ModelBinders { get; set; } = new List(); - - IReadOnlyList IModelBinderProvider.ModelBinders { get { return ModelBinders; } } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/MockModelValidatorProviderProvider.cs b/test/Microsoft.AspNet.Mvc.Core.Test/MockModelValidatorProviderProvider.cs deleted file mode 100644 index bd7877b13b..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/MockModelValidatorProviderProvider.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc.ModelBinding.Validation -{ - public class MockModelValidatorProviderProvider : IModelValidatorProviderProvider - { - public List ModelValidatorProviders { get; } = new List(); - - IReadOnlyList IModelValidatorProviderProvider.ModelValidatorProviders - { - get { return ModelValidatorProviders; } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/MockValueProviderFactoryProvider.cs b/test/Microsoft.AspNet.Mvc.Core.Test/MockValueProviderFactoryProvider.cs deleted file mode 100644 index f287f4078e..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/MockValueProviderFactoryProvider.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc.ModelBinding -{ - public class MockValueProviderFactoryProvider : IValueProviderFactoryProvider - { - public List ValueProviderFactories { get; } = new List(); - - IReadOnlyList IValueProviderFactoryProvider.ValueProviderFactories - { - get { return ValueProviderFactories; } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultModelBindersProviderTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultModelBindersProviderTest.cs deleted file mode 100644 index 1ed61fb367..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultModelBindersProviderTest.cs +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Threading.Tasks; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.OptionsModel; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - public class DefaultModelBindersProviderTest - { - [Fact] - public void DefaultModelBindersProvider_ProvidesInstancesOfModelBinders() - { - // Arrange - var service = Mock.Of(); - var binder = new TypeMatchModelBinder(); - var options = new MvcOptions(); - options.ModelBinders.Add(binder); - options.ModelBinders.Add(typeof(TestModelBinder)); - var optionsAccessor = new Mock>(); - optionsAccessor.SetupGet(o => o.Options) - .Returns(options); - var serviceProvider = new Mock(); - serviceProvider.Setup(p => p.GetService(typeof(ITestService))) - .Returns(service); - var typeActivatorCache = new DefaultTypeActivatorCache(); - - var provider = new DefaultModelBindersProvider(optionsAccessor.Object, - typeActivatorCache, - serviceProvider.Object); - - // Act - var binders = provider.ModelBinders; - - // Assert - Assert.Equal(2, binders.Count); - Assert.Same(binder, binders[0]); - var testModelBinder = Assert.IsType(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 - { - } - } -} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValidationExcludeFiltersProviderTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValidationExcludeFiltersProviderTests.cs deleted file mode 100644 index 0c1e571d49..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValidationExcludeFiltersProviderTests.cs +++ /dev/null @@ -1,152 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.Framework.OptionsModel; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - public class DefaultValidationExcludeFiltersProviderTests - { - [Theory] - [InlineData(typeof(BaseType))] - [InlineData(typeof(DerivedType))] - public void Add_WithType_RegistersTypesAndDerivedType_ToBeExcluded(Type type) - { - // Arrange - var options = new MvcOptions(); - options.ValidationExcludeFilters.Add(type); - var optionsAccessor = new Mock>(); - optionsAccessor.SetupGet(o => o.Options) - .Returns(options); - var typeActivatorCache = new Mock(); - var service = Mock.Of(); - var serviceProvider = new Mock(); - serviceProvider.Setup(p => p.GetService(typeof(ITestService))) - .Returns(service); - var provider = new DefaultValidationExcludeFiltersProvider(optionsAccessor.Object, - typeActivatorCache.Object, - serviceProvider.Object); - - // Act - var filters = provider.ExcludeFilters; - - // Assert - Assert.Equal(1, filters.Count); - Assert.True(filters[0].IsTypeExcluded(type)); - } - - [Theory] - [InlineData(typeof(BaseType))] - [InlineData(typeof(UnRelatedType))] - public void Add_RegisterDerivedType_BaseAndUnrealatedTypesAreNotExcluded(Type type) - { - // Arrange - var options = new MvcOptions(); - options.ValidationExcludeFilters.Add(typeof(DerivedType)); - var optionsAccessor = new Mock>(); - optionsAccessor.SetupGet(o => o.Options) - .Returns(options); - var typeActivatorCache = new Mock(); - var service = Mock.Of(); - var serviceProvider = new Mock(); - serviceProvider.Setup(p => p.GetService(typeof(ITestService))) - .Returns(service); - var provider = new DefaultValidationExcludeFiltersProvider(optionsAccessor.Object, - typeActivatorCache.Object, - serviceProvider.Object); - - // Act - var filters = provider.ExcludeFilters; - - // Assert - Assert.Equal(1, filters.Count); - Assert.False(filters[0].IsTypeExcluded(type)); - } - - [Theory] - [InlineData(typeof(BaseType))] - [InlineData(typeof(DerivedType))] - public void Add_WithTypeName_RegistersTypesAndDerivedType_ToBeExcluded(Type type) - { - // Arrange - var options = new MvcOptions(); - options.ValidationExcludeFilters.Add(type.FullName); - var optionsAccessor = new Mock>(); - optionsAccessor.SetupGet(o => o.Options) - .Returns(options); - var typeActivatorCache = new Mock(); - var service = Mock.Of(); - var serviceProvider = new Mock(); - serviceProvider.Setup(p => p.GetService(typeof(ITestService))) - .Returns(service); - var provider = new DefaultValidationExcludeFiltersProvider(optionsAccessor.Object, - typeActivatorCache.Object, - serviceProvider.Object); - - // Act - var filters = provider.ExcludeFilters; - - // Assert - Assert.Equal(1, filters.Count); - Assert.True(filters[0].IsTypeExcluded(type)); - } - - [Theory] - [InlineData(typeof(BaseType))] - [InlineData(typeof(UnRelatedType))] - [InlineData(typeof(SubNameSpace.UnRelatedType))] - public void Add_WithTypeName_RegisterDerivedType_BaseAndUnrealatedTypesAreNotExcluded(Type type) - { - // Arrange - var options = new MvcOptions(); - options.ValidationExcludeFilters.Add(typeof(DerivedType).FullName); - var optionsAccessor = new Mock>(); - optionsAccessor.SetupGet(o => o.Options) - .Returns(options); - var service = Mock.Of(); - var serviceProvider = new Mock(); - serviceProvider.Setup(p => p.GetService(typeof(ITestService))) - .Returns(service); - var typeActivatorCache = new Mock(); - var provider = new DefaultValidationExcludeFiltersProvider(optionsAccessor.Object, - typeActivatorCache.Object, - serviceProvider.Object); - - // Act - var filters = provider.ExcludeFilters; - - // Assert - Assert.Equal(1, filters.Count); - Assert.False(filters[0].IsTypeExcluded(type)); - } - - private class BaseType - { - } - - private class DerivedType : BaseType - { - } - - private class UnRelatedType - { - } - } - - namespace SubNameSpace - { - public class UnRelatedType - { - } - } - - public interface ITestService - { - } -} - - diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValidationProviderProviderTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValidationProviderProviderTest.cs deleted file mode 100644 index c8c8f021ce..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValidationProviderProviderTest.cs +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; -using Microsoft.Framework.OptionsModel; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - public class DefaultValidationProviderProviderTest - { - [Fact] - public void ValidationProviders_ReturnsInstantiatedListOfValueProviders() - { - // Arrange - var service = Mock.Of(); - var validationProvider = Mock.Of(); - var type = typeof(TestModelValidationProvider); - var serviceProvider = new Mock(); - serviceProvider.Setup(p => p.GetService(typeof(ITestService))) - .Returns(service); - var typeActivatorCache = new DefaultTypeActivatorCache(); - var options = new MvcOptions(); - options.ModelValidatorProviders.Add(type); - options.ModelValidatorProviders.Add(validationProvider); - var accessor = new Mock>(); - accessor.SetupGet(a => a.Options) - .Returns(options); - var provider = new DefaultModelValidatorProviderProvider(accessor.Object, - typeActivatorCache, - serviceProvider.Object); - - // Act - var result = provider.ModelValidatorProviders; - - // Assert - Assert.Equal(2, result.Count); - var testModelValidationProvider = Assert.IsType(result[0]); - Assert.Same(service, testModelValidationProvider.Service); - Assert.Same(validationProvider, result[1]); - } - - private class TestModelValidationProvider : IModelValidatorProvider - { - public TestModelValidationProvider(ITestService service) - { - Service = service; - } - - public ITestService Service { get; private set; } - - public void GetValidators(ModelValidatorProviderContext context) - { - } - } - - public interface ITestService - { - } - } -} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValueProviderFactoryProviderTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValueProviderFactoryProviderTest.cs deleted file mode 100644 index f5d0f76b53..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultValueProviderFactoryProviderTest.cs +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.Framework.OptionsModel; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - public class DefaultValueProviderFactoryProviderTest - { - [Fact] - public void ViewEngine_ReturnsInstantiatedListOfViewEngines() - { - // Arrange - var service = Mock.Of(); - var valueProviderFactory = Mock.Of(); - var type = typeof(TestValueProviderFactory); - var serviceProvider = new Mock(); - serviceProvider.Setup(p => p.GetService(typeof(ITestService))) - .Returns(service); - var typeActivatorCache = new DefaultTypeActivatorCache(); - var options = new MvcOptions(); - options.ValueProviderFactories.Add(valueProviderFactory); - options.ValueProviderFactories.Add(type); - var accessor = new Mock>(); - accessor.SetupGet(a => a.Options) - .Returns(options); - var provider = new DefaultValueProviderFactoryProvider(accessor.Object, - typeActivatorCache, - serviceProvider.Object); - - // Act - var result = provider.ValueProviderFactories; - - // Assert - Assert.Equal(2, result.Count); - Assert.Same(valueProviderFactory, result[0]); - var testValueProviderFactory = Assert.IsType(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/OptionDescriptors/DefaultViewEngineProviderTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultViewEngineProviderTest.cs deleted file mode 100644 index f77c05f683..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/DefaultViewEngineProviderTest.cs +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.Rendering; -using Microsoft.Framework.OptionsModel; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - public class DefaultViewEngineProviderTest - { - [Fact] - public void ViewEngine_ReturnsInstantiatedListOfViewEngines() - { - // Arrange - var service = Mock.Of(); - var viewEngine = Mock.Of(); - var type = typeof(TestViewEngine); - var serviceProvider = new Mock(); - serviceProvider.Setup(p => p.GetService(typeof(ITestService))) - .Returns(service); - var typeActivatorCache = new DefaultTypeActivatorCache(); - var options = new MvcOptions(); - options.ViewEngines.Add(viewEngine); - options.ViewEngines.Add(type); - var accessor = new Mock>(); - accessor.SetupGet(a => a.Options) - .Returns(options); - var provider = new DefaultViewEngineProvider(accessor.Object, typeActivatorCache, serviceProvider.Object); - - // Act - var result = provider.ViewEngines; - - // Assert - Assert.Equal(2, result.Count); - Assert.Same(viewEngine, result[0]); - var testViewEngine = Assert.IsType(result[1]); - Assert.Same(service, testViewEngine.Service); - } - - private class TestViewEngine : IViewEngine - { - public TestViewEngine(ITestService service) - { - Service = service; - } - - public ITestService Service { get; private set; } - - public ViewEngineResult FindPartialView(ActionContext context, string partialViewName) - { - throw new NotImplementedException(); - } - - public ViewEngineResult FindView(ActionContext context, string viewName) - { - throw new NotImplementedException(); - } - } - - public interface ITestService - { - } - } -} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ExcludeValidationDescriptorTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ExcludeValidationDescriptorTests.cs deleted file mode 100644 index aa7bd538ea..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ExcludeValidationDescriptorTests.cs +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; -using Microsoft.AspNet.Testing; -using Microsoft.Framework.Internal; -using Xunit; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - public class ExcludeValidationDescriptorTests - { - [Fact] - public void ConstructorThrows_IfTypeIsNotIExcludeTypeFromBodyValidation() - { - // Arrange - var expected = "The type 'System.String' must derive from " + - "'Microsoft.AspNet.Mvc.ModelBinding.Validation.IExcludeTypeValidationFilter'."; - var type = typeof(string); - - // Act & Assert - ExceptionAssert.ThrowsArgument(() => new ExcludeValidationDescriptor(type), "type", expected); - } - - [Fact] - public void ConstructorSetsOptionType() - { - // Arrange - var type = typeof(TestExcludeFilter); - - // Act - var descriptor = new ExcludeValidationDescriptor(type); - - // Assert - Assert.Equal(type, descriptor.OptionType); - Assert.Null(descriptor.Instance); - } - - [Fact] - public void ConstructorSetsInstanceeAndOptionType() - { - // Arrange - var instance = new TestExcludeFilter(); - - // Act - var descriptor = new ExcludeValidationDescriptor(instance); - - // Assert - Assert.Same(instance, descriptor.Instance); - Assert.Equal(instance.GetType(), descriptor.OptionType); - } - - private class TestExcludeFilter : IExcludeTypeValidationFilter - { - public bool IsTypeExcluded([NotNull] Type propertyType) - { - throw new NotImplementedException(); - } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/InputFormatterDescriptorExtensionTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/InputFormatterDescriptorExtensionTest.cs deleted file mode 100644 index 4ad8286832..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/InputFormatterDescriptorExtensionTest.cs +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc.Core.Test -{ - public class InputFormatterDescriptorExtensionTest - { - [Theory] - [InlineData(-1)] - [InlineData(5)] - public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - new InputFormatterDescriptor(Mock.Of()), - new InputFormatterDescriptor(Mock.Of()) - }; - - // Act & Assert - Assert.Throws("index", - () => collection.Insert(index, typeof(IInputFormatter))); - } - - [Theory] - [InlineData(-2)] - [InlineData(3)] - public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - new InputFormatterDescriptor(Mock.Of()), - new InputFormatterDescriptor(Mock.Of()) - }; - var formatter = Mock.Of(); - - // Act & Assert - Assert.Throws("index", () => collection.Insert(index, formatter)); - } - - [InlineData] - public void InputFormatterDescriptors_AddsTypesAndInstances() - { - // Arrange - var formatter1 = Mock.Of(); - var formatter2 = Mock.Of(); - var type1 = typeof(JsonInputFormatter); - var type2 = typeof(IInputFormatter); - var collection = new List(); - - // Act - collection.Add(formatter1); - collection.Insert(1, formatter2); - collection.Add(type1); - collection.Insert(2, type2); - - // Assert - Assert.Equal(4, collection.Count); - Assert.Equal(formatter1, collection[0].Instance); - Assert.Equal(formatter2, collection[1].Instance); - Assert.Equal(type2, collection[2].OptionType); - Assert.Equal(type1, collection[3].OptionType); - } - } -} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/InputFormatterDescriptorTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/InputFormatterDescriptorTest.cs deleted file mode 100644 index 212589bd31..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/InputFormatterDescriptorTest.cs +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Threading.Tasks; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.AspNet.Testing; -using Xunit; - -namespace Microsoft.AspNet.Mvc -{ - public class InputFormatterDescriptorTest - { - [Fact] - public void ConstructorThrows_IfTypeIsNotInputFormatter() - { - // Arrange - var expected = "The type 'System.String' must derive from " + - "'Microsoft.AspNet.Mvc.IInputFormatter'."; - - var type = typeof(string); - - // Act & Assert - ExceptionAssert.ThrowsArgument(() => new InputFormatterDescriptor(type), "type", expected); - } - - [Fact] - public void ConstructorSets_InputFormatterType() - { - // Arrange - var type = typeof(TestInputFormatter); - - // Act - var descriptor = new InputFormatterDescriptor(type); - - // Assert - Assert.Equal(type, descriptor.OptionType); - Assert.Null(descriptor.Instance); - } - - [Fact] - public void ConstructorSets_InputFormatterInstanceAndType() - { - // Arrange - var testFormatter = new TestInputFormatter(); - - // Act - var descriptor = new InputFormatterDescriptor(testFormatter); - - // Assert - Assert.Same(testFormatter, descriptor.Instance); - Assert.Equal(testFormatter.GetType(), descriptor.OptionType); - } - - private class TestInputFormatter : IInputFormatter - { - public bool CanRead(InputFormatterContext context) - { - throw new NotImplementedException(); - } - - public Task ReadAsync(InputFormatterContext context) - { - throw new NotImplementedException(); - } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorExtensionsTest.cs deleted file mode 100644 index 83a13749c4..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorExtensionsTest.cs +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Linq; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc -{ - public class ModelBinderDescriptorExtensionsTest - { - [Theory] - [InlineData(-1)] - [InlineData(5)] - public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - new ModelBinderDescriptor(Mock.Of()), - new ModelBinderDescriptor(Mock.Of()) - }; - - // Act & Assert - Assert.Throws("index", () => collection.Insert(index, typeof(IModelBinder))); - } - - [Theory] - [InlineData(-2)] - [InlineData(3)] - public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - new ModelBinderDescriptor(Mock.Of()), - new ModelBinderDescriptor(Mock.Of()) - }; - var binder = Mock.Of(); - - // Act & Assert - Assert.Throws("index", () => collection.Insert(index, binder)); - } - - [InlineData] - public void ModelBinderDescriptors_AddsTypesAndInstances() - { - // Arrange - var binder1 = Mock.Of(); - var binder2 = Mock.Of(); - var type1 = typeof(TypeMatchModelBinder); - var type2 = typeof(TypeConverterModelBinder); - var collection = new List(); - - // Act - collection.Add(binder1); - collection.Insert(1, binder2); - collection.Add(type1); - collection.Insert(2, type2); - - // Assert - Assert.Equal(4, collection.Count); - Assert.Equal(binder1, collection[0].Instance); - Assert.Equal(binder2, collection[1].Instance); - Assert.Equal(type2, collection[2].OptionType); - Assert.Equal(type1, collection[3].OptionType); - } - - [Fact] - public void ModelBinders_RemoveTypesOf_RemovesDescriptorsOfIModelBinder() - { - // Arrange - var modelBinders = new MvcOptions().ModelBinders; - modelBinders.Add(new ByteArrayModelBinder()); - modelBinders.Add(Mock.Of()); - modelBinders.Add(typeof(ByteArrayModelBinder)); - modelBinders.Add(Mock.Of()); - - // Act - modelBinders.RemoveTypesOf(); - - // Assert - Assert.DoesNotContain(modelBinders, descriptor => descriptor.OptionType == typeof(ByteArrayModelBinder)); - } - } -} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorTest.cs deleted file mode 100644 index a016f41503..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelBinderDescriptorTest.cs +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Threading.Tasks; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Testing; -using Xunit; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - public class ModelBinderDescriptorTest - { - [Fact] - public void ConstructorThrows_IfTypeIsNotIModelBinder() - { - // Arrange - var expected = "The type 'System.String' must derive from " + - "'Microsoft.AspNet.Mvc.ModelBinding.IModelBinder'."; - var type = typeof(string); - - // Act & Assert - ExceptionAssert.ThrowsArgument(() => new ModelBinderDescriptor(type), "type", expected); - } - - [Fact] - public void ConstructorSetsOptionType() - { - // Arrange - var type = typeof(TestModelBinder); - - // Act - var descriptor = new ModelBinderDescriptor(type); - - // Assert - Assert.Equal(type, descriptor.OptionType); - Assert.Null(descriptor.Instance); - } - - [Fact] - public void ConstructorSetsInstanceeAndOptionType() - { - // Arrange - var viewEngine = new TestModelBinder(); - - // Act - var descriptor = new ModelBinderDescriptor(viewEngine); - - // Assert - Assert.Same(viewEngine, descriptor.Instance); - Assert.Equal(viewEngine.GetType(), descriptor.OptionType); - } - - private class TestModelBinder : IModelBinder - { - public Task BindModelAsync(ModelBindingContext bindingContext) - { - throw new NotImplementedException(); - } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelValidatorProviderDescriptor.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelValidatorProviderDescriptor.cs deleted file mode 100644 index ad335b9247..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelValidatorProviderDescriptor.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; -using Microsoft.AspNet.Testing; -using Xunit; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - public class ModelValidatorProviderDescriptorTest - { - [Fact] - public void ConstructorThrows_IfTypeIsNotModelValidatorProvider() - { - // Arrange - var validatorProviderType = typeof(IModelValidatorProvider).FullName; - var type = typeof(string); - var expected = string.Format("The type '{0}' must derive from '{1}'.", - type.FullName, validatorProviderType); - - // Act & Assert - ExceptionAssert.ThrowsArgument(() => new ModelValidatorProviderDescriptor(type), "type", expected); - } - - [Fact] - public void ConstructorSetsModelValidatorProviderType() - { - // Arrange - var type = typeof(TestModelValidatorProvider); - - // Act - var descriptor = new ModelValidatorProviderDescriptor(type); - - // Assert - Assert.Equal(type, descriptor.OptionType); - Assert.Null(descriptor.Instance); - } - - [Fact] - public void ConstructorSetsInstanceAndType() - { - // Arrange - var validatorProvider = new TestModelValidatorProvider(); - - // Act - var descriptor = new ModelValidatorProviderDescriptor(validatorProvider); - - // Assert - Assert.Same(validatorProvider, descriptor.Instance); - Assert.Equal(validatorProvider.GetType(), descriptor.OptionType); - } - - private class TestModelValidatorProvider : IModelValidatorProvider - { - public void GetValidators(ModelValidatorProviderContext context) - { - } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelValidatorProviderDescriptorExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelValidatorProviderDescriptorExtensionsTest.cs deleted file mode 100644 index 882116f31b..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ModelValidatorProviderDescriptorExtensionsTest.cs +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Mvc.ModelBinding.Validation; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc -{ - public class ModelValidatorProviderDescriptorExtensionsTest - { - [Theory] - [InlineData(-1)] - [InlineData(5)] - public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - new ModelValidatorProviderDescriptor(Mock.Of()), - new ModelValidatorProviderDescriptor(Mock.Of()) - }; - - // Act & Assert - Assert.Throws("index", - () => collection.Insert(index, typeof(IModelValidatorProvider))); - } - - [Theory] - [InlineData(-2)] - [InlineData(3)] - public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - new ModelValidatorProviderDescriptor(Mock.Of()), - new ModelValidatorProviderDescriptor(Mock.Of()) - }; - var valueProviderFactory = Mock.Of(); - - // Act & Assert - Assert.Throws("index", () => collection.Insert(index, valueProviderFactory)); - } - - [InlineData] - public void ModelValidatorProviderDescriptors_AddsTypesAndInstances() - { - // Arrange - var provider1 = Mock.Of(); - var provider2 = Mock.Of(); - var type1 = typeof(DefaultModelValidatorProvider); - var type2 = typeof(DataAnnotationsModelValidatorProvider); - var collection = new List(); - - // Act - collection.Add(provider2); - collection.Insert(0, provider1); - collection.Add(type2); - collection.Insert(2, type1); - - // Assert - Assert.Equal(4, collection.Count); - Assert.Same(provider1, collection[0].Instance); - Assert.Same(provider2, collection[1].Instance); - Assert.IsType(collection[2].OptionType); - Assert.IsType(collection[3].OptionType); - } - } -} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OptionDescriptorExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OptionDescriptorExtensionsTest.cs index 07e02dfb9a..5c4f849b3a 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OptionDescriptorExtensionsTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OptionDescriptorExtensionsTest.cs @@ -11,266 +11,6 @@ namespace Microsoft.AspNet.Mvc.OptionDescriptors { public class OptionDescriptorExtensionsTest { - [Fact] - public void InputFormatters_InstanceOf_ThrowsInvalidOperationExceptionIfMoreThanOnceInstance() - { - // Arrange - var formatters = new MvcOptions().InputFormatters; - formatters.Add(new JsonInputFormatter()); - formatters.Add(Mock.Of()); - formatters.Add(new JsonInputFormatter()); - - // Act & Assert - Assert.Throws(() => formatters.InstanceOf()); - } - - [Fact] - public void InputFormatters_InstanceOf_ThrowsInvalidOperationExceptionIfNoInstance() - { - // Arrange - var formatters = new MvcOptions().InputFormatters; - formatters.Add(Mock.Of()); - formatters.Add(typeof(JsonInputFormatter)); - - // Act & Assert - Assert.Throws(() => formatters.InstanceOf()); - } - - [Fact] - public void InputFormatters_InstanceOf_ReturnsInstanceOfIInputFormatterIfOneExists() - { - // Arrange - var formatters = new MvcOptions().InputFormatters; - formatters.Add(Mock.Of()); - var jsonFormatter = new JsonInputFormatter(); - formatters.Add(jsonFormatter); - formatters.Add(typeof(JsonInputFormatter)); - - // Act - var formatter = formatters.InstanceOf(); - - // Assert - Assert.NotNull(formatter); - Assert.IsType(formatter); - Assert.Same(jsonFormatter, formatter); - } - - [Fact] - public void InputFormatters_InstanceOfOrDefault_ThrowsInvalidOperationExceptionIfMoreThanOnceInstance() - { - // Arrange - var formatters = new MvcOptions().InputFormatters; - formatters.Add(new JsonInputFormatter()); - formatters.Add(Mock.Of()); - formatters.Add(new JsonInputFormatter()); - - // Act & Assert - Assert.Throws(() => formatters.InstanceOfOrDefault()); - } - - [Fact] - public void InputFormatters_InstanceOfOrDefault_ReturnsNullIfNoInstance() - { - // Arrange - var formatters = new MvcOptions().InputFormatters; - formatters.Add(Mock.Of()); - formatters.Add(typeof(JsonInputFormatter)); - - // Act - var formatter = formatters.InstanceOfOrDefault(); - - // Assert - Assert.Null(formatter); - } - - [Fact] - public void InputFormatters_InstanceOfOrDefault_ReturnsInstanceOfIInputFormatterIfOneExists() - { - // Arrange - var formatters = new MvcOptions().InputFormatters; - formatters.Add(Mock.Of()); - formatters.Add(typeof(JsonInputFormatter)); - var jsonFormatter = new JsonInputFormatter(); - formatters.Add(jsonFormatter); - - // Act - var formatter = formatters.InstanceOfOrDefault(); - - // Assert - Assert.NotNull(formatter); - Assert.IsType(formatter); - Assert.Same(jsonFormatter, formatter); - } - - [Fact] - public void InputFormatters_InstancesOf_ReturnsEmptyCollectionIfNoneExist() - { - // Arrange - var formatters = new MvcOptions().InputFormatters; - formatters.Add(Mock.Of()); - formatters.Add(typeof(JsonInputFormatter)); - - // Act - var jsonFormatters = formatters.InstancesOf(); - - // Assert - Assert.Empty(jsonFormatters); - } - - [Fact] - public void InputFormatters_InstancesOf_ReturnsNonEmptyCollectionIfSomeExist() - { - // Arrange - var formatters = new MvcOptions().InputFormatters; - formatters.Add(typeof(JsonInputFormatter)); - var formatter1 = new JsonInputFormatter(); - var formatter2 = Mock.Of(); - var formatter3 = new JsonInputFormatter(); - var formatter4 = Mock.Of(); - formatters.Add(formatter1); - formatters.Add(formatter2); - formatters.Add(formatter3); - formatters.Add(formatter4); - - var expectedFormatters = new List { formatter1, formatter3 }; - - // Act - var jsonFormatters = formatters.InstancesOf().ToList(); - - // Assert - Assert.NotEmpty(jsonFormatters); - Assert.Equal(jsonFormatters, expectedFormatters); - } - - [Fact] - public void OutputFormatters_InstanceOf_ThrowsInvalidOperationExceptionIfMoreThanOnceInstance() - { - // Arrange - var formatters = new MvcOptions().OutputFormatters; - formatters.Add(new JsonOutputFormatter()); - formatters.Add(Mock.Of()); - formatters.Add(new JsonOutputFormatter()); - - // Act & Assert - Assert.Throws(() => formatters.InstanceOf()); - } - - [Fact] - public void OutputFormatters_InstanceOf_ThrowsInvalidOperationExceptionIfNoInstance() - { - // Arrange - var formatters = new MvcOptions().OutputFormatters; - formatters.Add(Mock.Of()); - formatters.Add(typeof(JsonOutputFormatter)); - - // Act & Assert - Assert.Throws(() => formatters.InstanceOf()); - } - - [Fact] - public void OutputFormatters_InstanceOf_ReturnsInstanceOfIInputFormatterIfOneExists() - { - // Arrange - var formatters = new MvcOptions().OutputFormatters; - formatters.Add(Mock.Of()); - var jsonFormatter = new JsonOutputFormatter(); - formatters.Add(jsonFormatter); - formatters.Add(typeof(JsonOutputFormatter)); - - // Act - var formatter = formatters.InstanceOf(); - - // Assert - Assert.NotNull(formatter); - Assert.IsType(formatter); - Assert.Same(jsonFormatter, formatter); - } - - [Fact] - public void OutputFormatters_InstanceOfOrDefault_ThrowsInvalidOperationExceptionIfMoreThanOnceInstance() - { - // Arrange - var formatters = new MvcOptions().OutputFormatters; - formatters.Add(new JsonOutputFormatter()); - formatters.Add(Mock.Of()); - formatters.Add(new JsonOutputFormatter()); - - // Act & Assert - Assert.Throws(() => formatters.InstanceOfOrDefault()); - } - - [Fact] - public void OutputFormatters_InstanceOfOrDefault_ReturnsNullIfNoInstance() - { - // Arrange - var formatters = new MvcOptions().OutputFormatters; - formatters.Add(Mock.Of()); - formatters.Add(typeof(JsonOutputFormatter)); - - // Act - var formatter = formatters.InstanceOfOrDefault(); - - // Assert - Assert.Null(formatter); - } - - [Fact] - public void OutputFormatters_InstanceOfOrDefault_ReturnsInstanceOfIOutputFormatterIfOneExists() - { - // Arrange - var formatters = new MvcOptions().OutputFormatters; - formatters.Add(Mock.Of()); - formatters.Add(typeof(JsonOutputFormatter)); - var jsonFormatter = new JsonOutputFormatter(); - formatters.Add(jsonFormatter); - - // Act - var formatter = formatters.InstanceOfOrDefault(); - - // Assert - Assert.NotNull(formatter); - Assert.IsType(formatter); - Assert.Same(jsonFormatter, formatter); - } - - [Fact] - public void OutputFormatters_InstancesOf_ReturnsEmptyCollectionIfNoneExist() - { - // Arrange - var formatters = new MvcOptions().OutputFormatters; - formatters.Add(Mock.Of()); - formatters.Add(typeof(JsonOutputFormatter)); - - // Act - var jsonFormatters = formatters.InstancesOf(); - - // Assert - Assert.Empty(jsonFormatters); - } - - [Fact] - public void OutputFormatters_InstancesOf_ReturnsNonEmptyCollectionIfSomeExist() - { - // Arrange - var formatters = new MvcOptions().OutputFormatters; - formatters.Add(typeof(JsonOutputFormatter)); - var formatter1 = new JsonOutputFormatter(); - var formatter2 = Mock.Of(); - var formatter3 = new JsonOutputFormatter(); - var formatter4 = Mock.Of(); - formatters.Add(formatter1); - formatters.Add(formatter2); - formatters.Add(formatter3); - formatters.Add(formatter4); - - var expectedFormatters = new List { formatter1, formatter3 }; - - // Act - var jsonFormatters = formatters.InstancesOf().ToList(); - - // Assert - Assert.NotEmpty(jsonFormatters); - Assert.Equal(jsonFormatters, expectedFormatters); - } + } } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorExtensionTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorExtensionTest.cs deleted file mode 100644 index 6b6bfc9320..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorExtensionTest.cs +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - - -using System; -using System.Collections.Generic; -using System.Linq; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc.Core.Test -{ - public class OutputFormatterDescriptorExtensionTest - { - [Theory] - [InlineData(-1)] - [InlineData(5)] - public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - new OutputFormatterDescriptor(Mock.Of()), - new OutputFormatterDescriptor(Mock.Of()) - }; - - // Act & Assert - Assert.Throws("index", - () => collection.Insert(index, typeof(IOutputFormatter))); - } - - [Theory] - [InlineData(-2)] - [InlineData(3)] - public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - new OutputFormatterDescriptor(Mock.Of()), - new OutputFormatterDescriptor(Mock.Of()) - }; - var formatter = Mock.Of(); - - // Act & Assert - Assert.Throws("index", () => collection.Insert(index, formatter)); - } - - [InlineData] - public void OutputFormatterDescriptors_AddsTypesAndInstances() - { - // Arrange - var formatter1 = Mock.Of(); - var formatter2 = Mock.Of(); - var type1 = typeof(JsonOutputFormatter); - var type2 = typeof(OutputFormatter); - var collection = new List(); - - // Act - collection.Add(formatter1); - collection.Insert(1, formatter2); - collection.Add(type1); - collection.Insert(2, type2); - - // Assert - Assert.Equal(4, collection.Count); - Assert.Equal(formatter1, collection[0].Instance); - Assert.Equal(formatter2, collection[1].Instance); - Assert.Equal(type2, collection[2].OptionType); - Assert.Equal(type1, collection[3].OptionType); - } - - [Fact] - public void OutputFormatters_RemoveTypesOf_RemovesDescriptorsOfIOutputFormatter() - { - // Arrange - var formatters = new MvcOptions().OutputFormatters; - formatters.Add(new JsonOutputFormatter()); - formatters.Add(Mock.Of()); - formatters.Add(typeof(JsonOutputFormatter)); - formatters.Add(Mock.Of()); - - // Act - formatters.RemoveTypesOf(); - - // Assert - Assert.DoesNotContain(formatters, descriptor => descriptor.OptionType == typeof(JsonOutputFormatter)); - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorTest.cs deleted file mode 100644 index 2f45a80f55..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/OutputFormatterDescriptorTest.cs +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Microsoft.Net.Http.Headers; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.AspNet.Testing; -using Xunit; - -namespace Microsoft.AspNet.Mvc.Core -{ - public class OutputFormatterDescriptorTest - { - [Fact] - public void ConstructorThrows_IfTypeIsNotOutputFormatter() - { - // Arrange - var expected = "The type 'System.String' must derive from " + - "'Microsoft.AspNet.Mvc.IOutputFormatter'."; - - var type = typeof(string); - - // Act & Assert - ExceptionAssert.ThrowsArgument(() => new OutputFormatterDescriptor(type), "type", expected); - } - - [Fact] - public void ConstructorSets_OutputFormatterType() - { - // Arrange - var type = typeof(TestOutputFormatter); - - // Act - var descriptor = new OutputFormatterDescriptor(type); - - // Assert - Assert.Equal(type, descriptor.OptionType); - Assert.Null(descriptor.Instance); - } - - [Fact] - public void ConstructorSets_OutputFormatterInsnaceAndType() - { - // Arrange - var testFormatter = new TestOutputFormatter(); - - // Act - var descriptor = new OutputFormatterDescriptor(testFormatter); - - // Assert - Assert.Same(testFormatter, descriptor.Instance); - Assert.Equal(testFormatter.GetType(), descriptor.OptionType); - } - - private class TestOutputFormatter : IOutputFormatter - { - public bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType) - { - throw new NotImplementedException(); - } - - public Task WriteAsync(OutputFormatterContext context) - { - throw new NotImplementedException(); - } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValidationExcludeFiltersExtensionsTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValidationExcludeFiltersExtensionsTests.cs deleted file mode 100644 index 65b9fe3ee9..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValidationExcludeFiltersExtensionsTests.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Xunit; - -namespace Microsoft.AspNet.Mvc -{ - public class ValidationExcludeFiltersExtensionsTests - { - [Fact] - public void InputFormatterDescriptors_AddsTypesAndTypeNames() - { - // Arrange - var type1 = typeof(BaseType); - var collection = new List(); - - // Act - collection.Add(type1); - collection.Add(type1.FullName); - - // Assert - Assert.Equal(2, collection.Count); - Assert.Equal(typeof(DefaultTypeBasedExcludeFilter), collection[0].OptionType); - Assert.Equal(typeof(DefaultTypeNameBasedExcludeFilter), collection[1].OptionType); - } - - private class BaseType - { - } - } -} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValueProviderFactoryDescriptorExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValueProviderFactoryDescriptorExtensionsTest.cs deleted file mode 100644 index 731a3b869d..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValueProviderFactoryDescriptorExtensionsTest.cs +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Linq; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc -{ - public class ValueProviderFactoryDescriptorExtensionsTest - { - [Theory] - [InlineData(-1)] - [InlineData(5)] - public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - 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); - } - - [Fact] - public void ValueProviderFactories_RemoveTypesOf_RemovesDescriptorsOfIValueProviderFactory() - { - // Arrange - var factories = new MvcOptions().ValueProviderFactories; - factories.Add(new FormValueProviderFactory()); - factories.Add(Mock.Of()); - factories.Add(typeof(FormValueProviderFactory)); - factories.Add(Mock.Of()); - - // Act - factories.RemoveTypesOf(); - - // Assert - Assert.DoesNotContain(factories, descriptor => descriptor.OptionType == typeof(FormValueProviderFactory)); - } - - 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 deleted file mode 100644 index 4c86802a0f..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ValueProviderFactoryDescriptorTest.cs +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.AspNet.Testing; -using Xunit; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - public class ValueProviderFactoryDescriptorTest - { - [Fact] - public void ConstructorThrows_IfTypeIsNotViewEngine() - { - // Arrange - var viewEngineType = typeof(IValueProviderFactory).FullName; - var type = typeof(string); - var expected = string.Format("The type '{0}' must derive from '{1}'.", - type.FullName, viewEngineType); - - // Act & Assert - ExceptionAssert.ThrowsArgument(() => new ValueProviderFactoryDescriptor(type), "type", expected); - } - - [Fact] - public void ConstructorSetsViewEngineType() - { - // Arrange - var type = typeof(TestValueProviderFactory); - - // Act - var descriptor = new ValueProviderFactoryDescriptor(type); - - // Assert - Assert.Equal(type, descriptor.OptionType); - Assert.Null(descriptor.Instance); - } - - [Fact] - public void ConstructorSetsViewEngineAndViewEngineType() - { - // Arrange - var viewEngine = new TestValueProviderFactory(); - - // Act - var descriptor = new ValueProviderFactoryDescriptor(viewEngine); - - // Assert - Assert.Same(viewEngine, descriptor.Instance); - Assert.Equal(viewEngine.GetType(), descriptor.OptionType); - } - - private class TestValueProviderFactory : IValueProviderFactory - { - public IValueProvider GetValueProvider(ValueProviderFactoryContext context) - { - throw new NotImplementedException(); - } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDescriptorExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDescriptorExtensionsTest.cs index 79d8263d81..17462c5d29 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDescriptorExtensionsTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDescriptorExtensionsTest.cs @@ -3,7 +3,7 @@ using System; using System.Collections.Generic; -using Microsoft.AspNet.Mvc.OptionDescriptors; +using System.Linq; using Microsoft.AspNet.Mvc.Rendering; using Moq; using Xunit; @@ -59,8 +59,139 @@ public void ViewEngineDescriptors_AddsTypesAndInstances() // Assert Assert.Equal(2, collection.Count); - Assert.IsType(collection[0].Instance); - Assert.Same(viewEngine, collection[0].Instance); + Assert.IsType(collection[0].ViewEngine); + Assert.Same(viewEngine, collection[0].ViewEngineType); + } + + [Fact] + public void InputviewEngines_InstanceOf_ThrowsInvalidOperationExceptionIfMoreThanOnceInstance() + { + // Arrange + var viewEngines = new MvcOptions().ViewEngines; + viewEngines.Add(new TestViewEngine()); + viewEngines.Add(Mock.Of()); + viewEngines.Add(new TestViewEngine()); + + // Act & Assert + Assert.Throws(() => viewEngines.InstanceOf()); + } + + [Fact] + public void InputviewEngines_InstanceOf_ThrowsInvalidOperationExceptionIfNoInstance() + { + // Arrange + var viewEngines = new MvcOptions().ViewEngines; + viewEngines.Add(Mock.Of()); + viewEngines.Add(typeof(TestViewEngine)); + + // Act & Assert + Assert.Throws(() => viewEngines.InstanceOf()); + } + + [Fact] + public void InputviewEngines_InstanceOf_ReturnsInstanceOfIInputFormatterIfOneExists() + { + // Arrange + var viewEngines = new MvcOptions().ViewEngines; + viewEngines.Add(Mock.Of()); + var testEngine = new TestViewEngine(); + viewEngines.Add(testEngine); + viewEngines.Add(typeof(TestViewEngine)); + + // Act + var formatter = viewEngines.InstanceOf(); + + // Assert + Assert.NotNull(formatter); + Assert.IsType(formatter); + Assert.Same(testEngine, formatter); + } + + [Fact] + public void InputviewEngines_InstanceOfOrDefault_ThrowsInvalidOperationExceptionIfMoreThanOnceInstance() + { + // Arrange + var viewEngines = new MvcOptions().ViewEngines; + viewEngines.Add(new TestViewEngine()); + viewEngines.Add(Mock.Of()); + viewEngines.Add(new TestViewEngine()); + + // Act & Assert + Assert.Throws(() => viewEngines.InstanceOfOrDefault()); + } + + [Fact] + public void InputviewEngines_InstanceOfOrDefault_ReturnsNullIfNoInstance() + { + // Arrange + var viewEngines = new MvcOptions().ViewEngines; + viewEngines.Add(Mock.Of()); + viewEngines.Add(typeof(TestViewEngine)); + + // Act + var formatter = viewEngines.InstanceOfOrDefault(); + + // Assert + Assert.Null(formatter); + } + + [Fact] + public void InputviewEngines_InstanceOfOrDefault_ReturnsInstanceOfIInputFormatterIfOneExists() + { + // Arrange + var viewEngines = new MvcOptions().ViewEngines; + viewEngines.Add(Mock.Of()); + viewEngines.Add(typeof(TestViewEngine)); + var testEngine = new TestViewEngine(); + viewEngines.Add(testEngine); + + // Act + var formatter = viewEngines.InstanceOfOrDefault(); + + // Assert + Assert.NotNull(formatter); + Assert.IsType(formatter); + Assert.Same(testEngine, formatter); + } + + [Fact] + public void InputviewEngines_InstancesOf_ReturnsEmptyCollectionIfNoneExist() + { + // Arrange + var viewEngines = new MvcOptions().ViewEngines; + viewEngines.Add(Mock.Of()); + viewEngines.Add(typeof(TestViewEngine)); + + // Act + var result = viewEngines.InstancesOf(); + + // Assert + Assert.Empty(result); + } + + [Fact] + public void InputviewEngines_InstancesOf_ReturnsNonEmptyCollectionIfSomeExist() + { + // Arrange + var viewEngines = new MvcOptions().ViewEngines; + viewEngines.Add(typeof(TestViewEngine)); + var viewEngine1 = new TestViewEngine(); + var viewEngine2 = Mock.Of(); + var viewEngine3 = new TestViewEngine(); + var viewEngine4 = Mock.Of(); + viewEngines.Add(viewEngine1); + viewEngines.Add(viewEngine2); + viewEngines.Add(viewEngine3); + viewEngines.Add(viewEngine4); + + var expectedviewEngines = new List { viewEngine1, viewEngine3 }; + + // Act + var result = viewEngines.InstancesOf().ToList(); + + // Assert + Assert.NotEmpty(result); + Assert.Equal(result, expectedviewEngines); } private class TestViewEngine : IViewEngine diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDescriptorTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDescriptorTest.cs index 1582a7e43d..5b8f001cf9 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDescriptorTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/OptionDescriptors/ViewEngineDescriptorTest.cs @@ -33,8 +33,8 @@ public void ConstructorSetsViewEngineType() var descriptor = new ViewEngineDescriptor(type); // Assert - Assert.Equal(type, descriptor.OptionType); - Assert.Null(descriptor.Instance); + Assert.Equal(type, descriptor.ViewEngineType); + Assert.Null(descriptor.ViewEngine); } [Fact] @@ -47,8 +47,8 @@ public void ConstructorSetsViewEngineAndViewEngineType() var descriptor = new ViewEngineDescriptor(viewEngine); // Assert - Assert.Same(viewEngine, descriptor.Instance); - Assert.Equal(viewEngine.GetType(), descriptor.OptionType); + Assert.Same(viewEngine, descriptor.ViewEngine); + Assert.Equal(viewEngine.GetType(), descriptor.ViewEngineType); } private class TestViewEngine : IViewEngine diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ParameterBinding/ModelBindingHelperTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ParameterBinding/ModelBindingHelperTest.cs index d09711de9b..896d446c70 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ParameterBinding/ModelBindingHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ParameterBinding/ModelBindingHelperTest.cs @@ -38,7 +38,7 @@ public async Task TryUpdateModel_ReturnsFalse_IfBinderReturnsFalse() metadataProvider, GetCompositeBinder(binder.Object), Mock.Of(), - new DefaultObjectValidator(Mock.Of(), metadataProvider), + new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider), Mock.Of()); // Assert @@ -78,7 +78,7 @@ public async Task TryUpdateModel_ReturnsFalse_IfModelValidationFails() modelMetadataProvider, GetCompositeBinder(binders), valueProvider, - new DefaultObjectValidator(Mock.Of(), modelMetadataProvider), + new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], modelMetadataProvider), validator); // Assert @@ -118,7 +118,7 @@ public async Task TryUpdateModel_ReturnsTrue_IfModelBindsAndValidatesSuccessfull metadataProvider, GetCompositeBinder(binders), valueProvider, - new DefaultObjectValidator(Mock.Of(), metadataProvider), + new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider), validator); // Assert @@ -202,7 +202,7 @@ public async Task TryUpdateModel_UsingIncludePredicateOverload_ReturnsTrue_Model metadataProvider, GetCompositeBinder(binders), valueProvider, - new DefaultObjectValidator(Mock.Of(), metadataProvider), + new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider), validator, includePredicate); @@ -284,7 +284,7 @@ public async Task TryUpdateModel_UsingIncludeExpressionOverload_ReturnsTrue_Mode TestModelMetadataProvider.CreateDefaultProvider(), GetCompositeBinder(binders), valueProvider, - new DefaultObjectValidator(Mock.Of(), metadataProvider), + new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider), validator, m => m.IncludedProperty, m => m.MyProperty); @@ -336,7 +336,7 @@ public async Task TryUpdateModel_UsingDefaultIncludeOverload_IncludesAllProperti metadataProvider, GetCompositeBinder(binders), valueProvider, - new DefaultObjectValidator(Mock.Of(), metadataProvider), + new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider), validator); // Assert @@ -545,7 +545,7 @@ public async Task TryUpdateModelNonGeneric_PredicateOverload_ReturnsTrue_ModelBi GetCompositeBinder(binders), valueProvider, new DefaultObjectValidator( - Mock.Of(), + new IExcludeTypeValidationFilter[0], metadataProvider), validator, includePredicate); @@ -619,7 +619,7 @@ public async Task TryUpdateModelNonGeneric_ModelTypeOverload_ReturnsTrue_IfModel GetCompositeBinder(binders), valueProvider, new DefaultObjectValidator( - Mock.Of(), + new IExcludeTypeValidationFilter[0], metadataProvider), validator); @@ -653,7 +653,7 @@ public async Task TryUpdataModel_ModelTypeDifferentFromModel_Throws() GetCompositeBinder(binder.Object), Mock.Of(), new DefaultObjectValidator( - Mock.Of(), + new IExcludeTypeValidationFilter[0], metadataProvider), Mock.Of(), includePredicate)); diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/ViewEngine/CompositeViewEngineTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/ViewEngine/CompositeViewEngineTest.cs index a0e34c1774..43bc869992 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/ViewEngine/CompositeViewEngineTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/ViewEngine/CompositeViewEngineTest.cs @@ -1,6 +1,7 @@ // 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.Linq; using Microsoft.AspNet.Http; using Microsoft.AspNet.Routing; @@ -16,11 +17,12 @@ public void FindView_ReturnsNotFoundResult_WhenNoViewEnginesAreRegistered() { // Arrange var viewName = "test-view"; - var provider = new Mock(); - provider.SetupGet(p => p.ViewEngines) - .Returns(new IViewEngine[0]); var actionContext = GetActionContext(); - var compositeViewEngine = new CompositeViewEngine(provider.Object); + var optionsAccessor = new MockMvcOptionsAccessor(); + var compositeViewEngine = new CompositeViewEngine( + optionsAccessor, + Mock.Of(), + Mock.Of()); // Act var result = compositeViewEngine.FindView(actionContext, viewName); @@ -36,13 +38,15 @@ public void FindView_ReturnsNotFoundResult_WhenExactlyOneViewEngineIsRegisteredW { // Arrange var viewName = "test-view"; - var provider = new Mock(); var engine = new Mock(); engine.Setup(e => e.FindView(It.IsAny(), It.IsAny())) .Returns(ViewEngineResult.NotFound(viewName, new[] { "controller/test-view" })); - provider.SetupGet(p => p.ViewEngines) - .Returns(new[] { engine.Object }); - var compositeViewEngine = new CompositeViewEngine(provider.Object); + var optionsAccessor = new MockMvcOptionsAccessor(); + optionsAccessor.Options.ViewEngines.Add(engine.Object); + var compositeViewEngine = new CompositeViewEngine( + optionsAccessor, + Mock.Of(), + Mock.Of()); // Act var result = compositeViewEngine.FindView(GetActionContext(), viewName); @@ -57,14 +61,16 @@ public void FindView_ReturnsView_WhenExactlyOneViewEngineIsRegisteredWhichReturn { // Arrange var viewName = "test-view"; - var provider = new Mock(); var engine = new Mock(); var view = Mock.Of(); engine.Setup(e => e.FindView(It.IsAny(), It.IsAny())) .Returns(ViewEngineResult.Found(viewName, view)); - provider.SetupGet(p => p.ViewEngines) - .Returns(new[] { engine.Object }); - var compositeViewEngine = new CompositeViewEngine(provider.Object); + var optionsAccessor = new MockMvcOptionsAccessor(); + optionsAccessor.Options.ViewEngines.Add(engine.Object); + var compositeViewEngine = new CompositeViewEngine( + optionsAccessor, + Mock.Of(), + Mock.Of()); // Act var result = compositeViewEngine.FindView(GetActionContext(), viewName); @@ -91,10 +97,14 @@ public void FindView_ReturnsViewFromFirstViewEngineWithFoundResult() engine3.Setup(e => e.FindView(It.IsAny(), It.IsAny())) .Returns(ViewEngineResult.Found(viewName, view3)); - var provider = new Mock(); - provider.SetupGet(p => p.ViewEngines) - .Returns(new[] { engine1.Object, engine2.Object, engine3.Object }); - var compositeViewEngine = new CompositeViewEngine(provider.Object); + var optionsAccessor = new MockMvcOptionsAccessor(); + optionsAccessor.Options.ViewEngines.Add(engine1.Object); + optionsAccessor.Options.ViewEngines.Add(engine2.Object); + optionsAccessor.Options.ViewEngines.Add(engine3.Object); + var compositeViewEngine = new CompositeViewEngine( + optionsAccessor, + Mock.Of(), + Mock.Of()); // Act var result = compositeViewEngine.FindView(GetActionContext(), viewName); @@ -120,10 +130,14 @@ public void FindView_ReturnsNotFound_IfAllViewEnginesReturnNotFound() engine3.Setup(e => e.FindView(It.IsAny(), It.IsAny())) .Returns(ViewEngineResult.NotFound(viewName, new[] { "4", "5" })); - var provider = new Mock(); - provider.SetupGet(p => p.ViewEngines) - .Returns(new[] { engine1.Object, engine2.Object, engine3.Object }); - var compositeViewEngine = new CompositeViewEngine(provider.Object); + var optionsAccessor = new MockMvcOptionsAccessor(); + optionsAccessor.Options.ViewEngines.Add(engine1.Object); + optionsAccessor.Options.ViewEngines.Add(engine2.Object); + optionsAccessor.Options.ViewEngines.Add(engine3.Object); + var compositeViewEngine = new CompositeViewEngine( + optionsAccessor, + Mock.Of(), + Mock.Of()); // Act var result = compositeViewEngine.FindView(GetActionContext(), viewName); @@ -138,10 +152,11 @@ public void FindPartialView_ReturnsNotFoundResult_WhenNoViewEnginesAreRegistered { // Arrange var viewName = "my-partial-view"; - var provider = new Mock(); - provider.SetupGet(p => p.ViewEngines) - .Returns(new IViewEngine[0]); - var compositeViewEngine = new CompositeViewEngine(provider.Object); + var optionsAccessor = new MockMvcOptionsAccessor(); + var compositeViewEngine = new CompositeViewEngine( + optionsAccessor, + Mock.Of(), + Mock.Of()); // Act var result = compositeViewEngine.FindPartialView(GetActionContext(), viewName); @@ -156,13 +171,15 @@ public void FindPartialView_ReturnsNotFoundResult_WhenExactlyOneViewEngineIsRegi { // Arrange var viewName = "partial-view"; - var provider = new Mock(); var engine = new Mock(); engine.Setup(e => e.FindPartialView(It.IsAny(), It.IsAny())) .Returns(ViewEngineResult.NotFound(viewName, new[] { "Shared/partial-view" })); - provider.SetupGet(p => p.ViewEngines) - .Returns(new[] { engine.Object }); - var compositeViewEngine = new CompositeViewEngine(provider.Object); + var optionsAccessor = new MockMvcOptionsAccessor(); + optionsAccessor.Options.ViewEngines.Add(engine.Object); + var compositeViewEngine = new CompositeViewEngine( + optionsAccessor, + Mock.Of(), + Mock.Of()); // Act var result = compositeViewEngine.FindPartialView(GetActionContext(), viewName); @@ -177,14 +194,16 @@ public void FindPartialView_ReturnsView_WhenExactlyOneViewEngineIsRegisteredWhic { // Arrange var viewName = "test-view"; - var provider = new Mock(); var engine = new Mock(); var view = Mock.Of(); engine.Setup(e => e.FindPartialView(It.IsAny(), It.IsAny())) .Returns(ViewEngineResult.Found(viewName, view)); - provider.SetupGet(p => p.ViewEngines) - .Returns(new[] { engine.Object }); - var compositeViewEngine = new CompositeViewEngine(provider.Object); + var optionsAccessor = new MockMvcOptionsAccessor(); + optionsAccessor.Options.ViewEngines.Add(engine.Object); + var compositeViewEngine = new CompositeViewEngine( + optionsAccessor, + Mock.Of(), + Mock.Of()); // Act var result = compositeViewEngine.FindPartialView(GetActionContext(), viewName); @@ -211,10 +230,14 @@ public void FindPartialView_ReturnsViewFromFirstViewEngineWithFoundResult() engine3.Setup(e => e.FindPartialView(It.IsAny(), It.IsAny())) .Returns(ViewEngineResult.Found(viewName, view3)); - var provider = new Mock(); - provider.SetupGet(p => p.ViewEngines) - .Returns(new[] { engine1.Object, engine2.Object, engine3.Object }); - var compositeViewEngine = new CompositeViewEngine(provider.Object); + var optionsAccessor = new MockMvcOptionsAccessor(); + optionsAccessor.Options.ViewEngines.Add(engine1.Object); + optionsAccessor.Options.ViewEngines.Add(engine2.Object); + optionsAccessor.Options.ViewEngines.Add(engine3.Object); + var compositeViewEngine = new CompositeViewEngine( + optionsAccessor, + Mock.Of(), + Mock.Of()); // Act var result = compositeViewEngine.FindPartialView(GetActionContext(), viewName); @@ -240,10 +263,14 @@ public void FindPartialView_ReturnsNotFound_IfAllViewEnginesReturnNotFound() engine3.Setup(e => e.FindPartialView(It.IsAny(), It.IsAny())) .Returns(ViewEngineResult.NotFound(viewName, new[] { "4", "5" })); - var provider = new Mock(); - provider.SetupGet(p => p.ViewEngines) - .Returns(new[] { engine1.Object, engine2.Object, engine3.Object }); - var compositeViewEngine = new CompositeViewEngine(provider.Object); + var optionsAccessor = new MockMvcOptionsAccessor(); + optionsAccessor.Options.ViewEngines.Add(engine1.Object); + optionsAccessor.Options.ViewEngines.Add(engine2.Object); + optionsAccessor.Options.ViewEngines.Add(engine3.Object); + var compositeViewEngine = new CompositeViewEngine( + optionsAccessor, + Mock.Of(), + Mock.Of()); // Act var result = compositeViewEngine.FindPartialView(GetActionContext(), viewName); diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/ModelBindingModelBinderAttributeTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/ModelBindingModelBinderAttributeTest.cs index f56b3ecb57..bf1c4806fe 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/ModelBindingModelBinderAttributeTest.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/ModelBindingModelBinderAttributeTest.cs @@ -61,10 +61,8 @@ public async Task ModelBinderAttribute_CustomModelPrefix_OnProperty() Assert.Equal("somealias", employee.EmailAlias); } - [Theory] - [InlineData("GetBinderType_UseModelBinderOnType")] - [InlineData("GetBinderType_UseModelBinderProviderOnType")] - public async Task ModelBinderAttribute_WithPrefixOnParameter(string action) + [Fact] + public async Task ModelBinderAttribute_WithPrefixOnParameter() { // Arrange var server = TestHelper.CreateServer(_app, SiteName, _configureServices); @@ -72,8 +70,7 @@ public async Task ModelBinderAttribute_WithPrefixOnParameter(string action) // [ModelBinder(Name = "customPrefix")] is used to apply a prefix var url = - "http://localhost/ModelBinderAttribute_Product/" + - action + + "http://localhost/ModelBinderAttribute_Product/GetBinderType_UseModelBinderOnType" + "?customPrefix.ProductId=5"; // Act @@ -86,18 +83,15 @@ public async Task ModelBinderAttribute_WithPrefixOnParameter(string action) body); } - [Theory] - [InlineData("GetBinderType_UseModelBinder")] - [InlineData("GetBinderType_UseModelBinderProvider")] - public async Task ModelBinderAttribute_WithBinderOnParameter(string action) + [Fact] + public async Task ModelBinderAttribute_WithBinderOnParameter() { // Arrange var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); var url = - "http://localhost/ModelBinderAttribute_Product/" + - action + + "http://localhost/ModelBinderAttribute_Product/GetBinderType_UseModelBinder/" + "?model.productId=5"; // Act diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/BinderTypeBasedModelBinderModelBinderTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/BinderTypeBasedModelBinderModelBinderTest.cs index 435d5596cc..bdbb420e01 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/BinderTypeBasedModelBinderModelBinderTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/BinderTypeBasedModelBinderModelBinderTest.cs @@ -3,7 +3,6 @@ #if DNX451 using System; -using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNet.Http.Core; using Microsoft.AspNet.Mvc.ModelBinding.Validation; @@ -71,41 +70,14 @@ public async Task BindModel_CallsBindAsync_OnProvidedModelBinder() } [Fact] - public async Task BindModel_CallsBindAsync_OnProvidedModelBinderProvider() - { - // Arrange - var bindingContext = GetBindingContext(typeof(Person), binderType: typeof(ModelBinderProvider)); - - var model = new Person(); - var provider = new ModelBinderProvider(); - - var serviceProvider = new ServiceCollection() - .AddSingleton(typeof(IModelBinderProvider)) - .AddSingleton(typeof(IModelBinder)) - .BuildServiceProvider(); - - bindingContext.OperationBindingContext.HttpContext.RequestServices = serviceProvider; - var binder = new BinderTypeBasedModelBinder(); - - // Act - var binderResult = await binder.BindModelAsync(bindingContext); - - // Assert - var p = (Person)binderResult.Model; - Assert.Equal(model.Age, p.Age); - Assert.Equal(model.Name, p.Name); - } - - [Fact] - public async Task BindModel_ForNonModelBinderAndModelBinderProviderTypes_Throws() + public async Task BindModel_ForNonModelBinder_Throws() { // Arrange var bindingContext = GetBindingContext(typeof(Person), binderType: typeof(Person)); var binder = new BinderTypeBasedModelBinder(); - var expected = "The type '" + typeof(Person).FullName + "' must implement either " + - "'Microsoft.AspNet.Mvc.ModelBinding.IModelBinder' or " + - "'Microsoft.AspNet.Mvc.ModelBinding.IModelBinderProvider' to be used as a model binder."; + var expected = $"The type '{typeof(Person).FullName}' must implement " + + $"'{typeof(IModelBinder).FullName}' to be used as a model binder."; // Act var ex = await Assert.ThrowsAsync( @@ -169,25 +141,6 @@ public Task BindModelAsync(ModelBindingContext bindingContex return Task.FromResult(new ModelBindingResult(_model, bindingContext.ModelName, true)); } } - - private class ModelBinderProvider : IModelBinderProvider - { - private readonly IModelBinder _inner; - - public ModelBinderProvider() - { - var innerModelBinder = new TrueModelBinder(); - _inner = innerModelBinder; - } - - public IReadOnlyList ModelBinders - { - get - { - return new List() { _inner, }; - } - } - } } } #endif diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Metadata/ModelBinderAttributeTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Metadata/ModelBinderAttributeTest.cs index ee720736de..6408b5b7ea 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Metadata/ModelBinderAttributeTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Metadata/ModelBinderAttributeTest.cs @@ -15,9 +15,8 @@ public void InvalidBinderType_Throws() var attribute = new ModelBinderAttribute(); var expected = - "The type 'System.String' must implement either " + - "'Microsoft.AspNet.Mvc.ModelBinding.IModelBinder' or " + - "'Microsoft.AspNet.Mvc.ModelBinding.IModelBinderProvider' to be used as a model binder."; + $"The type 'System.String' must implement '{typeof(IModelBinder).FullName}' " + + "to be used as a model binder."; // Act var ex = Assert.Throws(() => { attribute.BinderType = typeof(string); }); diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/DefaultObjectValidatorTests.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/DefaultObjectValidatorTests.cs index 17a6c19b6c..2429f81687 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/DefaultObjectValidatorTests.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/DefaultObjectValidatorTests.cs @@ -211,7 +211,7 @@ public void ExpectedValidationErrorsRaised(object model, Type type, Dictionary { new DefaultObjectValidator( - testValidationContext.ExcludeFiltersProvider, + testValidationContext.ExcludeFilters, testValidationContext.ModelMetadataProvider) .Validate(testValidationContext.ModelValidationContext); }); @@ -280,7 +280,7 @@ public void Validator_DoesNotThrow_IfExcludedPropertyAccessorsThrow( // Act & Assert (does not throw) new DefaultObjectValidator( - testValidationContext.ExcludeFiltersProvider, + testValidationContext.ExcludeFilters, testValidationContext.ModelMetadataProvider) .Validate(testValidationContext.ModelValidationContext); Assert.True(testValidationContext.ModelValidationContext.ModelState.IsValid); @@ -300,7 +300,7 @@ public void Validator_Throws_IfPropertyGetterThrows() () => { new DefaultObjectValidator( - testValidationContext.ExcludeFiltersProvider, + testValidationContext.ExcludeFilters, testValidationContext.ModelMetadataProvider) .Validate(validationContext); }); @@ -318,7 +318,7 @@ public void MultipleValidationErrorsOnSameMemberReported() // Act (does not throw) new DefaultObjectValidator( - testValidationContext.ExcludeFiltersProvider, + testValidationContext.ExcludeFilters, testValidationContext.ModelMetadataProvider) .Validate(validationContext); @@ -343,7 +343,7 @@ public void Validate_DoesNotUseOverridden_GetHashCodeOrEquals() // Act & Assert (does not throw) new DefaultObjectValidator( - testValidationContext.ExcludeFiltersProvider, + testValidationContext.ExcludeFilters, testValidationContext.ModelMetadataProvider) .Validate(testValidationContext.ModelValidationContext); } @@ -368,7 +368,7 @@ public void Validation_ShortCircuit_WhenMaxErrorCountIsSet() validationContext.ModelState.MaxAllowedErrors = 2; validationContext.ModelState.AddModelError("key1", "error1"); var validator = new DefaultObjectValidator( - testValidationContext.ExcludeFiltersProvider, + testValidationContext.ExcludeFilters, testValidationContext.ModelMetadataProvider); // Act @@ -402,7 +402,7 @@ public void ForExcludedNonModelBoundType_Properties_NotMarkedAsSkiped() new List { typeof(User) }); var validationContext = testValidationContext.ModelValidationContext; var validator = new DefaultObjectValidator( - testValidationContext.ExcludeFiltersProvider, + testValidationContext.ExcludeFilters, testValidationContext.ModelMetadataProvider); // Act @@ -438,7 +438,7 @@ public void ForExcludedModelBoundTypes_Properties_MarkedAsSkipped() "user.Password", Mock.Of()); var validator = new DefaultObjectValidator( - testValidationContext.ExcludeFiltersProvider, + testValidationContext.ExcludeFilters, testValidationContext.ModelMetadataProvider); // Act @@ -465,7 +465,7 @@ public void NonRequestBoundModel_MarkedAsSkipped() var validationContext = testValidationContext.ModelValidationContext; var validator = new DefaultObjectValidator( - testValidationContext.ExcludeFiltersProvider, + testValidationContext.ExcludeFilters, testValidationContext.ModelMetadataProvider); // Act @@ -502,17 +502,12 @@ public void EnumerableType_ValidationSuccessful(object model, Type type) var excludeTypeFilters = new List(); excludeTypeFilters.Add(new SimpleTypesExcludeFilter()); - - var mockValidationExcludeFiltersProvider = new Mock(); - mockValidationExcludeFiltersProvider - .SetupGet(o => o.ExcludeFilters) - .Returns(excludeTypeFilters); - testValidationContext.ExcludeFiltersProvider = mockValidationExcludeFiltersProvider.Object; + testValidationContext.ExcludeFilters = excludeTypeFilters; var validationContext = testValidationContext.ModelValidationContext; var validator = new DefaultObjectValidator( - testValidationContext.ExcludeFiltersProvider, + testValidationContext.ExcludeFilters, testValidationContext.ModelMetadataProvider); // Act @@ -549,17 +544,12 @@ public void DictionaryType_ValidationSuccessful() var excludeTypeFilters = new List(); excludeTypeFilters.Add(new SimpleTypesExcludeFilter()); - - var mockValidationExcludeFiltersProvider = new Mock(); - mockValidationExcludeFiltersProvider - .SetupGet(o => o.ExcludeFilters) - .Returns(excludeTypeFilters); - testValidationContext.ExcludeFiltersProvider = mockValidationExcludeFiltersProvider.Object; + testValidationContext.ExcludeFilters = excludeTypeFilters; var validationContext = testValidationContext.ModelValidationContext; var validator = new DefaultObjectValidator( - testValidationContext.ExcludeFiltersProvider, + testValidationContext.ExcludeFilters, testValidationContext.ModelMetadataProvider); // Act @@ -608,11 +598,6 @@ private TestModelValidationContext GetModelValidationContext( excludedValidationTypesPredicate.Add(mockExcludeTypeFilter.Object); } - var mockValidationExcludeFiltersProvider = new Mock(); - mockValidationExcludeFiltersProvider - .SetupGet(o => o.ExcludeFilters) - .Returns(excludedValidationTypesPredicate); - var modelExplorer = modelMetadataProvider.GetModelExplorerForType(type, model); return new TestModelValidationContext @@ -624,7 +609,7 @@ private TestModelValidationContext GetModelValidationContext( modelStateDictionary, modelExplorer), ModelMetadataProvider = modelMetadataProvider, - ExcludeFiltersProvider = mockValidationExcludeFiltersProvider.Object + ExcludeFilters = excludedValidationTypesPredicate }; } @@ -762,7 +747,7 @@ private class TestModelValidationContext public IModelMetadataProvider ModelMetadataProvider { get; set; } - public IValidationExcludeFiltersProvider ExcludeFiltersProvider { get; set; } + public IList ExcludeFilters { get; set; } } } } diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/OptionDescriptors/DefaultViewLocationExpanderProviderTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Test/OptionDescriptors/DefaultViewLocationExpanderProviderTest.cs deleted file mode 100644 index 495fdc0992..0000000000 --- a/test/Microsoft.AspNet.Mvc.Razor.Test/OptionDescriptors/DefaultViewLocationExpanderProviderTest.cs +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.OptionDescriptors; -using Microsoft.Framework.OptionsModel; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc.Razor.OptionDescriptors -{ - public class DefaultViewLocationExpanderProviderTest - { - [Fact] - public void ViewLocationExpanders_ReturnsActivatedListOfExpanders() - { - // Arrange - var service = Mock.Of(); - var expander = Mock.Of(); - var type = typeof(TestViewLocationExpander); - var serviceProvider = new Mock(); - serviceProvider.Setup(p => p.GetService(typeof(ITestService))) - .Returns(service); - var typeActivatorCache = new DefaultTypeActivatorCache(); - var options = new RazorViewEngineOptions(); - options.ViewLocationExpanders.Add(type); - options.ViewLocationExpanders.Add(expander); - var accessor = new Mock>(); - accessor.SetupGet(a => a.Options) - .Returns(options); - var provider = new DefaultViewLocationExpanderProvider(accessor.Object, - typeActivatorCache, - serviceProvider.Object); - - // Act - var result = provider.ViewLocationExpanders; - - // Assert - Assert.Equal(2, result.Count); - var testExpander = Assert.IsType(result[0]); - Assert.Same(service, testExpander.Service); - Assert.Same(expander, result[1]); - } - - private class TestViewLocationExpander : IViewLocationExpander - { - public TestViewLocationExpander(ITestService service) - { - Service = service; - } - - public ITestService Service { get; private set; } - - public IEnumerable ExpandViewLocations(ViewLocationExpanderContext context, - IEnumerable viewLocations) - { - throw new NotImplementedException(); - } - - public void PopulateValues(ViewLocationExpanderContext context) - { - throw new NotImplementedException(); - } - } - - public interface ITestService - { - } - } -} diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/OptionDescriptors/ViewLocationExpanderDescriptorExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Test/OptionDescriptors/ViewLocationExpanderDescriptorExtensionsTest.cs deleted file mode 100644 index a84e8382af..0000000000 --- a/test/Microsoft.AspNet.Mvc.Razor.Test/OptionDescriptors/ViewLocationExpanderDescriptorExtensionsTest.cs +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.Razor.OptionDescriptors; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc.Razor -{ - public class ViewLocationExpanderDescriptorExtensionsTest - { - [Theory] - [InlineData(-1)] - [InlineData(5)] - public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - new ViewLocationExpanderDescriptor(Mock.Of()), - new ViewLocationExpanderDescriptor(Mock.Of()) - }; - - // Act & Assert - Assert.Throws("index", - () => collection.Insert(index, typeof(IViewLocationExpander))); - } - - [Theory] - [InlineData(-2)] - [InlineData(3)] - public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - new ViewLocationExpanderDescriptor(Mock.Of()), - new ViewLocationExpanderDescriptor(Mock.Of()) - }; - var expander = Mock.Of(); - - // Act & Assert - Assert.Throws("index", () => collection.Insert(index, expander)); - } - - [InlineData] - public void ViewLocationExpanderDescriptors_AddsTypesAndInstances() - { - // Arrange - var expander = Mock.Of(); - var type = typeof(TestViewLocationExpander); - var collection = new List(); - - // Act - collection.Add(expander); - collection.Insert(0, type); - - // Assert - Assert.Equal(2, collection.Count); - Assert.IsType(collection[0].Instance); - Assert.Same(expander, collection[0].Instance); - } - - private class TestViewLocationExpander : IViewLocationExpander - { - public IEnumerable ExpandViewLocations(ViewLocationExpanderContext context, - IEnumerable viewLocations) - { - throw new NotImplementedException(); - } - - public void PopulateValues(ViewLocationExpanderContext context) - { - throw new NotImplementedException(); - } - } - } -} diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/OptionDescriptors/ViewLocationExpanderDescriptorTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Test/OptionDescriptors/ViewLocationExpanderDescriptorTest.cs deleted file mode 100644 index 515bccd7a1..0000000000 --- a/test/Microsoft.AspNet.Mvc.Razor.Test/OptionDescriptors/ViewLocationExpanderDescriptorTest.cs +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Testing; -using Xunit; - -namespace Microsoft.AspNet.Mvc.Razor.OptionDescriptors -{ - public class ViewLocationExpanderDescriptorTest - { - [Fact] - public void ConstructorThrows_IfTypeIsNotViewLocationExpander() - { - // Arrange - var viewEngineType = typeof(IViewLocationExpander).FullName; - var type = typeof(string); - var expected = string.Format("The type '{0}' must derive from '{1}'.", - type.FullName, viewEngineType); - - // Act & Assert - ExceptionAssert.ThrowsArgument(() => new ViewLocationExpanderDescriptor(type), "type", expected); - } - - [Fact] - public void ConstructorSetsViewLocationExpanderType() - { - // Arrange - var type = typeof(TestViewLocationExpander); - - // Act - var descriptor = new ViewLocationExpanderDescriptor(type); - - // Assert - Assert.Equal(type, descriptor.OptionType); - Assert.Null(descriptor.Instance); - } - - [Fact] - public void ConstructorSetsViewLocationExpanderAndType() - { - // Arrange - var expander = new TestViewLocationExpander(); - - // Act - var descriptor = new ViewLocationExpanderDescriptor(expander); - - // Assert - Assert.Same(expander, descriptor.Instance); - Assert.Equal(expander.GetType(), descriptor.OptionType); - } - - private class TestViewLocationExpander : IViewLocationExpander - { - public IEnumerable ExpandViewLocations(ViewLocationExpanderContext context, - IEnumerable viewLocations) - { - throw new NotImplementedException(); - } - - public void PopulateValues(ViewLocationExpanderContext context) - { - throw new NotImplementedException(); - } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewEngineTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewEngineTest.cs index af4582e92c..9c80095141 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewEngineTest.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewEngineTest.cs @@ -3,14 +3,14 @@ using System.Collections.Generic; using System.Linq; -using Microsoft.AspNet.Mvc.Razor.OptionDescriptors; -using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Http.Core; +using Microsoft.AspNet.Mvc.Rendering; +using Microsoft.AspNet.Mvc.Routing; using Microsoft.AspNet.Routing; using Microsoft.AspNet.Testing; +using Microsoft.Framework.OptionsModel; using Moq; using Xunit; -using Microsoft.AspNet.Mvc.Routing; namespace Microsoft.AspNet.Mvc.Razor.Test { @@ -300,7 +300,7 @@ public void FindView_UsesViewLocationFormat_IfRouteDoesNotContainArea() .Verifiable(); var viewEngine = new OverloadedLocationViewEngine(pageFactory.Object, viewFactory.Object, - GetViewLocationExpanders(), + GetOptionsAccessor(), GetViewLocationCache()); var context = GetActionContext(_controllerTestContext); @@ -323,7 +323,7 @@ public void FindView_UsesAreaViewLocationFormat_IfRouteContainsArea() .Verifiable(); var viewEngine = new OverloadedLocationViewEngine(pageFactory.Object, viewFactory.Object, - GetViewLocationExpanders(), + GetOptionsAccessor(), GetViewLocationCache()); var context = GetActionContext(_areaTestContext); @@ -1026,24 +1026,31 @@ private RazorViewEngine CreateViewEngine(IRazorPageFactory pageFactory = null, viewFactory = viewFactory ?? Mock.Of(); cache = cache ?? GetViewLocationCache(); - var viewLocationExpanderProvider = GetViewLocationExpanders(expanders); var viewEngine = new RazorViewEngine(pageFactory, viewFactory, - viewLocationExpanderProvider, + GetOptionsAccessor(expanders), cache); return viewEngine; } - private static IViewLocationExpanderProvider GetViewLocationExpanders( + private static IOptions GetOptionsAccessor( IEnumerable expanders = null) { - expanders = expanders ?? Enumerable.Empty(); - var viewLocationExpander = new Mock(); - viewLocationExpander.Setup(v => v.ViewLocationExpanders) - .Returns(expanders.ToList()); - return viewLocationExpander.Object; + var options = new RazorViewEngineOptions(); + if (expanders != null) + { + foreach (var expander in expanders) + { + options.ViewLocationExpanders.Add(expander); + } + } + + var optionsAccessor = new Mock>(); + optionsAccessor.SetupGet(v => v.Options) + .Returns(options); + return optionsAccessor.Object; } private static IViewLocationCache GetViewLocationCache() @@ -1106,9 +1113,9 @@ private class OverloadedLocationViewEngine : RazorViewEngine { public OverloadedLocationViewEngine(IRazorPageFactory pageFactory, IRazorViewFactory viewFactory, - IViewLocationExpanderProvider expanderProvider, + IOptions optionsAccessor, IViewLocationCache cache) - : base(pageFactory, viewFactory, expanderProvider, cache) + : base(pageFactory, viewFactory, optionsAccessor, cache) { } diff --git a/test/Microsoft.AspNet.Mvc.Test/MvcOptionsSetupTest.cs b/test/Microsoft.AspNet.Mvc.Test/MvcOptionsSetupTest.cs index c1fcb862fe..1b750be26e 100644 --- a/test/Microsoft.AspNet.Mvc.Test/MvcOptionsSetupTest.cs +++ b/test/Microsoft.AspNet.Mvc.Test/MvcOptionsSetupTest.cs @@ -25,7 +25,7 @@ public void Setup_SetsUpViewEngines() // Assert Assert.Equal(1, mvcOptions.ViewEngines.Count); - Assert.Equal(typeof(RazorViewEngine), mvcOptions.ViewEngines[0].OptionType); + Assert.Equal(typeof(RazorViewEngine), mvcOptions.ViewEngines[0].ViewEngineType); } [Fact] @@ -41,19 +41,19 @@ public void Setup_SetsUpModelBinders() // Assert var i = 0; Assert.Equal(13, mvcOptions.ModelBinders.Count); - Assert.Equal(typeof(BinderTypeBasedModelBinder), mvcOptions.ModelBinders[i++].OptionType); - Assert.Equal(typeof(ServicesModelBinder), mvcOptions.ModelBinders[i++].OptionType); - Assert.Equal(typeof(BodyModelBinder), mvcOptions.ModelBinders[i++].OptionType); - Assert.Equal(typeof(HeaderModelBinder), mvcOptions.ModelBinders[i++].OptionType); - Assert.Equal(typeof(TypeConverterModelBinder), mvcOptions.ModelBinders[i++].OptionType); - Assert.Equal(typeof(TypeMatchModelBinder), mvcOptions.ModelBinders[i++].OptionType); - Assert.Equal(typeof(CancellationTokenModelBinder), mvcOptions.ModelBinders[i++].OptionType); - Assert.Equal(typeof(ByteArrayModelBinder), mvcOptions.ModelBinders[i++].OptionType); - Assert.Equal(typeof(FormFileModelBinder), mvcOptions.ModelBinders[i++].OptionType); - Assert.Equal(typeof(FormCollectionModelBinder), mvcOptions.ModelBinders[i++].OptionType); - Assert.Equal(typeof(GenericModelBinder), mvcOptions.ModelBinders[i++].OptionType); - Assert.Equal(typeof(MutableObjectModelBinder), mvcOptions.ModelBinders[i++].OptionType); - Assert.Equal(typeof(ComplexModelDtoModelBinder), mvcOptions.ModelBinders[i++].OptionType); + Assert.IsType(typeof(BinderTypeBasedModelBinder), mvcOptions.ModelBinders[i++]); + Assert.IsType(typeof(ServicesModelBinder), mvcOptions.ModelBinders[i++]); + Assert.IsType(typeof(BodyModelBinder), mvcOptions.ModelBinders[i++]); + Assert.IsType(typeof(HeaderModelBinder), mvcOptions.ModelBinders[i++]); + Assert.IsType(typeof(TypeConverterModelBinder), mvcOptions.ModelBinders[i++]); + Assert.IsType(typeof(TypeMatchModelBinder), mvcOptions.ModelBinders[i++]); + Assert.IsType(typeof(CancellationTokenModelBinder), mvcOptions.ModelBinders[i++]); + Assert.IsType(typeof(ByteArrayModelBinder), mvcOptions.ModelBinders[i++]); + Assert.IsType(typeof(FormFileModelBinder), mvcOptions.ModelBinders[i++]); + Assert.IsType(typeof(FormCollectionModelBinder), mvcOptions.ModelBinders[i++]); + Assert.IsType(typeof(GenericModelBinder), mvcOptions.ModelBinders[i++]); + Assert.IsType(typeof(MutableObjectModelBinder), mvcOptions.ModelBinders[i++]); + Assert.IsType(typeof(ComplexModelDtoModelBinder), mvcOptions.ModelBinders[i++]); } [Fact] @@ -69,9 +69,9 @@ public void Setup_SetsUpValueProviders() // Assert var valueProviders = mvcOptions.ValueProviderFactories; Assert.Equal(3, valueProviders.Count); - Assert.IsType(valueProviders[0].Instance); - Assert.IsType(valueProviders[1].Instance); - Assert.IsType(valueProviders[2].Instance); + Assert.IsType(valueProviders[0]); + Assert.IsType(valueProviders[1]); + Assert.IsType(valueProviders[2]); } [Fact] @@ -86,10 +86,10 @@ public void Setup_SetsUpOutputFormatters() // Assert Assert.Equal(4, mvcOptions.OutputFormatters.Count); - Assert.IsType(mvcOptions.OutputFormatters[0].Instance); - Assert.IsType(mvcOptions.OutputFormatters[1].Instance); - Assert.IsType(mvcOptions.OutputFormatters[2].Instance); - Assert.IsType(mvcOptions.OutputFormatters[3].Instance); + Assert.IsType(mvcOptions.OutputFormatters[0]); + Assert.IsType(mvcOptions.OutputFormatters[1]); + Assert.IsType(mvcOptions.OutputFormatters[2]); + Assert.IsType(mvcOptions.OutputFormatters[3]); } [Fact] @@ -104,8 +104,8 @@ public void Setup_SetsUpInputFormatters() // Assert Assert.Equal(2, mvcOptions.InputFormatters.Count); - Assert.IsType(mvcOptions.InputFormatters[0].Instance); - Assert.IsType(mvcOptions.InputFormatters[1].Instance); + Assert.IsType(mvcOptions.InputFormatters[0]); + Assert.IsType(mvcOptions.InputFormatters[1]); } [Fact] @@ -120,8 +120,8 @@ public void Setup_SetsUpModelValidatorProviders() // Assert Assert.Equal(2, mvcOptions.ModelValidatorProviders.Count); - Assert.IsType(mvcOptions.ModelValidatorProviders[0].Instance); - Assert.IsType(mvcOptions.ModelValidatorProviders[1].Instance); + Assert.IsType(mvcOptions.ModelValidatorProviders[0]); + Assert.IsType(mvcOptions.ModelValidatorProviders[1]); } [Fact] @@ -153,42 +153,42 @@ public void Setup_SetsUpExcludeFromValidationDelegates() var i = 0; // Verify if the delegates registered by default exclude the given types. - Assert.Equal(typeof(SimpleTypesExcludeFilter), mvcOptions.ValidationExcludeFilters[i++].OptionType); - Assert.Equal(typeof(DefaultTypeBasedExcludeFilter), mvcOptions.ValidationExcludeFilters[i].OptionType); - var xObjectFilter - = Assert.IsType(mvcOptions.ValidationExcludeFilters[i++].Instance); + Assert.IsType(typeof(SimpleTypesExcludeFilter), mvcOptions.ValidationExcludeFilters[i++]); + Assert.IsType(typeof(DefaultTypeBasedExcludeFilter), mvcOptions.ValidationExcludeFilters[i]); + var xObjectFilter + = Assert.IsType(mvcOptions.ValidationExcludeFilters[i++]); Assert.Equal(xObjectFilter.ExcludedType, typeof(XObject)); - Assert.Equal(typeof(DefaultTypeBasedExcludeFilter), mvcOptions.ValidationExcludeFilters[i].OptionType); + Assert.IsType(typeof(DefaultTypeBasedExcludeFilter), mvcOptions.ValidationExcludeFilters[i]); var typeFilter - = Assert.IsType(mvcOptions.ValidationExcludeFilters[i++].Instance); + = Assert.IsType(mvcOptions.ValidationExcludeFilters[i++]); Assert.Equal(typeFilter.ExcludedType, typeof(Type)); - Assert.Equal(typeof(DefaultTypeBasedExcludeFilter), mvcOptions.ValidationExcludeFilters[i].OptionType); + Assert.IsType(typeof(DefaultTypeBasedExcludeFilter), mvcOptions.ValidationExcludeFilters[i]); var jTokenFilter - = Assert.IsType(mvcOptions.ValidationExcludeFilters[i++].Instance); + = Assert.IsType(mvcOptions.ValidationExcludeFilters[i++]); Assert.Equal(jTokenFilter.ExcludedType, typeof(JToken)); - Assert.Equal(typeof(DefaultTypeBasedExcludeFilter), mvcOptions.ValidationExcludeFilters[i].OptionType); + Assert.IsType(typeof(DefaultTypeBasedExcludeFilter), mvcOptions.ValidationExcludeFilters[i]); var cancellationTokenFilter - = Assert.IsType(mvcOptions.ValidationExcludeFilters[i++].Instance); + = Assert.IsType(mvcOptions.ValidationExcludeFilters[i++]); Assert.Equal(cancellationTokenFilter.ExcludedType, typeof(System.Threading.CancellationToken)); - Assert.Equal(typeof(DefaultTypeBasedExcludeFilter), mvcOptions.ValidationExcludeFilters[i].OptionType); + Assert.IsType(typeof(DefaultTypeBasedExcludeFilter), mvcOptions.ValidationExcludeFilters[i]); var formFileFilter - = Assert.IsType(mvcOptions.ValidationExcludeFilters[i++].Instance); + = Assert.IsType(mvcOptions.ValidationExcludeFilters[i++]); Assert.Equal(formFileFilter.ExcludedType, typeof(Http.IFormFile)); - Assert.Equal( + Assert.IsType( typeof(DefaultTypeBasedExcludeFilter), - mvcOptions.ValidationExcludeFilters[i].OptionType); + mvcOptions.ValidationExcludeFilters[i]); var formCollectionFilter - = Assert.IsType(mvcOptions.ValidationExcludeFilters[i++].Instance); + = Assert.IsType(mvcOptions.ValidationExcludeFilters[i++]); Assert.Equal(formCollectionFilter.ExcludedType, typeof(Http.IFormCollection)); - Assert.Equal(typeof(DefaultTypeNameBasedExcludeFilter), mvcOptions.ValidationExcludeFilters[i].OptionType); + Assert.IsType(typeof(DefaultTypeNameBasedExcludeFilter), mvcOptions.ValidationExcludeFilters[i]); var xmlNodeFilter = - Assert.IsType(mvcOptions.ValidationExcludeFilters[i++].Instance); + Assert.IsType(mvcOptions.ValidationExcludeFilters[i++]); Assert.Equal(xmlNodeFilter.ExcludedTypeName, "System.Xml.XmlNode"); } } diff --git a/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/BadRequestErrorMessageResultTest.cs b/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/BadRequestErrorMessageResultTest.cs index c24dd53f7f..671a80236a 100644 --- a/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/BadRequestErrorMessageResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/BadRequestErrorMessageResultTest.cs @@ -66,21 +66,15 @@ private IServiceProvider CreateServices() { var services = new Mock(MockBehavior.Strict); - var formatters = new Mock(MockBehavior.Strict); - formatters - .SetupGet(f => f.OutputFormatters) - .Returns(new List() { new JsonOutputFormatter(), }); + var options = new MvcOptions(); + options.OutputFormatters.Add(new JsonOutputFormatter()); - services - .Setup(s => s.GetService(typeof(IOutputFormattersProvider))) - .Returns(formatters.Object); - - var options = new Mock>(); - options.SetupGet(o => o.Options) - .Returns(new MvcOptions()); + var optionsAccessor = new Mock>(); + optionsAccessor.SetupGet(o => o.Options) + .Returns(options); services.Setup(s => s.GetService(typeof(IOptions))) - .Returns(options.Object); + .Returns(optionsAccessor.Object); return services.Object; } diff --git a/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/ExceptionResultTest.cs b/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/ExceptionResultTest.cs index 10fa7aafa2..f805f66d84 100644 --- a/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/ExceptionResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/ExceptionResultTest.cs @@ -66,21 +66,16 @@ private IServiceProvider CreateServices() { var services = new Mock(MockBehavior.Strict); - var formatters = new Mock(MockBehavior.Strict); - formatters - .SetupGet(f => f.OutputFormatters) - .Returns(new List() { new JsonOutputFormatter(), }); + var options = new MvcOptions(); + options.OutputFormatters.Add(new JsonOutputFormatter()); - services - .Setup(s => s.GetService(typeof(IOutputFormattersProvider))) - .Returns(formatters.Object); - - var options = new Mock>(); - options.SetupGet(o => o.Options) - .Returns(new MvcOptions()); + var optionsAccessor = new Mock>(); + optionsAccessor.SetupGet(o => o.Options) + .Returns(options); services.Setup(s => s.GetService(typeof(IOptions))) - .Returns(options.Object); + .Returns(optionsAccessor.Object); + return services.Object; } diff --git a/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/InvalidModelStateResultTest.cs b/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/InvalidModelStateResultTest.cs index 849d76e9b5..4b5d274918 100644 --- a/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/InvalidModelStateResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/InvalidModelStateResultTest.cs @@ -79,21 +79,15 @@ private IServiceProvider CreateServices() { var services = new Mock(MockBehavior.Strict); - var formatters = new Mock(MockBehavior.Strict); - formatters - .SetupGet(f => f.OutputFormatters) - .Returns(new List() { new JsonOutputFormatter(), }); + var options = new MvcOptions(); + options.OutputFormatters.Add(new JsonOutputFormatter()); - services - .Setup(s => s.GetService(typeof(IOutputFormattersProvider))) - .Returns(formatters.Object); - - var options = new Mock>(); - options.SetupGet(o => o.Options) - .Returns(new MvcOptions()); + var optionsAccessor = new Mock>(); + optionsAccessor.SetupGet(o => o.Options) + .Returns(options); services.Setup(s => s.GetService(typeof(IOptions))) - .Returns(options.Object); + .Returns(optionsAccessor.Object); return services.Object; } diff --git a/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/NegotiatedContentResultTest.cs b/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/NegotiatedContentResultTest.cs index c0e4cc21f1..f7491fe002 100644 --- a/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/NegotiatedContentResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/NegotiatedContentResultTest.cs @@ -67,21 +67,15 @@ private IServiceProvider CreateServices() { var services = new Mock(MockBehavior.Strict); - var formatters = new Mock(MockBehavior.Strict); - formatters - .SetupGet(f => f.OutputFormatters) - .Returns(new List() { new JsonOutputFormatter(), }); + var options = new MvcOptions(); + options.OutputFormatters.Add(new JsonOutputFormatter()); - services - .Setup(s => s.GetService(typeof(IOutputFormattersProvider))) - .Returns(formatters.Object); - - var options = new Mock>(); - options.SetupGet(o => o.Options) - .Returns(new MvcOptions()); + var optionsAccessor = new Mock>(); + optionsAccessor.SetupGet(o => o.Options) + .Returns(options); services.Setup(s => s.GetService(typeof(IOptions))) - .Returns(options.Object); + .Returns(optionsAccessor.Object); return services.Object; } diff --git a/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/OkNegotiatedContentResultTest.cs b/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/OkNegotiatedContentResultTest.cs index a01dac91b8..daa5d442e6 100644 --- a/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/OkNegotiatedContentResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ActionResults/OkNegotiatedContentResultTest.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. #if DNX451 -using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Microsoft.AspNet.Http.Core; @@ -41,21 +40,15 @@ private IServiceProvider CreateServices() { var services = new Mock(MockBehavior.Strict); - var formatters = new Mock(MockBehavior.Strict); - formatters - .SetupGet(f => f.OutputFormatters) - .Returns(new List() { new JsonOutputFormatter(), }); + var options = new MvcOptions(); + options.OutputFormatters.Add(new JsonOutputFormatter()); - services - .Setup(s => s.GetService(typeof(IOutputFormattersProvider))) - .Returns(formatters.Object); - - var options = new Mock>(); - options.SetupGet(o => o.Options) - .Returns(new MvcOptions()); + var optionsAccessor = new Mock>(); + optionsAccessor.SetupGet(o => o.Options) + .Returns(options); services.Setup(s => s.GetService(typeof(IOptions))) - .Returns(options.Object); + .Returns(optionsAccessor.Object); return services.Object; } diff --git a/test/WebSites/ContentNegotiationWebSite/Controllers/FallbackOnTypeBasedMatchController.cs b/test/WebSites/ContentNegotiationWebSite/Controllers/FallbackOnTypeBasedMatchController.cs index bc7f6576aa..a48b9127bb 100644 --- a/test/WebSites/ContentNegotiationWebSite/Controllers/FallbackOnTypeBasedMatchController.cs +++ b/test/WebSites/ContentNegotiationWebSite/Controllers/FallbackOnTypeBasedMatchController.cs @@ -3,6 +3,8 @@ using Microsoft.AspNet.Mvc; using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.OptionsModel; + namespace ContentNegotiationWebSite { public class FallbackOnTypeBasedMatchController : Controller @@ -49,9 +51,10 @@ public IActionResult OverrideTheFallback_UsingCustomFormatters(int input) public IActionResult OverrideTheFallback_WithDefaultFormatters(int input) { var objectResult = new ObjectResult(input); - var formattersProvider = ActionContext.HttpContext.RequestServices.GetRequiredService(); + var optionsAccessor = ActionContext.HttpContext.RequestServices + .GetRequiredService>(); objectResult.Formatters.Add(new HttpNotAcceptableOutputFormatter()); - foreach (var formatter in formattersProvider.OutputFormatters) + foreach (var formatter in optionsAccessor.Options.OutputFormatters) { objectResult.Formatters.Add(formatter); } @@ -61,7 +64,7 @@ public IActionResult OverrideTheFallback_WithDefaultFormatters(int input) public IActionResult ReturnString( bool matchFormatterOnObjectType, - [FromServices] IOutputFormattersProvider outputFormattersProvider) + [FromServices] IOptions optionsAccessor) { var objectResult = new ObjectResult("Hello World!"); if (matchFormatterOnObjectType) @@ -69,7 +72,7 @@ public IActionResult ReturnString( objectResult.Formatters.Add(new HttpNotAcceptableOutputFormatter()); } - foreach (var formatter in outputFormattersProvider.OutputFormatters) + foreach (var formatter in optionsAccessor.Options.OutputFormatters) { objectResult.Formatters.Add(formatter); } diff --git a/test/WebSites/ModelBindingWebSite/Controllers/ModelBinderAttribute_ProductController.cs b/test/WebSites/ModelBindingWebSite/Controllers/ModelBinderAttribute_ProductController.cs index 8c9fb6ea97..4f9bb0a661 100644 --- a/test/WebSites/ModelBindingWebSite/Controllers/ModelBinderAttribute_ProductController.cs +++ b/test/WebSites/ModelBindingWebSite/Controllers/ModelBinderAttribute_ProductController.cs @@ -19,24 +19,12 @@ public string GetBinderType_UseModelBinderOnType( return model.BinderType.FullName; } - public string GetBinderType_UseModelBinderProviderOnType( - [ModelBinder(Name = "customPrefix")] ProductWithBinderProviderOnType model) - { - return model.BinderType.FullName; - } - public string GetBinderType_UseModelBinder( [ModelBinder(BinderType = typeof(ProductModelBinder))] Product model) { return model.BinderType.FullName; } - public string GetBinderType_UseModelBinderProvider( - [ModelBinder(BinderType = typeof(ProductModelBinderProvider))] Product model) - { - return model.BinderType.FullName; - } - public string GetBinderType_UseModelBinderOnProperty(Order order) { return order.Product.BinderType.FullName; @@ -60,11 +48,6 @@ public class ProductWithBinderOnType : Product { } - [ModelBinder(BinderType = typeof(ProductModelBinderProvider))] - public class ProductWithBinderProviderOnType : Product - { - } - public class Order { [ModelBinder(BinderType = typeof(ProductModelBinder))] @@ -122,16 +105,5 @@ public async Task BindModelAsync(ModelBindingContext binding return null; } } - - private class ProductModelBinderProvider : IModelBinderProvider - { - public IReadOnlyList ModelBinders - { - get - { - return new[] { new ProductModelBinder() }; - } - } - } } } \ No newline at end of file diff --git a/test/WebSites/ModelBindingWebSite/Startup.cs b/test/WebSites/ModelBindingWebSite/Startup.cs index a41113fa6f..606dbcceb8 100644 --- a/test/WebSites/ModelBindingWebSite/Startup.cs +++ b/test/WebSites/ModelBindingWebSite/Startup.cs @@ -19,7 +19,7 @@ public void ConfigureServices(IServiceCollection services) .Configure(m => { m.MaxModelValidationErrors = 8; - m.ModelBinders.Insert(0, typeof(TestBindingSourceModelBinder)); + m.ModelBinders.Insert(0, new TestBindingSourceModelBinder()); m.AddXmlDataContractSerializerFormatter(); m.ValidationExcludeFilters.Add(typeof(Address));