зеркало из https://github.com/aspnet/Mvc.git
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:
Родитель
3dff1ca410
Коммит
b68fae9b8c
|
@ -25,7 +25,6 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
private readonly Stack<TextWriter> _writerScopes;
|
||||
private TextWriter _originalWriter;
|
||||
private IUrlHelper _urlHelper;
|
||||
private ITypeActivator _typeActivator;
|
||||
private ITagHelperActivator _tagHelperActivator;
|
||||
private bool _renderedBody;
|
||||
|
||||
|
@ -114,19 +113,6 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
/// <inheritdoc />
|
||||
public abstract Task ExecuteAsync();
|
||||
|
||||
private ITypeActivator TypeActivator
|
||||
{
|
||||
get
|
||||
{
|
||||
if(_typeActivator == null)
|
||||
{
|
||||
_typeActivator = ViewContext.HttpContext.RequestServices.GetService<ITypeActivator>();
|
||||
}
|
||||
|
||||
return _typeActivator;
|
||||
}
|
||||
}
|
||||
|
||||
private ITagHelperActivator TagHelperActivator
|
||||
{
|
||||
get
|
||||
|
@ -139,19 +125,17 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
return _tagHelperActivator;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates and activates a <see cref="ITagHelper"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TTagHelper">A <see cref="ITagHelper"/> type.</typeparam>
|
||||
/// <returns>The activated <see cref="ITagHelper"/>.</returns>
|
||||
/// <remarks>
|
||||
/// If the <see cref= "ITagHelper" /> implements <see cref="ICanHasViewContext"/> the
|
||||
/// <see cref="ICanHasViewContext.Contextualize(ViewContext)"/> method is called with <see cref="ViewContext"/>.
|
||||
/// <typeparamref name="TTagHelper"/> must have a parameterless constructor.
|
||||
/// </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);
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
|
@ -31,45 +32,18 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateTagHelper_ActivatesProvidedTagHelperType_Constructor()
|
||||
public void CreateTagHelper_ActivatesProvidedTagHelperType()
|
||||
{
|
||||
// Arrange
|
||||
var instance = CreateTestRazorPage();
|
||||
|
||||
// Act
|
||||
var tagHelper = instance.CreateTagHelper<ConstructorServiceTagHelper>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(tagHelper.PassedInService);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateTagHelper_ActivatesProvidedTagHelperType_Property()
|
||||
{
|
||||
// Arrange
|
||||
var instance = CreateTestRazorPage();
|
||||
|
||||
// Act
|
||||
var tagHelper = instance.CreateTagHelper<ActivateAttributeServiceTagHelper>();
|
||||
var tagHelper = instance.CreateTagHelper<ServiceTagHelper>();
|
||||
|
||||
// Assert
|
||||
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]
|
||||
public void CreateTagHelper_ProvidesTagHelperTypeWithViewContext()
|
||||
{
|
||||
|
@ -94,7 +68,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
|
||||
// Assert
|
||||
Assert.NotNull(tagHelper.ViewContext);
|
||||
Assert.NotNull(tagHelper.PassedInService);
|
||||
Assert.NotNull(tagHelper.ActivatedService);
|
||||
}
|
||||
|
||||
private static TestRazorPage CreateTestRazorPage()
|
||||
|
@ -138,35 +112,12 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
{
|
||||
}
|
||||
|
||||
private class ConstructorServiceTagHelper : TagHelper
|
||||
{
|
||||
public MyService PassedInService { get; set; }
|
||||
|
||||
public ConstructorServiceTagHelper(MyService service)
|
||||
{
|
||||
PassedInService = service;
|
||||
}
|
||||
}
|
||||
|
||||
private class ActivateAttributeServiceTagHelper : TagHelper
|
||||
private class ServiceTagHelper : TagHelper
|
||||
{
|
||||
[Activate]
|
||||
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
|
||||
{
|
||||
[Activate]
|
||||
|
@ -175,12 +126,8 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
|
||||
private class ViewContextServiceTagHelper : ViewContextTagHelper
|
||||
{
|
||||
public MyService PassedInService { get; set; }
|
||||
|
||||
public ViewContextServiceTagHelper(MyService service)
|
||||
{
|
||||
PassedInService = service;
|
||||
}
|
||||
[Activate]
|
||||
public MyService ActivatedService { get; set; }
|
||||
}
|
||||
|
||||
private class MyService
|
||||
|
|
Загрузка…
Ссылка в новой задаче