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 #4089] Add support for application parts
This commit introduces application parts as a concept on MVC. An application part is an abstraction that allows you to expose some feature or corncern in a way that is decoupled from their underlying source. Examples of this include types in an assembly, emdeded resources, files on disk etc. Application parts are configured during startup by adding or removing them from the application part manager available as part of IMvcBuilder and IMvcCoreBuilder. The application part manager provides the ability to populate features from the list of available application parts by using a list of application feature providers. Application feature providers are responsible for populating a given feature given a list of application parts. Examples of application providers can be a ControllerFeatureProvider that goes through the list of application parts, sees which one of those parts exposes types, determines which of those types are controller types, and adds them to a ControllerFeature that holds a list of all the types that will be considered controllers in the application.
- Loading branch information
Showing
25 changed files
with
676 additions
and
17 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/ApplicationPart.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,16 @@ | ||
// 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. | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ApplicationParts | ||
{ | ||
/// <summary> | ||
/// A part of an MVC application. | ||
/// </summary> | ||
public abstract class ApplicationPart | ||
{ | ||
/// <summary> | ||
/// Gets the <see cref="ApplicationPart"/> name. | ||
/// </summary> | ||
public abstract string Name { get; } | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/ApplicationPartManager.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,47 @@ | ||
// 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; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ApplicationParts | ||
{ | ||
/// <summary> | ||
/// Manages the parts and features of an MVC application. | ||
/// </summary> | ||
public class ApplicationPartManager | ||
{ | ||
/// <summary> | ||
/// Gets the list of <see cref="IApplicationFeatureProvider"/>s. | ||
/// </summary> | ||
public IList<IApplicationFeatureProvider> FeatureProviders { get; } = | ||
new List<IApplicationFeatureProvider>(); | ||
|
||
/// <summary> | ||
/// Gets the list of <see cref="ApplicationPart"/>s. | ||
/// </summary> | ||
public IList<ApplicationPart> ApplicationParts { get; } = | ||
new List<ApplicationPart>(); | ||
|
||
/// <summary> | ||
/// Populates the given <paramref name="feature"/> using the list of | ||
/// <see cref="IApplicationFeatureProvider{TFeature}"/>s configured on the | ||
/// <see cref="ApplicationPartManager"/>. | ||
/// </summary> | ||
/// <typeparam name="TFeature">The type of the feature.</typeparam> | ||
/// <param name="feature">The feature instance to populate.</param> | ||
public void PopulateFeature<TFeature>(TFeature feature) | ||
{ | ||
if (feature == null) | ||
{ | ||
throw new ArgumentNullException(nameof(feature)); | ||
} | ||
|
||
foreach (var provider in FeatureProviders.OfType<IApplicationFeatureProvider<TFeature>>()) | ||
{ | ||
provider.PopulateFeature(ApplicationParts, feature); | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/AssemblyPart.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,39 @@ | ||
// 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.Reflection; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ApplicationParts | ||
{ | ||
/// <summary> | ||
/// An <see cref="ApplicationPart"/> backed by an <see cref="Assembly"/>. | ||
/// </summary> | ||
public class AssemblyPart : ApplicationPart | ||
{ | ||
/// <summary> | ||
/// Initalizes a new <see cref="AssemblyPart"/> instance. | ||
/// </summary> | ||
/// <param name="assembly"></param> | ||
public AssemblyPart(Assembly assembly) | ||
{ | ||
if (assembly == null) | ||
{ | ||
throw new ArgumentNullException(nameof(assembly)); | ||
} | ||
|
||
Assembly = assembly; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the <see cref="Assembly"/> of the <see cref="ApplicationPart"/>. | ||
/// </summary> | ||
public Assembly Assembly { get; } | ||
|
||
/// <summary> | ||
/// Gets the name of the <see cref="ApplicationPart"/>. | ||
/// </summary> | ||
public override string Name => Assembly.GetName().Name; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/IApplicationFeatureProvider.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,13 @@ | ||
// 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. | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ApplicationParts | ||
{ | ||
/// <summary> | ||
/// Marker interface for <see cref="IApplicationFeatureProvider"/> | ||
/// implementations. | ||
/// </summary> | ||
public interface IApplicationFeatureProvider | ||
{ | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/IApplicationFeatureProviderOfT.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 @@ | ||
// 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; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ApplicationParts | ||
{ | ||
/// <summary> | ||
/// A provider for a given <typeparamref name="TFeature"/> feature. | ||
/// </summary> | ||
/// <typeparam name="TFeature">The type of the feature.</typeparam> | ||
public interface IApplicationFeatureProvider<TFeature> : IApplicationFeatureProvider | ||
{ | ||
/// <summary> | ||
/// Updates the <paramref name="feature"/> intance. | ||
/// </summary> | ||
/// <param name="parts">The list of <see cref="ApplicationPart"/>s of the | ||
/// application. | ||
/// </param> | ||
/// <param name="feature">The feature instance to populate.</param> | ||
void PopulateFeature(IEnumerable<ApplicationPart> parts, TFeature feature); | ||
} | ||
} |
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
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
Oops, something went wrong.