Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
* Simplify MvcOptions
Browse files Browse the repository at this point in the history
* Remove facades for accessing Options<T> and pass options to the invoker

Fixes #2266
Fixes #2269
  • Loading branch information
pranavkm committed Apr 3, 2015
1 parent 6058ad0 commit 5fb40e8
Show file tree
Hide file tree
Showing 119 changed files with 870 additions and 4,163 deletions.
4 changes: 3 additions & 1 deletion src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -115,7 +116,8 @@ private IOutputFormatter SelectFormatter(ObjectResult objectResult, OutputFormat
.ActionContext
.HttpContext
.RequestServices
.GetRequiredService<IOutputFormattersProvider>()
.GetRequiredService<IOptions<MvcOptions>>()
.Options
.OutputFormatters
.OfType<IJsonOutputFormatter>()
.ToArray();
Expand Down
10 changes: 6 additions & 4 deletions src/Microsoft.AspNet.Mvc.Core/ActionResults/ObjectResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,12 @@ private IEnumerable<IOutputFormatter> GetDefaultFormatters(ActionContext context
IEnumerable<IOutputFormatter> formatters = null;
if (Formatters == null || Formatters.Count == 0)
{
formatters = context.HttpContext
.RequestServices
.GetRequiredService<IOutputFormattersProvider>()
.OutputFormatters;
formatters = context
.HttpContext
.RequestServices
.GetRequiredService<IOptions<MvcOptions>>()
.Options
.OutputFormatters;
}
else
{
Expand Down
16 changes: 8 additions & 8 deletions src/Microsoft.AspNet.Mvc.Core/ControllerActionInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ public class ControllerActionInvoker : FilterActionInvoker
[NotNull] IReadOnlyList<IFilterProvider> filterProviders,
[NotNull] IControllerFactory controllerFactory,
[NotNull] ControllerActionDescriptor descriptor,
[NotNull] IInputFormattersProvider inputFormatterProvider,
[NotNull] IReadOnlyList<IInputFormatter> inputFormatters,
[NotNull] IControllerActionArgumentBinder controllerActionArgumentBinder,
[NotNull] IModelBinderProvider modelBinderProvider,
[NotNull] IModelValidatorProviderProvider modelValidatorProviderProvider,
[NotNull] IValueProviderFactoryProvider valueProviderFactoryProvider,
[NotNull] IReadOnlyList<IModelBinder> modelBinders,
[NotNull] IReadOnlyList<IModelValidatorProvider> modelValidatorProviders,
[NotNull] IReadOnlyList<IValueProviderFactory> valueProviderFactories,
[NotNull] IScopedInstance<ActionBindingContext> actionBindingContextAccessor,
[NotNull] ITempDataDictionary tempData)
: base(
actionContext,
filterProviders,
inputFormatterProvider,
modelBinderProvider,
modelValidatorProviderProvider,
valueProviderFactoryProvider,
inputFormatters,
modelBinders,
modelValidatorProviders,
valueProviderFactories,
actionBindingContextAccessor)
{
_descriptor = descriptor;
Expand Down
30 changes: 14 additions & 16 deletions src/Microsoft.AspNet.Mvc.Core/ControllerActionInvokerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<IInputFormatter> _inputFormatters;
private readonly IReadOnlyList<IModelBinder> _modelBinders;
private readonly IReadOnlyList<IModelValidatorProvider> _modelValidatorProviders;
private readonly IReadOnlyList<IValueProviderFactory> _valueProviderFactories;
private readonly IScopedInstance<ActionBindingContext> _actionBindingContextAccessor;
private readonly ITempDataDictionary _tempData;

public ControllerActionInvokerProvider(
IControllerFactory controllerFactory,
IInputFormattersProvider inputFormattersProvider,
IEnumerable<IFilterProvider> filterProviders,
IControllerActionArgumentBinder argumentBinder,
IModelBinderProvider modelBinderProvider,
IModelValidatorProviderProvider modelValidationProviderProvider,
IValueProviderFactoryProvider valueProviderFactoryProvider,
IOptions<MvcOptions> optionsAccessor,
IScopedInstance<ActionBindingContext> actionBindingContextAccessor,
ITempDataDictionary tempData)
{
_controllerFactory = controllerFactory;
_inputFormattersProvider = inputFormattersProvider;
_filterProviders = filterProviders.OrderBy(item => item.Order).ToArray();
_argumentBinder = argumentBinder;
_modelBinderProvider = modelBinderProvider;
_modelValidationProviderProvider = modelValidationProviderProvider;
_valueProviderFactoryProvider = valueProviderFactoryProvider;
_inputFormatters = optionsAccessor.Options.InputFormatters.ToArray();
_modelBinders = optionsAccessor.Options.ModelBinders.ToArray();
_modelValidatorProviders = optionsAccessor.Options.ModelValidatorProviders.ToArray();
_valueProviderFactories = optionsAccessor.Options.ValueProviderFactories.ToArray();
_actionBindingContextAccessor = actionBindingContextAccessor;
_tempData = tempData;
}
Expand All @@ -60,11 +58,11 @@ public void OnProvidersExecuting([NotNull] ActionInvokerProviderContext context)
_filterProviders,
_controllerFactory,
actionDescriptor,
_inputFormattersProvider,
_inputFormatters,
_argumentBinder,
_modelBinderProvider,
_modelValidationProviderProvider,
_valueProviderFactoryProvider,
_modelBinders,
_modelValidatorProviders,
_valueProviderFactories,
_actionBindingContextAccessor,
_tempData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,7 +35,7 @@ public class DefaultControllerActionArgumentBinder : IControllerActionArgumentBi

public async Task<IDictionary<string, object>> BindActionArgumentsAsync(
ActionContext actionContext,
ActionBindingContext actionBindingContext,
ActionBindingContext actionBindingContext,
object controller)
{
var actionDescriptor = actionContext.ActionDescriptor as ControllerActionDescriptor;
Expand Down Expand Up @@ -66,7 +64,7 @@ public class DefaultControllerActionArgumentBinder : IControllerActionArgumentBi
actionArguments,
actionDescriptor.Parameters);
return actionArguments;
}
}

private void ActivateProperties(object controller, Type containerType, Dictionary<string, object> properties)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,21 +22,23 @@ namespace Microsoft.AspNet.Mvc.Description
/// </summary>
public class DefaultApiDescriptionProvider : IApiDescriptionProvider
{
private readonly IOutputFormattersProvider _formattersProvider;
private readonly IList<IOutputFormatter> _outputFormatters;
private readonly IModelMetadataProvider _modelMetadataProvider;
private readonly IInlineConstraintResolver _constraintResolver;

/// <summary>
/// Creates a new instance of <see cref="DefaultApiDescriptionProvider"/>.
/// </summary>
/// <param name="formattersProvider">The <see cref="IOutputFormattersProvider"/>.</param>
/// <param name="optionsAccessor">The accessor for <see cref="MvcOptions"/>.</param>
/// <param name="constraintResolver">The <see cref="IInlineConstraintResolver"/> used for resolving inline
/// constraints.</param>
/// <param name="modelMetadataProvider">The <see cref="IModelMetadataProvider"/>.</param>
public DefaultApiDescriptionProvider(
IOutputFormattersProvider formattersProvider,
IOptions<MvcOptions> optionsAccessor,
IInlineConstraintResolver constraintResolver,
IModelMetadataProvider modelMetadataProvider)
{
_formattersProvider = formattersProvider;
_outputFormatters = optionsAccessor.Options.OutputFormatters;
_constraintResolver = constraintResolver;
_modelMetadataProvider = modelMetadataProvider;
}
Expand Down Expand Up @@ -316,10 +319,9 @@ private string GetRelativePath(RouteTemplate parsedTemplate)
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.ModelBinding.Validation;

namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Extensions for <see cref="IList{IExcludeTypeValidationFilter}"/>.
/// </summary>
public static class ExcludeTypeValidationFilterExtensions
{
/// <summary>
/// Adds a descriptor to the specified <paramref name="excludeTypeValidationFilters" /> that excludes the properties of
/// the <see cref="Type"/> specified and its derived types from validaton.
/// </summary>
/// <param name="excludeTypeValidationFilters">A list of <see cref="IExcludeTypeValidationFilter"/> which are used to
/// get a collection of exclude filters to be applied for filtering model properties during validation.
/// </param>
/// <param name="type"><see cref="Type"/> which should be excluded from validation.</param>
public static void Add(this IList<IExcludeTypeValidationFilter> excludeTypeValidationFilters, Type type)
{
var typeBasedExcludeFilter = new DefaultTypeBasedExcludeFilter(type);
excludeTypeValidationFilters.Add(typeBasedExcludeFilter);
}

/// <summary>
/// Adds a descriptor to the specified <paramref name="excludeTypeValidationFilters" /> that excludes the properties of
/// the type specified and its derived types from validaton.
/// </summary>
/// <param name="excludeTypeValidationFilters">A list of <see cref="IExcludeTypeValidationFilter"/> which are used to
/// get a collection of exclude filters to be applied for filtering model properties during validation.
/// </param>
/// <param name="typeFullName">Full name of the type which should be excluded from validation.</param>
public static void Add(this IList<IExcludeTypeValidationFilter> excludeTypeValidationFilters, string typeFullName)
{
var filter = new DefaultTypeNameBasedExcludeFilter(typeFullName);
excludeTypeValidationFilters.Add(filter);
}
}
}
37 changes: 17 additions & 20 deletions src/Microsoft.AspNet.Mvc.Core/FilterActionInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ namespace Microsoft.AspNet.Mvc.Core
public abstract class FilterActionInvoker : IActionInvoker
{
private readonly IReadOnlyList<IFilterProvider> _filterProviders;
private readonly IInputFormattersProvider _inputFormatterProvider;
private readonly IModelBinderProvider _modelBinderProvider;
private readonly IModelValidatorProviderProvider _modelValidatorProviderProvider;
private readonly IValueProviderFactoryProvider _valueProviderFactoryProvider;
private readonly IReadOnlyList<IInputFormatter> _inputFormatters;
private readonly IReadOnlyList<IModelBinder> _modelBinders;
private readonly IReadOnlyList<IModelValidatorProvider> _modelValidatorProviders;
private readonly IReadOnlyList<IValueProviderFactory> _valueProviderFactories;

private readonly IScopedInstance<ActionBindingContext> _actionBindingContextAccessor;

private IFilter[] _filters;
Expand All @@ -41,19 +42,19 @@ public abstract class FilterActionInvoker : IActionInvoker
public FilterActionInvoker(
[NotNull] ActionContext actionContext,
[NotNull] IReadOnlyList<IFilterProvider> filterProviders,
[NotNull] IInputFormattersProvider inputFormatterProvider,
[NotNull] IModelBinderProvider modelBinderProvider,
[NotNull] IModelValidatorProviderProvider modelValidatorProviderProvider,
[NotNull] IValueProviderFactoryProvider valueProviderFactoryProvider,
[NotNull] IReadOnlyList<IInputFormatter> inputFormatters,
[NotNull] IReadOnlyList<IModelBinder> modelBinders,
[NotNull] IReadOnlyList<IModelValidatorProvider> modelValidatorProviders,
[NotNull] IReadOnlyList<IValueProviderFactory> valueProviderFactories,
[NotNull] IScopedInstance<ActionBindingContext> actionBindingContextAccessor)
{
ActionContext = actionContext;

_filterProviders = filterProviders;
_inputFormatterProvider = inputFormatterProvider;
_modelBinderProvider = modelBinderProvider;
_modelValidatorProviderProvider = modelValidatorProviderProvider;
_valueProviderFactoryProvider = valueProviderFactoryProvider;
_inputFormatters = inputFormatters;
_modelBinders = modelBinders;
_modelValidatorProviders = modelValidatorProviders;
_valueProviderFactories = valueProviderFactories;
_actionBindingContextAccessor = actionBindingContextAccessor;
}

Expand Down Expand Up @@ -206,14 +207,10 @@ private async Task InvokeAllResourceFiltersAsync()

var context = new ResourceExecutingContext(ActionContext, _filters);

context.InputFormatters = new List<IInputFormatter>(_inputFormatterProvider.InputFormatters);
context.ModelBinders = new List<IModelBinder>(_modelBinderProvider.ModelBinders);

context.ValidatorProviders = new List<IModelValidatorProvider>(
_modelValidatorProviderProvider.ModelValidatorProviders);

context.ValueProviderFactories = new List<IValueProviderFactory>(
_valueProviderFactoryProvider.ValueProviderFactories);
context.InputFormatters = new List<IInputFormatter>(_inputFormatters);
context.ModelBinders = new List<IModelBinder>(_modelBinders);
context.ValidatorProviders = new List<IModelValidatorProvider>(_modelValidatorProviders);
context.ValueProviderFactories = new List<IValueProviderFactory>(_valueProviderFactories);

_resourceExecutingContext = context;
await InvokeResourceFilterAsync();
Expand Down

This file was deleted.

0 comments on commit 5fb40e8

Please sign in to comment.