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

Inject the IRazorViewEngine directory instead of using GetRequiredService #4731

Merged
merged 1 commit into from
May 24, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,31 @@ public class MvcRazorMvcViewOptionsSetup : ConfigureOptions<MvcViewOptions>
/// <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))
/// <param name="razorViewEngine">The <see cref="IRazorViewEngine"/>.</param>
public MvcRazorMvcViewOptionsSetup(IRazorViewEngine razorViewEngine)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's two other places where we use IServiceProvider, but there's slightly more involved since they use GetService instead of GetRequiredService to get an optionally injected feature. We'd have to use constructor overloading to make those work correctly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would our lives here be simpler if we got rid of the ConfigureOptions base class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: base(options => ConfigureMvc(razorViewEngine, options))
{
}

/// <summary>
/// Configures <paramref name="options"/> to use <see cref="RazorViewEngine"/>.
/// </summary>
/// <param name="serviceProvider">The application's <see cref="IServiceProvider"/>.</param>
/// <param name="razorViewEngine">The <see cref="IRazorViewEngine"/>.</param>
/// <param name="options">The <see cref="MvcViewOptions"/> to configure.</param>
public static void ConfigureMvc(
IServiceProvider serviceProvider,
IRazorViewEngine razorViewEngine,
MvcViewOptions options)
{
if (serviceProvider == null)
if (razorViewEngine == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
throw new ArgumentNullException(nameof(razorViewEngine));
}

if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

var razorViewEngine = serviceProvider.GetRequiredService<IRazorViewEngine>();
options.ViewEngines.Add(razorViewEngine);
}
}
Expand Down