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

Commit

Permalink
Part of #2151 - Remove [Activate] from TagHelpers
Browse files Browse the repository at this point in the history
This change removes [Activate] support from TagHelpers. TagHelpers which
need access to context should use [ViewContext] to have it injected. To
access services, use constructor injection.
  • Loading branch information
rynowak committed May 22, 2015
1 parent 5809995 commit 2cc71e4
Show file tree
Hide file tree
Showing 42 changed files with 692 additions and 580 deletions.
17 changes: 17 additions & 0 deletions src/Microsoft.AspNet.Mvc.Core/ViewContextAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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;

namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Specifies that a tag helper property should be set with the current
/// <see cref="ViewContext"/> when creating the tag helper. The property must have a public
/// set method.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class ViewContextAttribute : Attribute
{
}
}
36 changes: 7 additions & 29 deletions src/Microsoft.AspNet.Mvc.Razor/DefaultTagHelperActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ public DefaultTagHelperActivator()
_injectActions = new ConcurrentDictionary<Type, PropertyActivator<ViewContext>[]>();
_getPropertiesToActivate = type =>
PropertyActivator<ViewContext>.GetPropertiesToActivate(
type, typeof(ActivateAttribute), CreateActivateInfo);
type,
typeof(ViewContextAttribute),
CreateActivateInfo);
}

/// <inheritdoc />
public void Activate<TTagHelper>([NotNull] TTagHelper tagHelper, [NotNull] ViewContext context)
where TTagHelper : ITagHelper
{
var propertiesToActivate = _injectActions.GetOrAdd(tagHelper.GetType(),
_getPropertiesToActivate);
var propertiesToActivate = _injectActions.GetOrAdd(
tagHelper.GetType(),
_getPropertiesToActivate);

for (var i = 0; i < propertiesToActivate.Length; i++)
{
Expand All @@ -60,32 +63,7 @@ private static void InitializeTagHelper<TTagHelper>(TTagHelper tagHelper, ViewCo

private static PropertyActivator<ViewContext> CreateActivateInfo(PropertyInfo property)
{
Func<ViewContext, object> valueAccessor;
var propertyType = property.PropertyType;

if (propertyType == typeof(ViewContext))
{
valueAccessor = viewContext => viewContext;
}
else if (propertyType == typeof(ViewDataDictionary))
{
valueAccessor = viewContext => viewContext.ViewData;
}
else
{
valueAccessor = (viewContext) =>
{
var serviceProvider = viewContext.HttpContext.RequestServices;
var service = serviceProvider.GetRequiredService(propertyType);
var contextable = service as ICanHasViewContext;
contextable?.Contextualize(viewContext);
return service;
};
}

return new PropertyActivator<ViewContext>(property, valueAccessor);
return new PropertyActivator<ViewContext>(property, viewContext => viewContext);
}
}
}
25 changes: 21 additions & 4 deletions src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public abstract class RazorPage : IRazorPage
private TextWriter _originalWriter;
private IUrlHelper _urlHelper;
private ITagHelperActivator _tagHelperActivator;
private ITypeActivatorCache _typeActivatorCache;
private bool _renderedBody;

public RazorPage()
Expand Down Expand Up @@ -143,14 +144,28 @@ private ITagHelperActivator TagHelperActivator
{
if (_tagHelperActivator == null)
{
_tagHelperActivator =
ViewContext.HttpContext.RequestServices.GetRequiredService<ITagHelperActivator>();
var services = ViewContext.HttpContext.RequestServices;
_tagHelperActivator = services.GetRequiredService<ITagHelperActivator>();
}

return _tagHelperActivator;
}
}

private ITypeActivatorCache TypeActivatorCache
{
get
{
if (_typeActivatorCache == null)
{
var services = ViewContext.HttpContext.RequestServices;
_typeActivatorCache = services.GetRequiredService<ITypeActivatorCache>();
}

return _typeActivatorCache;
}
}

