Rename ICanHasViewContext to IViewContextAware

Fixes #4073
This commit is contained in:
Pranav K 2016-03-01 15:49:53 -08:00
parent 0b7035ddcf
commit 3aa3799494
10 changed files with 31 additions and 31 deletions

View File

@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Mvc.Localization
/// An <see cref="IViewLocalizer"/> implementation that derives the resource location from the executing view's
/// file path.
/// </summary>
public class ViewLocalizer : IViewLocalizer, ICanHasViewContext
public class ViewLocalizer : IViewLocalizer, IViewContextAware
{
private readonly IHtmlLocalizerFactory _localizerFactory;
private readonly string _applicationName;

View File

@ -9,7 +9,6 @@ using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Internal;
@ -123,7 +122,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
valueAccessor = context =>
{
var serviceProvider = context.HttpContext.RequestServices;
var factory = (IUrlHelperFactory)serviceProvider.GetRequiredService(typeof(IUrlHelperFactory));
var factory = serviceProvider.GetRequiredService<IUrlHelperFactory>();
return factory.GetUrlHelper(context);
};
}
@ -133,11 +132,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
{
var serviceProvider = context.HttpContext.RequestServices;
var value = serviceProvider.GetRequiredService(property.PropertyType);
var canHasViewContext = value as ICanHasViewContext;
if (canHasViewContext != null)
{
canHasViewContext.Contextualize(context);
}
(value as IViewContextAware)?.Contextualize(context);
return value;
};

View File

@ -0,0 +1,20 @@
// 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 Microsoft.AspNetCore.Mvc.Rendering;
namespace Microsoft.AspNetCore.Mvc.ViewFeatures
{
/// <summary>
/// Contract for contextualizing a property activated by a view with the <see cref="ViewContext"/>.
/// </summary>
/// <remarks>This interface is used for contextualizing properties added to a Razor page using <c>@inject</c>.</remarks>
public interface IViewContextAware
{
/// <summary>
/// Contextualizes the instance with the specified <paramref name="viewContext"/>.
/// </summary>
/// <param name="viewContext">The <see cref="ViewContext"/>.</param>
void Contextualize(ViewContext viewContext);
}
}

View File

@ -1,12 +0,0 @@
// 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 Microsoft.AspNetCore.Mvc.Rendering;
namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
{
public interface ICanHasViewContext
{
void Contextualize(ViewContext viewContext);
}
}

View File

@ -120,7 +120,7 @@ namespace Microsoft.AspNetCore.Mvc
writer,
htmlHelperOptions);
(viewComponentHelper as ICanHasViewContext)?.Contextualize(viewContext);
(viewComponentHelper as IViewContextAware)?.Contextualize(viewContext);
var result = await GetViewComponentResult(viewComponentHelper, logger);
result.WriteTo(writer, htmlEncoder);

View File

@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents
/// <summary>
/// Default implementation for <see cref="IViewComponentHelper"/>.
/// </summary>
public class DefaultViewComponentHelper : IViewComponentHelper, ICanHasViewContext
public class DefaultViewComponentHelper : IViewComponentHelper, IViewContextAware
{
private readonly IViewComponentDescriptorCollectionProvider _descriptorProvider;
private readonly HtmlEncoder _htmlEncoder;

View File

@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
/// <summary>
/// Default implementation of <see cref="IHtmlHelper"/>.
/// </summary>
public class HtmlHelper : IHtmlHelper, ICanHasViewContext
public class HtmlHelper : IHtmlHelper, IViewContextAware
{
public static readonly string ValidationInputCssClassName = "input-validation-error";
public static readonly string ValidationInputValidCssClassName = "input-validation-valid";

View File

@ -255,7 +255,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
{
var newHelper = viewContext.HttpContext.RequestServices.GetRequiredService<IHtmlHelper>();
var contextable = newHelper as ICanHasViewContext;
var contextable = newHelper as IViewContextAware;
if (contextable != null)
{
var newViewContext = new ViewContext(viewContext, viewContext.View, viewData, viewContext.Writer);

View File

@ -3,21 +3,17 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.WebEncoders.Testing;
@ -323,6 +319,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
}
}
private class MyService : IViewContextAware
private class HasIncorrectViewDataPropertyType : RazorPage<MyModel>
{
[RazorInject]
@ -385,7 +382,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
{
}
private class MyService : ICanHasViewContext
private class MyService : IViewContextAware
{
public ViewContext ViewContext { get; private set; }

View File

@ -917,7 +917,7 @@ Environment.NewLine;
public string OrderedProperty1 { get; set; }
}
private class StubbyHtmlHelper : IHtmlHelper, ICanHasViewContext
private class StubbyHtmlHelper : IHtmlHelper, IViewContextAware
{
private readonly IHtmlHelper _innerHelper;
@ -969,7 +969,7 @@ Environment.NewLine;
public void Contextualize(ViewContext viewContext)
{
(_innerHelper as ICanHasViewContext)?.Contextualize(viewContext);
(_innerHelper as IViewContextAware)?.Contextualize(viewContext);
}
public IHtmlContent ActionLink(