This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes #4087] Add AddViewComponentsAsServices() and ServiceBasedViewC…
…omponentActivator * Added ViewComponentFeture and ViewComponentFeatureProvider to perform view component discovery. * Changed view component discovery to use application parts. * Changed ViewComponentDescriptorProvider to make use of Application parts. * Added AddViewComponentsAsServices method on IMvcBuilder that performs view component discovery through the ApplicationPartManager and registers those view components as services in the service collection. Assemblies should be added to the ApplicationPartManager in order to discover view components in them in them.
- Loading branch information
Showing
19 changed files
with
530 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...icrosoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/ServiceBasedViewComponentActivator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 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; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ViewComponents | ||
{ | ||
/// <summary> | ||
/// A <see cref="IViewComponentActivator"/> that retrieves view components as services from the request's | ||
/// <see cref="IServiceProvider"/>. | ||
/// </summary> | ||
public class ServiceBasedViewComponentActivator : IViewComponentActivator | ||
{ | ||
/// <inheritdoc /> | ||
public object Create(ViewComponentContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
var viewComponentType = context.ViewComponentDescriptor.TypeInfo.AsType(); | ||
|
||
return context.ViewContext.HttpContext.RequestServices.GetRequiredService(viewComponentType); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual void Release(ViewComponentContext context, object viewComponent) | ||
{ | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/ViewComponentFeature.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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.Collections.Generic; | ||
using System.Reflection; | ||
using Microsoft.AspNetCore.Mvc.ApplicationParts; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ViewComponents | ||
{ | ||
/// <summary> | ||
/// The list of view component types in an MVC application.The <see cref="ViewComponentFeature"/> can be populated | ||
/// using the <see cref="ApplicationPartManager"/> that is available during startup at <see cref="IMvcBuilder.PartManager"/> | ||
/// and <see cref="IMvcCoreBuilder.PartManager"/> or at a later stage by requiring the <see cref="ApplicationPartManager"/> | ||
/// as a dependency in a component. | ||
/// </summary> | ||
public class ViewComponentFeature | ||
{ | ||
/// <summary> | ||
/// Gets the list of view component types in an MVC application. | ||
/// </summary> | ||
public IList<TypeInfo> ViewComponents { get; } = new List<TypeInfo>(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/ViewComponentFeatureProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc.ApplicationParts; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ViewComponents | ||
{ | ||
/// <summary> | ||
/// Discovers view components from a list of <see cref="ApplicationPart"/> instances. | ||
/// </summary> | ||
public class ViewComponentFeatureProvider : IApplicationFeatureProvider<ViewComponentFeature> | ||
{ | ||
/// <inheritdoc /> | ||
public void PopulateFeature(IEnumerable<ApplicationPart> parts, ViewComponentFeature feature) | ||
{ | ||
if (parts == null) | ||
{ | ||
throw new ArgumentNullException(nameof(parts)); | ||
} | ||
|
||
if (feature == null) | ||
{ | ||
throw new ArgumentNullException(nameof(feature)); | ||
} | ||
|
||
foreach (var type in parts.OfType<IApplicationPartTypeProvider>().SelectMany(p => p.Types)) | ||
{ | ||
if (ViewComponentConventions.IsComponent(type) && ! feature.ViewComponents.Contains(type)) | ||
{ | ||
feature.ViewComponents.Add(type); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
test/Microsoft.AspNetCore.Mvc.FunctionalTests/ViewComponentFromServicesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// 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; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.FunctionalTests | ||
{ | ||
public class ViewComponentFromServicesTest : IClassFixture<MvcTestFixture<ControllersFromServicesWebSite.Startup>> | ||
{ | ||
public ViewComponentFromServicesTest(MvcTestFixture<ControllersFromServicesWebSite.Startup> fixture) | ||
{ | ||
Client = fixture.Client; | ||
} | ||
|
||
public HttpClient Client { get; } | ||
|
||
[Fact] | ||
public async Task ViewComponentsWithConstructorInjectionAreCreatedAndActivated() | ||
{ | ||
// Arrange | ||
var expected = "Value = 3"; | ||
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/another/InServicesViewComponent"); | ||
|
||
// Act | ||
var response = await Client.SendAsync(request); | ||
var responseText = await response.Content.ReadAsStringAsync(); | ||
|
||
// Assert | ||
Assert.Equal(expected, responseText); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.