/// <summary>
/// Format an error message about using an indexer when the tag helper property is <c>null</c>.
/// </summary>
Expand All @@ -177,9 +192,11 @@ public static string InvalidTagHelperIndexerAssignment(
/// <remarks>
/// <typeparamref name="TTagHelper"/> must have a parameterless constructor.
/// </remarks>
public TTagHelper CreateTagHelper<TTagHelper>() where TTagHelper : ITagHelper, new()
public TTagHelper CreateTagHelper<TTagHelper>() where TTagHelper : ITagHelper
{
var tagHelper = new TTagHelper();
var tagHelper = TypeActivatorCache.CreateInstance<TTagHelper>(
ViewContext.HttpContext.RequestServices,
typeof(TTagHelper));

TagHelperActivator.Activate(tagHelper, ViewContext);

Expand Down
13 changes: 10 additions & 3 deletions src/Microsoft.AspNet.Mvc.TagHelpers/AnchorTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ public class AnchorTagHelper : TagHelper
private const string RouteValuesPrefix = "asp-route-";
private const string Href = "href";

[Activate]
[HtmlAttributeNotBound]
public IHtmlGenerator Generator { get; set; }
/// <summary>
/// Creates a new <see cref="AnchorTagHelper"/>.
/// </summary>
/// <param name="generator">The <see cref="IHtmlGenerator"/>.</param>
public AnchorTagHelper(IHtmlGenerator generator)
{
Generator = generator;
}

protected IHtmlGenerator Generator { get; }

/// <summary>
/// The name of the action method.
Expand Down
17 changes: 12 additions & 5 deletions src/Microsoft.AspNet.Mvc.TagHelpers/CacheTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,24 @@ public class CacheTagHelper : TagHelper
private static readonly char[] AttributeSeparator = new[] { ',' };

/// <summary>
/// Gets or sets the <see cref="IMemoryCache"/> instance used to cache entries.
/// Creates a new <see cref="CacheTagHelper"/>.
/// </summary>
[Activate]
[HtmlAttributeNotBound]
public IMemoryCache MemoryCache { get; set; }
/// <param name="memoryCache">The <see cref="IMemoryCache"/>.</param>
public CacheTagHelper(IMemoryCache memoryCache)
{
MemoryCache = memoryCache;
}

/// <summary>
/// Gets the <see cref="IMemoryCache"/> instance used to cache entries.
/// </summary>
protected IMemoryCache MemoryCache { get; }

/// <summary>
/// Gets or sets the <see cref="ViewContext"/> for the current executing View.
/// </summary>
[Activate]
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }

/// <summary>
Expand Down
13 changes: 10 additions & 3 deletions src/Microsoft.AspNet.Mvc.TagHelpers/EnvironmentTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ public class EnvironmentTagHelper : TagHelper
{
private static readonly char[] NameSeparator = new[] { ',' };

/// <summary>
/// Creates a new <see cref="EnvironmentTagHelper"/>.
/// </summary>
/// <param name="hostingEnvironment">The <see cref="IHostingEnvironment"/>.</param>
public EnvironmentTagHelper(IHostingEnvironment hostingEnvironment)
{
HostingEnvironment = hostingEnvironment;
}

/// <summary>
/// A comma separated list of environment names in which the content should be rendered.
/// </summary>
Expand All @@ -25,9 +34,7 @@ public class EnvironmentTagHelper : TagHelper
/// </remarks>
public string Names { get; set; }

[Activate]
[HtmlAttributeNotBound]
public IHostingEnvironment HostingEnvironment { get; set; }
protected IHostingEnvironment HostingEnvironment { get; }

/// <inheritdoc />
public override void Process(TagHelperContext context, TagHelperOutput output)
Expand Down
15 changes: 11 additions & 4 deletions src/Microsoft.AspNet.Mvc.TagHelpers/FormTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ public class FormTagHelper : TagHelper
private const string RouteValuesPrefix = "asp-route-";
private const string HtmlActionAttributeName = "action";

[Activate]
/// <summary>
/// Creates a new <see cref="FormTagHelper"/>.
/// </summary>
/// <param name="generator">The <see cref="IHtmlGenerator"/>.</param>
public FormTagHelper(IHtmlGenerator generator)
{
Generator = generator;
}

[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }

[Activate]
[HtmlAttributeNotBound]
public IHtmlGenerator Generator { get; set; }
protected IHtmlGenerator Generator { get; }

/// <summary>
/// The name of the action method.
Expand Down
22 changes: 14 additions & 8 deletions src/Microsoft.AspNet.Mvc.TagHelpers/ImageTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public class ImageTagHelper : TagHelper

private FileVersionProvider _fileVersionProvider;

/// <summary>
/// Creates a new <see cref="ImageTagHelper"/>.
/// </summary>
/// <param name="hostingEnvironment">The <see cref="IHostingEnvironment"/>.</param>
/// <param name="cache">The <see cref="IMemoryCache"/>.</param>
public ImageTagHelper(IHostingEnvironment hostingEnvironment, IMemoryCache cache)
{
HostingEnvironment = hostingEnvironment;
Cache = cache;
}

/// <summary>
/// Source of the image.
/// </summary>
Expand All @@ -43,17 +54,12 @@ public class ImageTagHelper : TagHelper
[HtmlAttributeName(FileVersionAttributeName)]
public bool FileVersion { get; set; }

[Activate]
[HtmlAttributeNotBound]
public IHostingEnvironment HostingEnvironment { get; set; }
public IHostingEnvironment HostingEnvironment { get; }

[Activate]
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }

[Activate]
[HtmlAttributeNotBound]
public IMemoryCache Cache { get; set; }
public IMemoryCache Cache { get; }

/// <inheritdoc />
public override void Process(TagHelperContext context, TagHelperOutput output)
Expand Down
15 changes: 11 additions & 4 deletions src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,19 @@ public class InputTagHelper : TagHelper
{ "time", "{0:HH:mm:ss.fff}" },
};

[Activate]
[HtmlAttributeNotBound]
public IHtmlGenerator Generator { get; set; }
/// <summary>
/// Creates a new <see cref="InputTagHelper"/>.
/// </summary>
/// <param name="generator">The <see cref="IHtmlGenerator"/>.</param>
public InputTagHelper(IHtmlGenerator generator)
{
Generator = generator;
}

protected IHtmlGenerator Generator { get; }

[Activate]
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }

/// <summary>
Expand Down
15 changes: 11 additions & 4 deletions src/Microsoft.AspNet.Mvc.TagHelpers/LabelTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ public class LabelTagHelper : TagHelper
{
private const string ForAttributeName = "asp-for";

[Activate]
/// <summary>
/// Creates a new <see cref="LabelTagHelper"/>.
/// </summary>
/// <param name="generator">The <see cref="IHtmlGenerator"/>.</param>
public LabelTagHelper(IHtmlGenerator generator)
{
Generator = generator;
}

[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }

[Activate]
[HtmlAttributeNotBound]
public IHtmlGenerator Generator { get; set; }
protected IHtmlGenerator Generator { get; }

/// <summary>
/// An expression to be evaluated against the current model.
Expand Down
Loading

0 comments on commit 2cc71e4

Please sign in to comment.