[Fixes #4224] Remove use of service locator on ViewExecutor

This commit is contained in:
jacalvar 2016-05-26 10:25:16 -07:00
parent 6a6d2e0d9f
commit 4b5df98bc3
8 changed files with 32 additions and 13 deletions

View File

@ -8,6 +8,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ViewEngines; using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal; using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -31,14 +32,16 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
/// <param name="tempDataFactory">The <see cref="ITempDataDictionaryFactory"/>.</param> /// <param name="tempDataFactory">The <see cref="ITempDataDictionaryFactory"/>.</param>
/// <param name="diagnosticSource">The <see cref="DiagnosticSource"/>.</param> /// <param name="diagnosticSource">The <see cref="DiagnosticSource"/>.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param> /// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
/// <param name="modelMetadataProvider">The <see cref="IModelMetadataProvider"/>.</param>
public PartialViewResultExecutor( public PartialViewResultExecutor(
IOptions<MvcViewOptions> viewOptions, IOptions<MvcViewOptions> viewOptions,
IHttpResponseStreamWriterFactory writerFactory, IHttpResponseStreamWriterFactory writerFactory,
ICompositeViewEngine viewEngine, ICompositeViewEngine viewEngine,
ITempDataDictionaryFactory tempDataFactory, ITempDataDictionaryFactory tempDataFactory,
DiagnosticSource diagnosticSource, DiagnosticSource diagnosticSource,
ILoggerFactory loggerFactory) ILoggerFactory loggerFactory,
: base(viewOptions, writerFactory, viewEngine, tempDataFactory, diagnosticSource) IModelMetadataProvider modelMetadataProvider)
: base(viewOptions, writerFactory, viewEngine, tempDataFactory, diagnosticSource, modelMetadataProvider)
{ {
if (loggerFactory == null) if (loggerFactory == null)
{ {

View File

@ -8,6 +8,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ViewEngines; using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal; using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -31,14 +32,16 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
/// <param name="tempDataFactory">The <see cref="ITempDataDictionaryFactory"/>.</param> /// <param name="tempDataFactory">The <see cref="ITempDataDictionaryFactory"/>.</param>
/// <param name="diagnosticSource">The <see cref="DiagnosticSource"/>.</param> /// <param name="diagnosticSource">The <see cref="DiagnosticSource"/>.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param> /// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
/// <param name="modelMetadataProvider">The <see cref="IModelMetadataProvider"/>.</param>
public ViewResultExecutor( public ViewResultExecutor(
IOptions<MvcViewOptions> viewOptions, IOptions<MvcViewOptions> viewOptions,
IHttpResponseStreamWriterFactory writerFactory, IHttpResponseStreamWriterFactory writerFactory,
ICompositeViewEngine viewEngine, ICompositeViewEngine viewEngine,
ITempDataDictionaryFactory tempDataFactory, ITempDataDictionaryFactory tempDataFactory,
DiagnosticSource diagnosticSource, DiagnosticSource diagnosticSource,
ILoggerFactory loggerFactory) ILoggerFactory loggerFactory,
: base(viewOptions, writerFactory, viewEngine, tempDataFactory, diagnosticSource) IModelMetadataProvider modelMetadataProvider)
: base(viewOptions, writerFactory, viewEngine, tempDataFactory, diagnosticSource, modelMetadataProvider)
{ {
if (loggerFactory == null) if (loggerFactory == null)
{ {

View File

@ -25,6 +25,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
/// </summary> /// </summary>
public static readonly string DefaultContentType = "text/html; charset=utf-8"; public static readonly string DefaultContentType = "text/html; charset=utf-8";
private readonly IModelMetadataProvider _modelMetadataProvider;
/// <summary> /// <summary>
/// Creates a new <see cref="ViewExecutor"/>. /// Creates a new <see cref="ViewExecutor"/>.
/// </summary> /// </summary>
@ -33,12 +35,14 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
/// <param name="viewEngine">The <see cref="ICompositeViewEngine"/>.</param> /// <param name="viewEngine">The <see cref="ICompositeViewEngine"/>.</param>
/// <param name="tempDataFactory">The <see cref="ITempDataDictionaryFactory"/>.</param> /// <param name="tempDataFactory">The <see cref="ITempDataDictionaryFactory"/>.</param>
/// <param name="diagnosticSource">The <see cref="DiagnosticSource"/>.</param> /// <param name="diagnosticSource">The <see cref="DiagnosticSource"/>.</param>
/// <param name="modelMetadataProvider">The <see cref="IModelMetadataProvider" />.</param>
public ViewExecutor( public ViewExecutor(
IOptions<MvcViewOptions> viewOptions, IOptions<MvcViewOptions> viewOptions,
IHttpResponseStreamWriterFactory writerFactory, IHttpResponseStreamWriterFactory writerFactory,
ICompositeViewEngine viewEngine, ICompositeViewEngine viewEngine,
ITempDataDictionaryFactory tempDataFactory, ITempDataDictionaryFactory tempDataFactory,
DiagnosticSource diagnosticSource) DiagnosticSource diagnosticSource,
IModelMetadataProvider modelMetadataProvider)
{ {
if (viewOptions == null) if (viewOptions == null)
{ {
@ -65,11 +69,17 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
throw new ArgumentNullException(nameof(diagnosticSource)); throw new ArgumentNullException(nameof(diagnosticSource));
} }
if (modelMetadataProvider == null)
{
throw new ArgumentNullException(nameof(modelMetadataProvider));
}
ViewOptions = viewOptions.Value; ViewOptions = viewOptions.Value;
WriterFactory = writerFactory; WriterFactory = writerFactory;
ViewEngine = viewEngine; ViewEngine = viewEngine;
TempDataFactory = tempDataFactory; TempDataFactory = tempDataFactory;
DiagnosticSource = diagnosticSource; DiagnosticSource = diagnosticSource;
_modelMetadataProvider = modelMetadataProvider;
} }
/// <summary> /// <summary>
@ -132,9 +142,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
if (viewData == null) if (viewData == null)
{ {
var services = actionContext.HttpContext.RequestServices; viewData = new ViewDataDictionary(_modelMetadataProvider, actionContext.ModelState);
var metadataProvider = services.GetRequiredService<IModelMetadataProvider>();
viewData = new ViewDataDictionary(metadataProvider, actionContext.ModelState);
} }
if (tempData == null) if (tempData == null)

View File

@ -342,7 +342,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
new CompositeViewEngine(options), new CompositeViewEngine(options),
new TempDataDictionaryFactory(new SessionStateTempDataProvider()), new TempDataDictionaryFactory(new SessionStateTempDataProvider()),
diagnosticSource, diagnosticSource,
NullLoggerFactory.Instance); NullLoggerFactory.Instance,
new EmptyModelMetadataProvider());
return viewExecutor; return viewExecutor;
} }

View File

@ -199,7 +199,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
new CompositeViewEngine(options), new CompositeViewEngine(options),
new TempDataDictionaryFactory(new SessionStateTempDataProvider()), new TempDataDictionaryFactory(new SessionStateTempDataProvider()),
new DiagnosticListener("Microsoft.AspNetCore"), new DiagnosticListener("Microsoft.AspNetCore"),
NullLoggerFactory.Instance); NullLoggerFactory.Instance,
new EmptyModelMetadataProvider());
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddSingleton(viewExecutor); services.AddSingleton(viewExecutor);

View File

@ -332,7 +332,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
new CompositeViewEngine(options), new CompositeViewEngine(options),
new TempDataDictionaryFactory(new SessionStateTempDataProvider()), new TempDataDictionaryFactory(new SessionStateTempDataProvider()),
diagnosticSource, diagnosticSource,
NullLoggerFactory.Instance); NullLoggerFactory.Instance,
new EmptyModelMetadataProvider());
return viewExecutor; return viewExecutor;
} }

View File

@ -364,7 +364,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
new TestHttpResponseStreamWriterFactory(), new TestHttpResponseStreamWriterFactory(),
new Mock<ICompositeViewEngine>(MockBehavior.Strict).Object, new Mock<ICompositeViewEngine>(MockBehavior.Strict).Object,
new TempDataDictionaryFactory(new SessionStateTempDataProvider()), new TempDataDictionaryFactory(new SessionStateTempDataProvider()),
diagnosticSource); diagnosticSource,
new EmptyModelMetadataProvider());
} }
} }
} }

View File

@ -227,7 +227,8 @@ namespace Microsoft.AspNetCore.Mvc
new CompositeViewEngine(options), new CompositeViewEngine(options),
new TempDataDictionaryFactory(new SessionStateTempDataProvider()), new TempDataDictionaryFactory(new SessionStateTempDataProvider()),
new DiagnosticListener("Microsoft.AspNetCore"), new DiagnosticListener("Microsoft.AspNetCore"),
NullLoggerFactory.Instance); NullLoggerFactory.Instance,
new EmptyModelMetadataProvider());
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddSingleton(viewExecutor); services.AddSingleton(viewExecutor);