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 5, 2015
1 parent 6058ad0 commit 29ac06d
Show file tree
Hide file tree
Showing 27 changed files with 200 additions and 202 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 ControllerActionInvoker(
[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 DefaultControllerActionArgumentBinder(

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 @@ await PopulateArgumentsAsync(
actionArguments,
actionDescriptor.Parameters);
return actionArguments;
}
}

private void ActivateProperties(object controller, Type containerType, Dictionary<string, object> properties)
{
Expand Down
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 IReadOnlyList<ApiResponseFormat> 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)
Expand Down
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
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.AspNet.Mvc
/// </summary>
/// <remarks>
/// The <see cref="JsonResult"/> class filter the collection of
/// <see cref="IOutputFormattersProvider.OutputFormatters"/> and use only those which implement
/// <see cref="MvcOptions.OutputFormatters"/> and use only those which implement
/// <see cref="IJsonOutputFormatter"/>.
///
/// To create a custom formatter that can be used by <see cref="JsonResult"/>, derive from
Expand Down
32 changes: 8 additions & 24 deletions src/Microsoft.AspNet.Mvc.Core/ModelBinders/BodyModelBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,39 +17,25 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </summary>
public class BodyModelBinder : BindingSourceModelBinder
{
private readonly ActionContext _actionContext;
private readonly IScopedInstance<ActionBindingContext> _bindingContext;
private readonly IInputFormatterSelector _formatterSelector;
private readonly IValidationExcludeFiltersProvider _bodyValidationExcludeFiltersProvider;

/// <summary>
/// Creates a new <see cref="BodyModelBinder"/>.
/// </summary>
/// <param name="context">An accessor to the <see cref="ActionContext"/>.</param>
/// <param name="bindingContext">An accessor to the <see cref="ActionBindingContext"/>.</param>
/// <param name="selector">The <see cref="IInputFormatterSelector"/>.</param>
/// <param name="bodyValidationExcludeFiltersProvider">
/// The <see cref="IValidationExcludeFiltersProvider"/>.
/// </param>
public BodyModelBinder([NotNull] IScopedInstance<ActionContext> context,
[NotNull] IScopedInstance<ActionBindingContext> bindingContext,
[NotNull] IInputFormatterSelector selector,
[NotNull] IValidationExcludeFiltersProvider bodyValidationExcludeFiltersProvider)
public BodyModelBinder()
: base(BindingSource.Body)
{
_actionContext = context.Value;
_bindingContext = bindingContext;
_formatterSelector = selector;
_bodyValidationExcludeFiltersProvider = bodyValidationExcludeFiltersProvider;
}

/// <inheritdoc />
protected async override Task<ModelBindingResult> BindModelCoreAsync([NotNull] ModelBindingContext bindingContext)
{
var formatters = _bindingContext.Value.InputFormatters;
var requestServices = bindingContext.OperationBindingContext.HttpContext.RequestServices;

var formatterSelector = requestServices.GetRequiredService<IInputFormatterSelector>();
var actionContext = requestServices.GetRequiredService<IScopedInstance<ActionContext>>().Value;
var formatters = requestServices.GetRequiredService<IScopedInstance<ActionBindingContext>>().Value.InputFormatters;

var formatterContext = new InputFormatterContext(_actionContext, bindingContext.ModelType);
var formatter = _formatterSelector.SelectFormatter(formatters.ToList(), formatterContext);
var formatterContext = new InputFormatterContext(actionContext, bindingContext.ModelType);
var formatter = formatterSelector.SelectFormatter(formatters.ToList(), formatterContext);

if (formatter == null)
{
Expand Down
Loading

0 comments on commit 29ac06d

Please sign in to comment.