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

Add ITagHelperComponentManager #6302

Merged
merged 5 commits into from
May 23, 2017
Merged

Add ITagHelperComponentManager #6302

merged 5 commits into from
May 23, 2017

Conversation

jbagga
Copy link
Contributor

@jbagga jbagga commented May 20, 2017

Addresses #6282

cc @sebastienros

}

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

_components = components;
_components = new List<ITagHelperComponent>(manager.Components.OrderBy(p => p.Order));
Copy link
Member

Choose a reason for hiding this comment

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

manager.Components.OrderBy(p => p.Order).ToArray()

Copy link
Member

Choose a reason for hiding this comment

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

Why ToArray()

Copy link
Member

Choose a reason for hiding this comment

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

The ToList() operation is more readable.
Then there is no need for a list, hence ToArray

Copy link
Member

@davidfowl davidfowl May 20, 2017

Choose a reason for hiding this comment

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

Ah, I thought this was the provider itself. This is the taghelper.


namespace Microsoft.AspNetCore.Mvc.Razor.TagHelpers
{
public interface ITagHelperComponentManager
Copy link
Member

Choose a reason for hiding this comment

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

It will need comments.

public void Add(ITagHelperComponent tagHelperComponent)
{
_tagHelperComponents.Add(tagHelperComponent);
}
Copy link
Member

Choose a reason for hiding this comment

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

Why not IList<> for the components? Is is not supported to remove components?

I can see two sensible ways that ITagHelperManager deals with ordering in a consistent way.

  1. Expose IList<> for the components. The components from DI are always put in order for you. Callers can add or insert a component wherever they want, reorder the list, remove stuff, etc.

  2. Expose IEnumerable<> or ICollection<>. Calling add should find the right place in the list for the new component based on its order and insert it. Use ICollection<> if you want to support removing things or IEnumerable<> if you don't.

@jbagga
Copy link
Contributor Author

jbagga commented May 22, 2017

@sebastienros @rynowak Updated

{
/// <summary>
/// Contract used to manage <see cref="ITagHelperComponent"/>s.
/// </summary>
Copy link
Member

Choose a reason for hiding this comment

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

This isn't super useful, what can you do with it?

using System.Collections.ObjectModel;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace Microsoft.AspNetCore.Mvc.Razor.TagHelpers
Copy link
Member

Choose a reason for hiding this comment

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

This should be in a more secretive namespace like .Internal, there's no need for anyone to interact with this type.

Copy link
Member

@rynowak rynowak left a comment

Choose a reason for hiding this comment

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

:shipit: from me after addressing small comments. Get a :shipit: from 🇫🇷 as well

}

/// <inheritdoc />
public ICollection<ITagHelperComponent> Components { get; private set; }
Copy link
Member

Choose a reason for hiding this comment

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

