From fdd29a4cf99556733f795c2bf3be424159795a57 Mon Sep 17 00:00:00 2001 From: zhiyuanliang Date: Mon, 9 Oct 2023 15:36:38 +0800 Subject: [PATCH 1/4] update the recommended way to reference feature flag name --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 49c2ec6f..22a8b81f 100644 --- a/README.md +++ b/README.md @@ -144,12 +144,12 @@ In the above example, `FeatureW` specifies a `RequirementType` of `All`, meaning To make it easier to reference these feature flags in code, we recommend to define feature flag variables like below. ``` C# -// Define feature flags in an enum -public enum MyFeatureFlags +// Define feature flags using const strings +public static class MyFeatureFlags { - FeatureT, - FeatureU, - FeatureV + public const string FeatureT = "FeatureT"; + public const string FeatureU = "FeatureU"; + public const string FeatureV = "FeatureV"; } ``` From c3cc21a80b2e5b48122e45c1679b77e430822bff Mon Sep 17 00:00:00 2001 From: zhiyuanliang Date: Tue, 10 Oct 2023 10:02:05 +0800 Subject: [PATCH 2/4] remove nameof --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 22a8b81f..7aab8eaf 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ The basic form of feature management is checking if a feature is enabled and the … IFeatureManager featureManager; … -if (await featureManager.IsEnabledAsync(nameof(MyFeatureFlags.FeatureU))) +if (await featureManager.IsEnabledAsync(MyFeatureFlags.FeatureU)) { // Do something } @@ -251,7 +251,7 @@ public interface IDisabledFeaturesHandler In MVC views `` tags can be used to conditionally render content based on whether a feature is enabled or not. ``` HTML+Razor - +

This can only be seen if 'FeatureX' is enabled.

``` @@ -269,7 +269,7 @@ The feature management pipeline supports async MVC Action filters, which impleme ``` C# services.AddMvc(o => { - o.Filters.AddForFeature(nameof(MyFeatureFlags.FeatureV)); + o.Filters.AddForFeature(MyFeatureFlags.FeatureV); }); ``` @@ -297,7 +297,7 @@ When used on Razor pages, the `FeatureGateAttribute` must be placed on the page The feature management library can be used to add application branches and middleware that execute conditionally based on feature state. ``` C# -app.UseMiddlewareForFeature(nameof(MyFeatureFlags.FeatureU)); +app.UseMiddlewareForFeature(MyFeatureFlags.FeatureU); ``` With the above call, the application adds a middleware component that only appears in the request pipeline if the feature "FeatureU" is enabled. If the feature is enabled/disabled during runtime, the middleware pipeline can be changed dynamically. From d2c79ea288a41013fdb95f6f22aad4c707d28416 Mon Sep 17 00:00:00 2001 From: zhiyuanliang Date: Wed, 11 Oct 2023 11:22:38 +0800 Subject: [PATCH 3/4] remove recommended referencing way --- README.md | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 7aab8eaf..2267161f 100644 --- a/README.md +++ b/README.md @@ -141,17 +141,7 @@ In the above example, `FeatureW` specifies a `RequirementType` of `All`, meaning ### Referencing -To make it easier to reference these feature flags in code, we recommend to define feature flag variables like below. - -``` C# -// Define feature flags using const strings -public static class MyFeatureFlags -{ - public const string FeatureT = "FeatureT"; - public const string FeatureU = "FeatureU"; - public const string FeatureV = "FeatureV"; -} -``` +In the following code examples, MyFeatureFlags.FeatureX which is a const string property of the MyFeatureFlags class, is used for referencing the name of feature flag "FeatureX". ### Service Registration @@ -187,7 +177,7 @@ The basic form of feature management is checking if a feature is enabled and the … IFeatureManager featureManager; … -if (await featureManager.IsEnabledAsync(MyFeatureFlags.FeatureU)) +if (await featureManager.IsEnabledAsync(MyFeatureFlags.FeatureX)) { // Do something } @@ -226,14 +216,14 @@ public class HomeController : Controller The `HomeController` above is gated by "FeatureX". "FeatureX" must be enabled before any action the `HomeController` contains can be executed. ``` C# -[FeatureGate(MyFeatureFlags.FeatureY)] +[FeatureGate(MyFeatureFlags.FeatureX)] public IActionResult Index() { return View(); } ``` -The `Index` MVC action above requires "FeatureY" to be enabled before it can execute. +The `Index` MVC action above requires "FeatureX" to be enabled before it can execute. ### Disabled Action Handling @@ -269,17 +259,17 @@ The feature management pipeline supports async MVC Action filters, which impleme ``` C# services.AddMvc(o => { - o.Filters.AddForFeature(MyFeatureFlags.FeatureV); + o.Filters.AddForFeature(MyFeatureFlags.FeatureX); }); ``` -The code above adds an MVC filter named `SomeMvcFilter`. This filter is only triggered within the MVC pipeline if the feature it specifies, "FeatureV", is enabled. +The code above adds an MVC filter named `SomeMvcFilter`. This filter is only triggered within the MVC pipeline if the feature it specifies, "FeatureX", is enabled. ### Razor Pages MVC Razor pages can require that a given feature, or one of any list of features, be enabled in order to execute. This can be done by using a `FeatureGateAttribute`, which can be found in the `Microsoft.FeatureManagement.Mvc` namespace. ``` C# -[FeatureGate(MyFeatureFlags.FeatureU)] +[FeatureGate(MyFeatureFlags.FeatureX)] public class IndexModel : PageModel { public void OnGet() @@ -288,7 +278,7 @@ public class IndexModel : PageModel } ``` -The code above sets up a Razor page to require the "FeatureU" to be enabled. If the feature is not enabled, the page will generate an HTTP 404 (NotFound) result. +The code above sets up a Razor page to require the "FeatureX" to be enabled. If the feature is not enabled, the page will generate an HTTP 404 (NotFound) result. When used on Razor pages, the `FeatureGateAttribute` must be placed on the page handler type. It cannot be placed on individual handler methods. @@ -297,10 +287,10 @@ When used on Razor pages, the `FeatureGateAttribute` must be placed on the page The feature management library can be used to add application branches and middleware that execute conditionally based on feature state. ``` C# -app.UseMiddlewareForFeature(MyFeatureFlags.FeatureU); +app.UseMiddlewareForFeature(MyFeatureFlags.FeatureX); ``` -With the above call, the application adds a middleware component that only appears in the request pipeline if the feature "FeatureU" is enabled. If the feature is enabled/disabled during runtime, the middleware pipeline can be changed dynamically. +With the above call, the application adds a middleware component that only appears in the request pipeline if the feature "FeatureX" is enabled. If the feature is enabled/disabled during runtime, the middleware pipeline can be changed dynamically. This builds off the more generic capability to branch the entire application based on a feature. From 8905db319a46a1faf52bbc3c3d7e24369d7e22c9 Mon Sep 17 00:00:00 2001 From: zhiyuanliang Date: Thu, 12 Oct 2023 10:03:03 +0800 Subject: [PATCH 4/4] using string literals directly --- README.md | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2267161f..6b74ea23 100644 --- a/README.md +++ b/README.md @@ -138,10 +138,6 @@ A `RequirementType` of `All` changes the traversal. First, if there are no filte ``` In the above example, `FeatureW` specifies a `RequirementType` of `All`, meaning all of it's filters must evaluate to true for the feature to be enabled. In this case, the feature will be enabled for 50% of users during the specified time window. - -### Referencing - -In the following code examples, MyFeatureFlags.FeatureX which is a const string property of the MyFeatureFlags class, is used for referencing the name of feature flag "FeatureX". ### Service Registration @@ -177,7 +173,7 @@ The basic form of feature management is checking if a feature is enabled and the … IFeatureManager featureManager; … -if (await featureManager.IsEnabledAsync(MyFeatureFlags.FeatureX)) +if (await featureManager.IsEnabledAsync("FeatureX")) { // Do something } @@ -206,7 +202,7 @@ The feature management library provides functionality in ASP.NET Core and MVC to MVC controller and actions can require that a given feature, or one of any list of features, be enabled in order to execute. This can be done by using a `FeatureGateAttribute`, which can be found in the `Microsoft.FeatureManagement.Mvc` namespace. ``` C# -[FeatureGate(MyFeatureFlags.FeatureX)] +[FeatureGate("FeatureX")] public class HomeController : Controller { … @@ -216,7 +212,7 @@ public class HomeController : Controller The `HomeController` above is gated by "FeatureX". "FeatureX" must be enabled before any action the `HomeController` contains can be executed. ``` C# -[FeatureGate(MyFeatureFlags.FeatureX)] +[FeatureGate("FeatureX")] public IActionResult Index() { return View(); @@ -241,7 +237,7 @@ public interface IDisabledFeaturesHandler In MVC views `` tags can be used to conditionally render content based on whether a feature is enabled or not. ``` HTML+Razor - +

This can only be seen if 'FeatureX' is enabled.

``` @@ -259,7 +255,7 @@ The feature management pipeline supports async MVC Action filters, which impleme ``` C# services.AddMvc(o => { - o.Filters.AddForFeature(MyFeatureFlags.FeatureX); + o.Filters.AddForFeature("FeatureX"); }); ``` @@ -269,7 +265,7 @@ The code above adds an MVC filter named `SomeMvcFilter`. This filter is only tri MVC Razor pages can require that a given feature, or one of any list of features, be enabled in order to execute. This can be done by using a `FeatureGateAttribute`, which can be found in the `Microsoft.FeatureManagement.Mvc` namespace. ``` C# -[FeatureGate(MyFeatureFlags.FeatureX)] +[FeatureGate("FeatureX")] public class IndexModel : PageModel { public void OnGet() @@ -287,7 +283,7 @@ When used on Razor pages, the `FeatureGateAttribute` must be placed on the page The feature management library can be used to add application branches and middleware that execute conditionally based on feature state. ``` C# -app.UseMiddlewareForFeature(MyFeatureFlags.FeatureX); +app.UseMiddlewareForFeature("FeatureX"); ``` With the above call, the application adds a middleware component that only appears in the request pipeline if the feature "FeatureX" is enabled. If the feature is enabled/disabled during runtime, the middleware pipeline can be changed dynamically.