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

Commit

Permalink
Removing MvcViewEngineDescriptor and switching to use OptionsSetup to
Browse files Browse the repository at this point in the history
setup RazorViewEngine.

Fixes #2269
  • Loading branch information
pranavkm committed Jul 29, 2015
1 parent e96b4d7 commit 2d04187
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 578 deletions.
26 changes: 22 additions & 4 deletions src/Microsoft.AspNet.Mvc.Razor/MvcRazorMvcViewOptionsSetup.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;

namespace Microsoft.AspNet.Mvc.Razor
{
/// <summary>
/// Configures <see cref="MvcViewOptions"/> to use <see cref="RazorViewEngine"/>.
/// </summary>
public class MvcRazorMvcViewOptionsSetup : ConfigureOptions<MvcViewOptions>
{
public MvcRazorMvcViewOptionsSetup()
: base(ConfigureMvc)
/// <summary>
/// Initializes a new instance of <see cref="MvcRazorMvcViewOptionsSetup"/>.
/// </summary>
/// <param name="serviceProvider">The application's <see cref="IServiceProvider"/>.</param>
public MvcRazorMvcViewOptionsSetup(IServiceProvider serviceProvider)
: base(options => ConfigureMvc(serviceProvider, options))
{
Order = DefaultOrder.DefaultFrameworkSortOrder;
}

public static void ConfigureMvc(MvcViewOptions options)
/// <summary>
/// Configures <paramref name="options"/> to use <see cref="RazorViewEngine"/>.
/// </summary>
/// <param name="serviceProvider">The application's <see cref="IServiceProvider"/>.</param>
/// <param name="options">The <see cref="MvcViewOptions"/> to configure.</param>
public static void ConfigureMvc(
[NotNull] IServiceProvider serviceProvider,
[NotNull] MvcViewOptions options)
{
options.ViewEngines.Add(typeof(RazorViewEngine));
var razorViewEngine = ActivatorUtilities.CreateInstance<RazorViewEngine>(serviceProvider);
options.ViewEngines.Add(razorViewEngine);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ internal static void AddViewServices(IServiceCollection services)
//
// View Engine and related infrastructure
//
// The provider is inexpensive to initialize and provides ViewEngines that may require request
// specific services.
services.TryAddScoped<ICompositeViewEngine, CompositeViewEngine>();
services.TryAddSingleton<ICompositeViewEngine, CompositeViewEngine>();

// Support for activating ViewDataDictionary
services.TryAddEnumerable(
Expand Down
5 changes: 2 additions & 3 deletions src/Microsoft.AspNet.Mvc.ViewFeatures/MvcViewOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ public class MvcViewOptions
public HtmlHelperOptions HtmlHelperOptions { get;[param: NotNull] set; } = new HtmlHelperOptions();

/// <summary>
/// Gets a list of descriptors that represent <see cref="Rendering.IViewEngine"/> used
/// by this application.
/// Gets a list <see cref="IViewEngine"/>s used by this application.
/// </summary>
public IList<ViewEngineDescriptor> ViewEngines { get; } = new List<ViewEngineDescriptor>();
public IList<IViewEngine> ViewEngines { get; } = new List<IViewEngine>();

/// <summary>
/// Gets a list of <see cref="IClientModelValidatorProvider"/> instances.
Expand Down
5 changes: 4 additions & 1 deletion src/Microsoft.AspNet.Mvc.ViewFeatures/MvcViewOptionsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Sets up default options for <see cref="MvcOptions"/>.
/// Sets up default options for <see cref="MvcViewOptions"/>.
/// </summary>
public class MvcViewOptionsSetup : ConfigureOptions<MvcViewOptions>
{
/// <summary>
/// Initializes a new instance of <see cref="MvcRazorMvcViewOptionsSetup"/>.
/// </summary>
public MvcViewOptionsSetup()
: base(ConfigureMvc)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. 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.Linq;
using Microsoft.Framework.Internal;
Expand All @@ -15,41 +14,14 @@ public class CompositeViewEngine : ICompositeViewEngine
/// <summary>
/// Initializes a new instance of <see cref="CompositeViewEngine"/>.
/// </summary>
/// <param name="optionsAccessor">The options accessor for <see cref="MvcOptions"/>.</param>
/// <param name="typeActivatorCache">As <see cref="ITypeActivatorCache"/> instance that creates
/// an instance of type <see cref="IViewEngine"/>.</param>
/// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance that retrieves services from the
/// service collection.</param>
public CompositeViewEngine(
IOptions<MvcViewOptions> optionsAccessor,
ITypeActivatorCache typeActivatorCache,
IServiceProvider serviceProvider)
/// <param name="optionsAccessor">The options accessor for <see cref="MvcViewOptions"/>.</param>
public CompositeViewEngine(IOptions<MvcViewOptions> optionsAccessor)
{
var viewEngines = new List<IViewEngine>();
foreach (var descriptor in optionsAccessor.Options.ViewEngines)
{
IViewEngine viewEngine;
if (descriptor.ViewEngine != null)
{
viewEngine = descriptor.ViewEngine;
}
else
{
viewEngine = typeActivatorCache.CreateInstance<IViewEngine>(
serviceProvider,
descriptor.ViewEngineType);
}

viewEngines.Add(viewEngine);
}

ViewEngines = viewEngines;
ViewEngines = optionsAccessor.Options.ViewEngines.ToArray();
}

/// <summary>
/// Gets the list of <see cref="IViewEngine"/> this instance of <see cref="CompositeViewEngine"/> delegates to.
/// </summary>
public IReadOnlyList<IViewEngine> ViewEngines { get; }
/// <inheritdoc />
public IReadOnlyList<IViewEngine> ViewEngines { get; }

/// <inheritdoc />
public ViewEngineResult FindPartialView([NotNull] ActionContext context,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;

namespace Microsoft.AspNet.Mvc.Rendering
{
/// <summary>
/// Represents an <see cref="IViewEngine"/> that delegates to one of a collection of view engines.
/// </summary>
public interface ICompositeViewEngine : IViewEngine
{
/// <summary>
/// Gets the list of <see cref="IViewEngine"/> this instance of <see cref="ICompositeViewEngine"/> delegates
/// to.
/// </summary>
IReadOnlyList<IViewEngine> ViewEngines { get; }
}
}
52 changes: 0 additions & 52 deletions src/Microsoft.AspNet.Mvc.ViewFeatures/ViewEngineDescriptor.cs

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions test/Microsoft.AspNet.Mvc.Test/MvcOptionsSetupTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public void Setup_SetsUpViewEngines()
var options = GetOptions<MvcViewOptions>();

// Assert
Assert.Equal(1, options.ViewEngines.Count);
Assert.Equal(typeof(RazorViewEngine), options.ViewEngines[0].ViewEngineType);
var viewEngine = Assert.Single(options.ViewEngines);
Assert.IsType<RazorViewEngine>(viewEngine);
}

[Fact]
Expand Down
Loading

0 comments on commit 2d04187

Please sign in to comment.