  • Suggest IList<T> since ICollection<T> is a loser interface.
  • remove the private set; bit

Copy link
Member

Choose a reason for hiding this comment

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

Oops, missed @rynowak's comment about the chosen collection type. We obviously want to Add() so IEnumerable<T> is out.

The main differences between ICollection<T> and IList<T> are odd gaps in ICollection<T> e.g. no RemoveAt(). But, both support Remove() and I lean toward IList<T> here.

@@ -15,7 +15,7 @@ public override Task ProcessAsync(TagHelperContext context, TagHelperOutput outp
{
if (string.Equals(context.TagName, "body", StringComparison.Ordinal) && output.Attributes.ContainsName("inject"))
{
output.PostContent.AppendHtml("<script>'This was injected!!'</script>");
output.PostContent.AppendHtml("<script>'This was injected!!'</script>\r\n");
Copy link
Member

Choose a reason for hiding this comment

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

Do not add Windows-specific line endings. My preference is to leave this unchanged because this changes the baselines for no reason.

Copy link
Member

Choose a reason for hiding this comment

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

Same for TestHeadTagHelperComponent

<script>'This was injected!!'</script>
</body>
Copy link
Member

Choose a reason for hiding this comment

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

Confusing that this and RazorWebSite.TagHelperComponent.Head.html changed at all. As mentioned later, revert the .cshtml changes.

{
new CallbackTagHelperComponent(
new CallbackTagHelperComponent(
Copy link
Member

Choose a reason for hiding this comment

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

Restore the leading space.

}

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

_components = components;
_components = manager.Components.OrderBy(p => p.Order).ToArray();
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment mentioning this ordering must be done after the manager is created (otherwise controller-added components won't be positioned correctly).

Copy link
Member

Choose a reason for hiding this comment

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

thx

@jbagga jbagga force-pushed the jbagga/ITagHelperComponents6282 branch from 67e1e9d to 8f343be Compare May 23, 2017 01:33
@jbagga
Copy link
Contributor Author

jbagga commented May 23, 2017

@dougbu Updated

Copy link
Member

@dougbu dougbu left a comment

Choose a reason for hiding this comment

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

Getting close. Sorry if I missed a couple of things in my previous read-through.

@@ -18,10 +18,11 @@ public class BodyTagHelper : TagHelperComponentTagHelper
/// <summary>
/// Creates a new <see cref="BodyTagHelper"/>.
/// </summary>
/// <param name="components">The list of <see cref="ITagHelperComponent"/>.</param>
/// <param name="manager">The <see cref="ITagHelperComponentManager"/> which contains the list
Copy link
Member

Choose a reason for hiding this comment

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

"contains the list" -> "contains the collection" throughout this PR. (There's a few left after your comment updates.)

{
/// <summary>
/// An implementation of this interface provides the collection of <see cref="ITagHelperComponent"/>s
/// that will be used by the <see cref="TagHelperComponentTagHelper"/>.
Copy link
Member

Choose a reason for hiding this comment

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

Minor but <see cref="TagHelperComponentTagHelper"/>s is better than the <see cref="TagHelperComponentTagHelper"/> because there isn't just one <see cref="TagHelperComponentTagHelper"/>.

}

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

_components = components;
_components = manager.Components.OrderBy(p => p.Order).ToArray();
Copy link
Member

Choose a reason for hiding this comment

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

thx


public void Init(TagHelperContext context)
{
context.Items["Key2"] = "Value2";
Copy link
Member

Choose a reason for hiding this comment

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

What is this Items entry used for? Same for the similar code in later test components.


private class TestAddTagHelperComponent : ITagHelperComponent
{
public int Order => 0;
Copy link
Member

Choose a reason for hiding this comment

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

Suggest making this settable and including it in what's added to PostContent. Then, can test that controller-added components are processed in the correct order.

<ProjectReference Include="..\Microsoft.AspNetCore.Mvc.TestConfiguration\Microsoft.AspNetCore.Mvc.TestConfiguration.csproj" />

<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="$(AspNetCoreVersion)" />
Copy link
Member

Choose a reason for hiding this comment

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

Remove these dependencies; Microsoft.AspNetCore.Mvc brings them in.


namespace RazorWebSite
{
public class TestViewTagHelperComponent : ITagHelperComponent
Copy link
Member

Choose a reason for hiding this comment

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

Not quite sure why we've got multiple almost-identical components in this web site. Couldn't the bits that differ e.g. the appended content be passed into the constructor or (hmm, Order) set?

@jbagga
Copy link
Contributor Author

jbagga commented May 23, 2017

@dougbu Updated


// Act
await testTagHelperComponentTagHelper.ProcessAsync(tagHelperContext, output);

// Assert
Assert.Equal("Processed", output.PostContent.GetContent());
Assert.Equal("Processed0Processed1Processed2", output.PostContent.GetContent());
Copy link
Member

Choose a reason for hiding this comment

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

👍

@jbagga jbagga force-pushed the jbagga/ITagHelperComponents6282 branch from b1cef98 to a92f25c Compare May 23, 2017 19:06
@jbagga jbagga merged commit e681c23 into dev May 23, 2017
@jbagga jbagga deleted the jbagga/ITagHelperComponents6282 branch May 23, 2017 20:22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants