Change IViewEngine.FindView to be synchronous

This commit is contained in:
Pranav K 2014-04-02 16:19:06 -07:00
parent 2e0bed750e
commit 75dae948b2
14 changed files with 39 additions and 50 deletions

View File

@ -19,14 +19,14 @@ namespace Microsoft.AspNet.Mvc
_viewEngine = viewEngine;
}
public string ViewName {get; set; }
public string ViewName { get; set; }
public ViewDataDictionary ViewData { get; set; }
public async Task ExecuteResultAsync([NotNull] ActionContext context)
{
var viewName = ViewName ?? context.ActionDescriptor.Name;
var view = await FindView(context.RouteValues, viewName);
var view = FindView(context.RouteValues, viewName);
using (view as IDisposable)
{
@ -39,10 +39,9 @@ namespace Microsoft.AspNet.Mvc
}
}
private async Task<IView> FindView([NotNull] IDictionary<string, object> context,[NotNull] string viewName)
private IView FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName)
{
var result = await _viewEngine.FindView(context, viewName);
var result = _viewEngine.FindView(context, viewName);
return result.View;
}

View File

@ -68,17 +68,16 @@ namespace Microsoft.AspNet.Mvc
_viewName);
}
var view = await FindView(context.ViewContext.ViewEngineContext, qualifiedViewName);
var view = FindView(context.ViewContext.ViewEngineContext, qualifiedViewName);
using (view as IDisposable)
{
await view.RenderAsync(childViewContext);
}
}
private async Task<IView> FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName)
private IView FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName)
{
var result = await _viewEngine.FindView(context, viewName);
var result = _viewEngine.FindView(context, viewName);
return result.View;
}
}

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Microsoft.AspNet.FileSystems;
namespace Microsoft.AspNet.Mvc.Razor
@ -14,7 +13,7 @@ namespace Microsoft.AspNet.Mvc.Razor
_cache = new ConcurrentDictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
}
public async Task<CompilationResult> GetOrAdd(IFileInfo file, Func<Task<CompilationResult>> compile)
public CompilationResult GetOrAdd(IFileInfo file, Func<CompilationResult> compile)
{
// Generate a content id
string contentId = file.PhysicalPath + '|' + file.LastModified.Ticks;
@ -22,7 +21,7 @@ namespace Microsoft.AspNet.Mvc.Razor
Type compiledType;
if (!_cache.TryGetValue(contentId, out compiledType))
{
CompilationResult result = await compile();
CompilationResult result = compile();
_cache.TryAdd(contentId, result.CompiledType);
return result;

View File

@ -5,6 +5,6 @@ namespace Microsoft.AspNet.Mvc.Razor
{
public interface ICompilationService
{
Task<CompilationResult> Compile(string content);
CompilationResult Compile(string content);
}
}

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
_libraryManager = libraryManager;
}
public Task<CompilationResult> Compile(string content)
public CompilationResult Compile(string content)
{
var syntaxTrees = new[] { CSharpSyntaxTree.ParseText(content) };
var targetFramework = _environment.TargetFramework;
@ -51,14 +51,14 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
var messages = result.Diagnostics.Where(IsError).Select(d => GetCompilationMessage(formatter, d)).ToList();
return Task.FromResult(CompilationResult.Failed(content, messages));
return CompilationResult.Failed(content, messages);
}
var type = _loader.LoadBytes(ms.ToArray(), pdb.ToArray())
.GetExportedTypes()
.First();
return Task.FromResult(CompilationResult.Successful(String.Empty, type));
return CompilationResult.Successful(String.Empty, type);
}
}
}

View File

@ -1,10 +1,9 @@
using System.Threading.Tasks;
using Microsoft.AspNet.FileSystems;
using Microsoft.AspNet.FileSystems;
namespace Microsoft.AspNet.Mvc.Razor
{
public interface IRazorCompilationService
{
Task<CompilationResult> Compile(IFileInfo fileInfo);
CompilationResult Compile(IFileInfo fileInfo);
}
}

View File

@ -2,7 +2,6 @@
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.FileSystems;
using Microsoft.AspNet.Razor;
using Microsoft.Net.Runtime;
@ -27,13 +26,13 @@ namespace Microsoft.AspNet.Mvc.Razor
_appRoot = EnsureTrailingSlash(environment.ApplicationBasePath);
}
public Task<CompilationResult> Compile([NotNull]IFileInfo file)
public CompilationResult Compile([NotNull]IFileInfo file)
{
return _cache.GetOrAdd(file, () => CompileCore(file));
}
// TODO: Make this internal
public async Task<CompilationResult> CompileCore(IFileInfo file)
public CompilationResult CompileCore(IFileInfo file)
{
GeneratorResults results;
using (Stream inputStream = file.CreateReadStream())
@ -49,7 +48,7 @@ namespace Microsoft.AspNet.Mvc.Razor
throw new CompilationFailedException(messages, results.GeneratedCode);
}
return await _baseCompilationService.Compile(results.GeneratedCode);
return _baseCompilationService.Compile(results.GeneratedCode);
}
private static string EnsureTrailingSlash([NotNull]string path)

View File

@ -74,7 +74,7 @@ namespace Microsoft.AspNet.Mvc.Razor
private async Task RenderLayoutAsync(ViewContext context, string bodyContent)
{
var virtualPathFactory = context.ServiceProvider.GetService<IVirtualPathViewFactory>();
var layoutView = (RazorView)(await virtualPathFactory.CreateInstance(Layout));
var layoutView = (RazorView)virtualPathFactory.CreateInstance(Layout);
if (layoutView == null)
{

View File

@ -1,11 +1,10 @@

using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering;
namespace Microsoft.AspNet.Mvc.Razor
{
public interface IVirtualPathViewFactory
{
Task<IView> CreateInstance(string virtualPath);
IView CreateInstance(string virtualPath);
}
}

View File

@ -28,10 +28,10 @@ namespace Microsoft.AspNet.Mvc.Razor
get { return _viewLocationFormats; }
}
public async Task<ViewEngineResult> FindView([NotNull] IDictionary<string, object> context,
[NotNull] string viewName)
public ViewEngineResult FindView([NotNull] IDictionary<string, object> context,
[NotNull] string viewName)
{
var viewEngineResult = await CreateViewEngineResult(context, viewName);
var viewEngineResult = CreateViewEngineResult(context, viewName);
var errorMessage = Resources.FormatViewEngine_ViewNotFound(
viewName,
ToLocationString(viewEngineResult.SearchedLocations));
@ -41,10 +41,10 @@ namespace Microsoft.AspNet.Mvc.Razor
return viewEngineResult;
}
public async Task<ViewEngineResult> FindPartialView([NotNull] IDictionary<string, object> context,
[NotNull] string partialViewName)
public ViewEngineResult FindPartialView([NotNull] IDictionary<string, object> context,
[NotNull] string partialViewName)
{
var viewEngineResult = await CreateViewEngineResult(context, partialViewName);
var viewEngineResult = CreateViewEngineResult(context, partialViewName);
var errorMessage = Resources.FormatViewEngine_PartialViewNotFound(
partialViewName,
ToLocationString(viewEngineResult.SearchedLocations));
@ -54,14 +54,14 @@ namespace Microsoft.AspNet.Mvc.Razor
return viewEngineResult;
}
private async Task<ViewEngineResult> CreateViewEngineResult([NotNull] IDictionary<string, object> context,
[NotNull] string viewName)
private ViewEngineResult CreateViewEngineResult([NotNull] IDictionary<string, object> context,
[NotNull] string viewName)
{
var nameRepresentsPath = IsSpecificPath(viewName);
if (nameRepresentsPath)
{
var view = await _virtualPathFactory.CreateInstance(viewName);
var view = _virtualPathFactory.CreateInstance(viewName);
return view != null ? ViewEngineResult.Found(viewName, view) :
ViewEngineResult.NotFound(viewName, new[] { viewName });
}
@ -74,7 +74,7 @@ namespace Microsoft.AspNet.Mvc.Razor
for (int i = 0; i < _viewLocationFormats.Length; i++)
{
var path = String.Format(CultureInfo.InvariantCulture, _viewLocationFormats[i], viewName, controllerName, areaName);
IView view = await _virtualPathFactory.CreateInstance(path);
IView view = _virtualPathFactory.CreateInstance(path);
if (view != null)
{
return ViewEngineResult.Found(viewName, view);

View File

@ -19,12 +19,12 @@ namespace Microsoft.AspNet.Mvc.Razor
_compilationService = compilationService;
}
public async Task<IView> CreateInstance([NotNull]string virtualPath)
public IView CreateInstance([NotNull]string virtualPath)
{
IFileInfo fileInfo;
if (_fileSystem.TryGetFileInfo(virtualPath, out fileInfo))
{
CompilationResult result = await _compilationService.Compile(fileInfo);
CompilationResult result = _compilationService.Compile(fileInfo);
return (IView)Activator.CreateInstance(result.CompiledType);
}

View File

@ -180,7 +180,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
Writer = writer
};
var viewEngineResult = await _viewEngine.FindPartialView(newViewContext.ViewEngineContext, partialViewName);
var viewEngineResult = _viewEngine.FindPartialView(newViewContext.ViewEngineContext, partialViewName);
await viewEngineResult.View.RenderAsync(newViewContext);
}

View File

@ -1,11 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Mvc.Rendering
{
public interface IViewEngine
{
Task<ViewEngineResult> FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName);
Task<ViewEngineResult> FindPartialView([NotNull] IDictionary<string, object> context, [NotNull] string partialViewName);
ViewEngineResult FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName);
ViewEngineResult FindPartialView([NotNull] IDictionary<string, object> context, [NotNull] string partialViewName);
}
}

View File

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.AspNet.FileSystems;
using Microsoft.AspNet.Razor;
using Microsoft.AspNet.Razor.Generator.Compiler;
@ -31,7 +26,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
.Verifiable();
var compiler = new Mock<ICompilationService>();
compiler.Setup(c => c.Compile(It.IsAny<string>()))
.Returns(Task.FromResult(CompilationResult.Successful("", typeof(RazorCompilationServiceTest))));
.Returns(CompilationResult.Successful("", typeof(RazorCompilationServiceTest)));
var razorService = new RazorCompilationService(env.Object, compiler.Object, host.Object);
var fileInfo = new Mock<IFileInfo>();
@ -39,7 +34,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
fileInfo.Setup(f => f.CreateReadStream()).Returns(Stream.Null);
// Act
razorService.CompileCore(fileInfo.Object).Wait();
razorService.CompileCore(fileInfo.Object);
// Assert
host.Verify();