From 4b5df98bc329c3c47d93d2f801c5809bd375cb50 Mon Sep 17 00:00:00 2001 From: jacalvar Date: Thu, 26 May 2016 10:25:16 -0700 Subject: [PATCH] [Fixes #4224] Remove use of service locator on ViewExecutor --- .../Internal/PartialViewResultExecutor.cs | 7 +++++-- .../Internal/ViewResultExecutor.cs | 7 +++++-- .../ViewFeatures/ViewExecutor.cs | 16 ++++++++++++---- .../Internal/PartialViewResultExecutorTest.cs | 3 ++- .../Internal/PartialViewResultTest.cs | 3 ++- .../Internal/ViewResultExecutorTest.cs | 3 ++- .../ViewFeatures/ViewExecutorTest.cs | 3 ++- .../ViewResultTest.cs | 3 ++- 8 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PartialViewResultExecutor.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PartialViewResultExecutor.cs index a06d1f4e5e..67cb50784d 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PartialViewResultExecutor.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PartialViewResultExecutor.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Internal; +using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ViewEngines; using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal; using Microsoft.Extensions.Logging; @@ -31,14 +32,16 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal /// The . /// The . /// The . + /// The . public PartialViewResultExecutor( IOptions viewOptions, IHttpResponseStreamWriterFactory writerFactory, ICompositeViewEngine viewEngine, ITempDataDictionaryFactory tempDataFactory, DiagnosticSource diagnosticSource, - ILoggerFactory loggerFactory) - : base(viewOptions, writerFactory, viewEngine, tempDataFactory, diagnosticSource) + ILoggerFactory loggerFactory, + IModelMetadataProvider modelMetadataProvider) + : base(viewOptions, writerFactory, viewEngine, tempDataFactory, diagnosticSource, modelMetadataProvider) { if (loggerFactory == null) { diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewResultExecutor.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewResultExecutor.cs index 217f97d8c3..88b5071b91 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewResultExecutor.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewResultExecutor.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Internal; +using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ViewEngines; using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal; using Microsoft.Extensions.Logging; @@ -31,14 +32,16 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal /// The . /// The . /// The . + /// The . public ViewResultExecutor( IOptions viewOptions, IHttpResponseStreamWriterFactory writerFactory, ICompositeViewEngine viewEngine, ITempDataDictionaryFactory tempDataFactory, DiagnosticSource diagnosticSource, - ILoggerFactory loggerFactory) - : base(viewOptions, writerFactory, viewEngine, tempDataFactory, diagnosticSource) + ILoggerFactory loggerFactory, + IModelMetadataProvider modelMetadataProvider) + : base(viewOptions, writerFactory, viewEngine, tempDataFactory, diagnosticSource, modelMetadataProvider) { if (loggerFactory == null) { diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewExecutor.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewExecutor.cs index 4fe015770c..e60e75e07a 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewExecutor.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewExecutor.cs @@ -25,6 +25,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures /// public static readonly string DefaultContentType = "text/html; charset=utf-8"; + private readonly IModelMetadataProvider _modelMetadataProvider; + /// /// Creates a new . /// @@ -33,12 +35,14 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures /// The . /// The . /// The . + /// The . public ViewExecutor( IOptions viewOptions, IHttpResponseStreamWriterFactory writerFactory, ICompositeViewEngine viewEngine, ITempDataDictionaryFactory tempDataFactory, - DiagnosticSource diagnosticSource) + DiagnosticSource diagnosticSource, + IModelMetadataProvider modelMetadataProvider) { if (viewOptions == null) { @@ -65,11 +69,17 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures throw new ArgumentNullException(nameof(diagnosticSource)); } + if (modelMetadataProvider == null) + { + throw new ArgumentNullException(nameof(modelMetadataProvider)); + } + ViewOptions = viewOptions.Value; WriterFactory = writerFactory; ViewEngine = viewEngine; TempDataFactory = tempDataFactory; DiagnosticSource = diagnosticSource; + _modelMetadataProvider = modelMetadataProvider; } /// @@ -132,9 +142,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures if (viewData == null) { - var services = actionContext.HttpContext.RequestServices; - var metadataProvider = services.GetRequiredService(); - viewData = new ViewDataDictionary(metadataProvider, actionContext.ModelState); + viewData = new ViewDataDictionary(_modelMetadataProvider, actionContext.ModelState); } if (tempData == null) diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultExecutorTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultExecutorTest.cs index a6032ae2a5..aef76176d2 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultExecutorTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultExecutorTest.cs @@ -342,7 +342,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal new CompositeViewEngine(options), new TempDataDictionaryFactory(new SessionStateTempDataProvider()), diagnosticSource, - NullLoggerFactory.Instance); + NullLoggerFactory.Instance, + new EmptyModelMetadataProvider()); return viewExecutor; } diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultTest.cs index 36a89d3215..cf5adf0247 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultTest.cs @@ -199,7 +199,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal new CompositeViewEngine(options), new TempDataDictionaryFactory(new SessionStateTempDataProvider()), new DiagnosticListener("Microsoft.AspNetCore"), - NullLoggerFactory.Instance); + NullLoggerFactory.Instance, + new EmptyModelMetadataProvider()); var services = new ServiceCollection(); services.AddSingleton(viewExecutor); diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/ViewResultExecutorTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/ViewResultExecutorTest.cs index e32b246058..cff64489ca 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/ViewResultExecutorTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/ViewResultExecutorTest.cs @@ -332,7 +332,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal new CompositeViewEngine(options), new TempDataDictionaryFactory(new SessionStateTempDataProvider()), diagnosticSource, - NullLoggerFactory.Instance); + NullLoggerFactory.Instance, + new EmptyModelMetadataProvider()); return viewExecutor; } diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/ViewExecutorTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/ViewExecutorTest.cs index 03fc01229d..e607623625 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/ViewExecutorTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/ViewExecutorTest.cs @@ -364,7 +364,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures new TestHttpResponseStreamWriterFactory(), new Mock(MockBehavior.Strict).Object, new TempDataDictionaryFactory(new SessionStateTempDataProvider()), - diagnosticSource); + diagnosticSource, + new EmptyModelMetadataProvider()); } } } diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewResultTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewResultTest.cs index ece5c57dcd..d9bf6609ae 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewResultTest.cs @@ -227,7 +227,8 @@ namespace Microsoft.AspNetCore.Mvc new CompositeViewEngine(options), new TempDataDictionaryFactory(new SessionStateTempDataProvider()), new DiagnosticListener("Microsoft.AspNetCore"), - NullLoggerFactory.Instance); + NullLoggerFactory.Instance, + new EmptyModelMetadataProvider()); var services = new ServiceCollection(); services.AddSingleton(viewExecutor);