-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Blazor] Update discovery APIs and automatically configure the endpoi…
…nts based on the discovered RenderModes (#48283) * Removes IRazorComponentApplication<TRootComponent> * Adds APIs for component discovery across assemblies. * Adds support for wiring up server endpoints. * Adds support for automatic render mode detection.
- Loading branch information
Showing
36 changed files
with
1,878 additions
and
177 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/Components/Endpoints/src/Builder/RazorComponentApplicationAttribute.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,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Components.Discovery; | ||
|
||
namespace Microsoft.AspNetCore.Components.Infrastructure; | ||
|
||
/// <summary> | ||
/// Indicates how to collect the components that are part of a razor components | ||
/// application. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)] | ||
public abstract class RazorComponentApplicationAttribute : Attribute, IRazorComponentApplication | ||
{ | ||
/// <summary> | ||
/// Creates a builder that can be used to customize the definition of the application. | ||
/// For example, to add or remove pages, change routes, etc. | ||
/// </summary> | ||
/// <returns> | ||
/// The <see cref="ComponentApplicationBuilder"/> associated with the application definition. | ||
/// </returns> | ||
public abstract ComponentApplicationBuilder GetBuilder(); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Components/Endpoints/src/Builder/RazorComponentDataSourceOptions.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,26 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace Microsoft.AspNetCore.Components.Endpoints; | ||
|
||
/// <summary> | ||
/// Options associated with the endpoints defined by the components in the | ||
/// given <see cref="RazorComponentsEndpointRouteBuilderExtensions.MapRazorComponents{TRootComponent}(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder)"/> | ||
/// invocation. | ||
/// </summary> | ||
public class RazorComponentDataSourceOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets whether to automatically wire up the necessary endpoints | ||
/// based on the declared render modes of the components that are | ||
/// part of this set of endpoints. | ||
/// </summary> | ||
/// <remarks> | ||
/// The default value is <c>true</c>. | ||
/// </remarks> | ||
public bool UseDeclaredRenderModes { get; set; } = true; | ||
|
||
internal IList<IComponentRenderMode> ConfiguredRenderModes { get; } = new List<IComponentRenderMode>(); | ||
} |
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
17 changes: 12 additions & 5 deletions
17
src/Components/Endpoints/src/Builder/RazorComponentEndpointDataSourceFactory.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 |
---|---|---|
@@ -1,23 +1,30 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Components.Discovery; | ||
using Microsoft.AspNetCore.Components.Endpoints; | ||
using Microsoft.AspNetCore.Routing; | ||
|
||
namespace Microsoft.AspNetCore.Components.Infrastructure; | ||
|
||
internal class RazorComponentEndpointDataSourceFactory | ||
{ | ||
private readonly RazorComponentEndpointFactory _factory; | ||
private readonly IEnumerable<RenderModeEndpointProvider> _providers; | ||
|
||
public RazorComponentEndpointDataSourceFactory(RazorComponentEndpointFactory factory) | ||
public RazorComponentEndpointDataSourceFactory( | ||
RazorComponentEndpointFactory factory, | ||
IEnumerable<RenderModeEndpointProvider> providers) | ||
{ | ||
_factory = factory; | ||
_providers = providers; | ||
} | ||
|
||
public RazorComponentEndpointDataSource<TRootComponent> CreateDataSource<TRootComponent>() | ||
where TRootComponent : IRazorComponentApplication<TRootComponent> | ||
public RazorComponentEndpointDataSource<TRootComponent> CreateDataSource<TRootComponent>(IEndpointRouteBuilder endpoints) | ||
{ | ||
var builder = TRootComponent.GetBuilder(); | ||
return new RazorComponentEndpointDataSource<TRootComponent>(builder, _factory); | ||
var builder = ComponentApplicationBuilder.GetBuilder<TRootComponent>() ?? | ||
DefaultRazorComponentApplication<TRootComponent>.Instance.GetBuilder(); | ||
|
||
return new RazorComponentEndpointDataSource<TRootComponent>(builder, _providers, endpoints.CreateApplicationBuilder(), _factory); | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
src/Components/Endpoints/src/Builder/RenderModeEndpointProvider.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,58 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
|
||
namespace Microsoft.AspNetCore.Components.Endpoints; | ||
|
||
/// <summary> | ||
/// A provider that can register endpoints to support a specific <see cref="IComponentRenderMode"/>. | ||
/// </summary> | ||
public abstract class RenderModeEndpointProvider | ||
{ | ||
/// <summary> | ||
/// Determines whether this <see cref="RenderModeEndpointProvider"/> supports the specified <paramref name="renderMode"/>. | ||
/// </summary> | ||
/// <param name="renderMode">The <see cref="IComponentRenderMode"/>.</param> | ||
/// <returns><c>true</c> if the <see cref="IComponentRenderMode"/> is supported; <c>false</c> otherwise.</returns> | ||
public abstract bool Supports(IComponentRenderMode renderMode); | ||
|
||
/// <summary> | ||
/// Gets the endpoints for the specified <paramref name="renderMode"/>. | ||
/// </summary> | ||
/// <param name="renderMode">The <see cref="IComponentRenderMode"/>.</param> | ||
/// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/> used to configure non endpoint aware endpoints.</param> | ||
/// <returns>The list of endpoints this provider is registering.</returns> | ||
public abstract IEnumerable<RouteEndpointBuilder> GetEndpointBuilders( | ||
IComponentRenderMode renderMode, | ||
IApplicationBuilder applicationBuilder); | ||
|
||
internal static void AddEndpoints( | ||
List<Endpoint> endpoints, | ||
Type rootComponent, | ||
IEnumerable<RouteEndpointBuilder> renderModeEndpoints, | ||
IComponentRenderMode renderMode, | ||
List<Action<EndpointBuilder>> conventions, | ||
List<Action<EndpointBuilder>> finallyConventions) | ||
{ | ||
foreach (var builder in renderModeEndpoints) | ||
{ | ||
builder.Metadata.Add(new RootComponentMetadata(rootComponent)); | ||
builder.Metadata.Add(renderMode); | ||
|
||
foreach (var convention in conventions) | ||
{ | ||
convention(builder); | ||
} | ||
|
||
foreach (var finallyConvention in finallyConventions) | ||
{ | ||
finallyConvention(builder); | ||
} | ||
|
||
endpoints.Add(builder.Build()); | ||
} | ||
} | ||
} |
Oops, something went wrong.