-
Notifications
You must be signed in to change notification settings - Fork 120
Feature-based Injection #335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zhiyuanliang-ms
merged 31 commits into
preview
from
zhiyuanliang/feature-based-injection
Feb 9, 2024
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
57d2243
init
zhiyuanliang-ms f9506c9
draft
zhiyuanliang-ms f9c1484
Merge branch 'preview' into zhiyuanliang/feature-based-injection
zhiyuanliang-ms 5edd9be
use ValueTask
zhiyuanliang-ms 06f410b
Merge branch 'preview' of https://github.com/microsoft/FeatureManagem…
zhiyuanliang-ms e1b2450
support factory method
zhiyuanliang-ms f497405
add example
zhiyuanliang-ms 2b7ad46
Merge branch 'preview' of https://github.com/microsoft/FeatureManagem…
zhiyuanliang-ms d89abc8
update
zhiyuanliang-ms ca772ec
Update
zhiyuanliang-ms 3710dab
Merge branch 'preview' into zhiyuanliang/feature-based-injection
zhiyuanliang-ms b92fefb
Update
zhiyuanliang-ms f3926f8
update example
zhiyuanliang-ms 0d0f3f3
match variant name or configuration value
zhiyuanliang-ms 176cf78
update to the latest design
zhiyuanliang-ms a7b8f0f
merge with preview branch
zhiyuanliang-ms 73aac10
resolve comments
zhiyuanliang-ms 25f91e9
remove check for variant value
zhiyuanliang-ms 3ed4c4c
rename to VariantService
zhiyuanliang-ms 941c7a1
update & add comments
zhiyuanliang-ms 18ae0eb
remove POC example
zhiyuanliang-ms f48767c
add testcases & use method name GetServiceAsync
zhiyuanliang-ms 1556c0d
update comments
zhiyuanliang-ms 7120abd
add variant service cache
zhiyuanliang-ms 1a4e0fa
resolve comments
zhiyuanliang-ms 926182a
throw exception for duplicated registration
zhiyuanliang-ms 1065d76
add testcase
zhiyuanliang-ms 37cda1b
remove unused package
zhiyuanliang-ms df47105
update comment
zhiyuanliang-ms cbda0c6
set feature name in constructor
zhiyuanliang-ms 6488a81
Merge branch 'preview' into zhiyuanliang/feature-based-injection
zhiyuanliang-ms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
21 changes: 21 additions & 0 deletions
21
src/Microsoft.FeatureManagement/IVariantServiceProvider.cs
This file contains hidden or 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,21 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| // | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.FeatureManagement | ||
| { | ||
| /// <summary> | ||
| /// Used to get different implementation variants of TService. | ||
| /// </summary> | ||
| public interface IVariantServiceProvider<TService> where TService : class | ||
| { | ||
| /// <summary> | ||
| /// Gets an implementation variant of TService. | ||
| /// </summary> | ||
| /// <param name="cancellationToken">The cancellation token to cancel the operation.</param> | ||
| /// <returns>An implementation of TService.</returns> | ||
| ValueTask<TService> GetServiceAsync(CancellationToken cancellationToken); | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
src/Microsoft.FeatureManagement/VariantServiceAliasAttribute.cs
This file contains hidden or 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,32 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| // | ||
| using System; | ||
|
|
||
| namespace Microsoft.FeatureManagement | ||
| { | ||
| /// <summary> | ||
| /// Allows the name of a variant service to be customized to relate to the variant name specified in configuration. | ||
| /// </summary> | ||
| public class VariantServiceAliasAttribute : Attribute | ||
| { | ||
| /// <summary> | ||
| /// Creates a variant service alias using the provided alias. | ||
| /// </summary> | ||
| /// <param name="alias">The alias of the variant service.</param> | ||
| public VariantServiceAliasAttribute(string alias) | ||
| { | ||
| if (string.IsNullOrEmpty(alias)) | ||
| { | ||
| throw new ArgumentNullException(nameof(alias)); | ||
| } | ||
|
|
||
| Alias = alias; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The name that will be used to match variant name specified in the configuration. | ||
| /// </summary> | ||
| public string Alias { get; } | ||
| } | ||
| } |
This file contains hidden or 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,80 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| // | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.FeatureManagement | ||
| { | ||
| /// <summary> | ||
| /// Used to get different implementations of TService depending on the assigned variant from a specific variant feature flag. | ||
| /// </summary> | ||
| internal class VariantServiceProvider<TService> : IVariantServiceProvider<TService> where TService : class | ||
| { | ||
| private readonly IEnumerable<TService> _services; | ||
| private readonly IVariantFeatureManager _featureManager; | ||
| private readonly string _featureName; | ||
| private readonly ConcurrentDictionary<string, TService> _variantServiceCache; | ||
|
|
||
| /// <summary> | ||
| /// Creates a variant service provider. | ||
| /// </summary> | ||
| /// <param name="featureName">The feature flag that should be used to determine which variant of the service should be used.</param> | ||
| /// <param name="featureManager">The feature manager to get the assigned variant of the feature flag.</param> | ||
| /// <param name="services">Implementation variants of TService.</param> | ||
| /// <exception cref="ArgumentNullException">Thrown if <paramref name="featureName"/> is null.</exception> | ||
| /// <exception cref="ArgumentNullException">Thrown if <paramref name="featureManager"/> is null.</exception> | ||
| /// <exception cref="ArgumentNullException">Thrown if <paramref name="services"/> is null.</exception> | ||
| public VariantServiceProvider(string featureName, IVariantFeatureManager featureManager, IEnumerable<TService> services) | ||
| { | ||
| _featureName = featureName ?? throw new ArgumentNullException(nameof(featureName)); | ||
| _featureManager = featureManager ?? throw new ArgumentNullException(nameof(featureManager)); | ||
| _services = services ?? throw new ArgumentNullException(nameof(services)); | ||
| _variantServiceCache = new ConcurrentDictionary<string, TService>(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets implementation of TService according to the assigned variant from the feature flag. | ||
| /// </summary> | ||
| /// <param name="cancellationToken">The cancellation token to cancel the operation.</param> | ||
| /// <returns>An implementation matched with the assigned variant. If there is no matched implementation, it will return null.</returns> | ||
| public async ValueTask<TService> GetServiceAsync(CancellationToken cancellationToken) | ||
| { | ||
| Debug.Assert(_featureName != null); | ||
|
|
||
| Variant variant = await _featureManager.GetVariantAsync(_featureName, cancellationToken); | ||
|
|
||
| TService implementation = null; | ||
|
|
||
| if (variant != null) | ||
| { | ||
| implementation = _variantServiceCache.GetOrAdd( | ||
| variant.Name, | ||
| (_) => _services.FirstOrDefault( | ||
| service => IsMatchingVariantName( | ||
| service.GetType(), | ||
| variant.Name)) | ||
| ); | ||
| } | ||
|
|
||
| return implementation; | ||
| } | ||
|
|
||
| private bool IsMatchingVariantName(Type implementationType, string variantName) | ||
| { | ||
| string implementationName = ((VariantServiceAliasAttribute)Attribute.GetCustomAttribute(implementationType, typeof(VariantServiceAliasAttribute)))?.Alias; | ||
|
|
||
| if (implementationName == null) | ||
| { | ||
| implementationName = implementationType.Name; | ||
| } | ||
|
|
||
| return string.Equals(implementationName, variantName, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,40 @@ | ||
| using Microsoft.FeatureManagement; | ||
|
|
||
| namespace Tests.FeatureManagement | ||
| { | ||
| interface IAlgorithm | ||
| { | ||
| public string Style { get; } | ||
| } | ||
|
|
||
| class AlgorithmBeta : IAlgorithm | ||
| { | ||
| public string Style { get; set; } | ||
|
|
||
| public AlgorithmBeta() | ||
| { | ||
| Style = "Beta"; | ||
| } | ||
| } | ||
|
|
||
| class AlgorithmSigma : IAlgorithm | ||
| { | ||
| public string Style { get; set; } | ||
|
|
||
| public AlgorithmSigma() | ||
| { | ||
| Style = "Sigma"; | ||
| } | ||
| } | ||
|
|
||
| [VariantServiceAlias("Omega")] | ||
| class AlgorithmOmega : IAlgorithm | ||
| { | ||
| public string Style { get; set; } | ||
|
|
||
| public AlgorithmOmega(string style) | ||
| { | ||
| Style = style; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.