Removed the TypeActivator from TagHelper creation.
- We now new up TagHelper's, requiring them to have parameterless constructors. - Added test to validate throwing. - Removed existing tests that expected the constructor injection behavior. #1303
This commit is contained in:
parent
3dff1ca410
commit
b68fae9b8c
|
|
@ -25,7 +25,6 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
private readonly Stack<TextWriter> _writerScopes;
|
private readonly Stack<TextWriter> _writerScopes;
|
||||||
private TextWriter _originalWriter;
|
private TextWriter _originalWriter;
|
||||||
private IUrlHelper _urlHelper;
|
private IUrlHelper _urlHelper;
|
||||||
private ITypeActivator _typeActivator;
|
|
||||||
private ITagHelperActivator _tagHelperActivator;
|
private ITagHelperActivator _tagHelperActivator;
|
||||||
private bool _renderedBody;
|
private bool _renderedBody;
|
||||||
|
|
||||||
|
|
@ -114,19 +113,6 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public abstract Task ExecuteAsync();
|
public abstract Task ExecuteAsync();
|
||||||
|
|
||||||
private ITypeActivator TypeActivator
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
if(_typeActivator == null)
|
|
||||||
{
|
|
||||||
_typeActivator = ViewContext.HttpContext.RequestServices.GetService<ITypeActivator>();
|
|
||||||
}
|
|
||||||
|
|
||||||
return _typeActivator;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ITagHelperActivator TagHelperActivator
|
private ITagHelperActivator TagHelperActivator
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
@ -139,19 +125,17 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
return _tagHelperActivator;
|
return _tagHelperActivator;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates and activates a <see cref="ITagHelper"/>.
|
/// Creates and activates a <see cref="ITagHelper"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TTagHelper">A <see cref="ITagHelper"/> type.</typeparam>
|
/// <typeparam name="TTagHelper">A <see cref="ITagHelper"/> type.</typeparam>
|
||||||
/// <returns>The activated <see cref="ITagHelper"/>.</returns>
|
/// <returns>The activated <see cref="ITagHelper"/>.</returns>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// If the <see cref= "ITagHelper" /> implements <see cref="ICanHasViewContext"/> the
|
/// <typeparamref name="TTagHelper"/> must have a parameterless constructor.
|
||||||
/// <see cref="ICanHasViewContext.Contextualize(ViewContext)"/> method is called with <see cref="ViewContext"/>.
|
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public TTagHelper CreateTagHelper<TTagHelper>() where TTagHelper : ITagHelper
|
public TTagHelper CreateTagHelper<TTagHelper>() where TTagHelper : ITagHelper, new()
|
||||||
{
|
{
|
||||||
var tagHelper = TypeActivator.CreateInstance<TTagHelper>(ViewContext.HttpContext.RequestServices);
|
var tagHelper = new TTagHelper();
|
||||||
|
|
||||||
TagHelperActivator.Activate(tagHelper, ViewContext);
|
TagHelperActivator.Activate(tagHelper, ViewContext);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
|
|
@ -31,45 +32,18 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CreateTagHelper_ActivatesProvidedTagHelperType_Constructor()
|
public void CreateTagHelper_ActivatesProvidedTagHelperType()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var instance = CreateTestRazorPage();
|
var instance = CreateTestRazorPage();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var tagHelper = instance.CreateTagHelper<ConstructorServiceTagHelper>();
|
var tagHelper = instance.CreateTagHelper<ServiceTagHelper>();
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.NotNull(tagHelper.PassedInService);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void CreateTagHelper_ActivatesProvidedTagHelperType_Property()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var instance = CreateTestRazorPage();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var tagHelper = instance.CreateTagHelper<ActivateAttributeServiceTagHelper>();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.NotNull(tagHelper.ActivatedService);
|
Assert.NotNull(tagHelper.ActivatedService);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void CreateTagHelper_ActivatesProvidedTagHelperType_PropertyAndConstructor()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var instance = CreateTestRazorPage();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var tagHelper = instance.CreateTagHelper<AttributeConstructorServiceTagHelper>();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.NotNull(tagHelper.ActivatedService);
|
|
||||||
Assert.NotNull(tagHelper.PassedInService);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CreateTagHelper_ProvidesTagHelperTypeWithViewContext()
|
public void CreateTagHelper_ProvidesTagHelperTypeWithViewContext()
|
||||||
{
|
{
|
||||||
|
|
@ -94,7 +68,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.NotNull(tagHelper.ViewContext);
|
Assert.NotNull(tagHelper.ViewContext);
|
||||||
Assert.NotNull(tagHelper.PassedInService);
|
Assert.NotNull(tagHelper.ActivatedService);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TestRazorPage CreateTestRazorPage()
|
private static TestRazorPage CreateTestRazorPage()
|
||||||
|
|
@ -138,35 +112,12 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ConstructorServiceTagHelper : TagHelper
|
private class ServiceTagHelper : TagHelper
|
||||||
{
|
|
||||||
public MyService PassedInService { get; set; }
|
|
||||||
|
|
||||||
public ConstructorServiceTagHelper(MyService service)
|
|
||||||
{
|
|
||||||
PassedInService = service;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class ActivateAttributeServiceTagHelper : TagHelper
|
|
||||||
{
|
{
|
||||||
[Activate]
|
[Activate]
|
||||||
public MyService ActivatedService { get; set; }
|
public MyService ActivatedService { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
private class AttributeConstructorServiceTagHelper : TagHelper
|
|
||||||
{
|
|
||||||
[Activate]
|
|
||||||
public MyService ActivatedService { get; set; }
|
|
||||||
|
|
||||||
public MyService PassedInService { get; set; }
|
|
||||||
|
|
||||||
public AttributeConstructorServiceTagHelper(MyService service)
|
|
||||||
{
|
|
||||||
PassedInService = service;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class ViewContextTagHelper : TagHelper
|
private class ViewContextTagHelper : TagHelper
|
||||||
{
|
{
|
||||||
[Activate]
|
[Activate]
|
||||||
|
|
@ -175,12 +126,8 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
|
|
||||||
private class ViewContextServiceTagHelper : ViewContextTagHelper
|
private class ViewContextServiceTagHelper : ViewContextTagHelper
|
||||||
{
|
{
|
||||||
public MyService PassedInService { get; set; }
|
[Activate]
|
||||||
|
public MyService ActivatedService { get; set; }
|
||||||
public ViewContextServiceTagHelper(MyService service)
|
|
||||||
{
|
|
||||||
PassedInService = service;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MyService
|
private class MyService
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue