This is the second part in a series.
EDIT: my Feature Management Pluralsight training course is now available.
One of the downsides of Microsoft’s feature flags/feature toggle implementation is the reliance on magic strings in code to query the status of features.
For example, the following code checks for the state of the Printing feature:
if (await _featureManager.IsEnabledAsync("Printing"))
{
ViewData["PrintMessage"] = "On";
}
else
{
ViewData["PrintMessage"] = "Off";
}
Notice in the preceding code that the feature is referenced with the string “Printing”. These kind of magic strings in code can be problematic because they have no sematic compiler meaning, for example if the feature is mentioned in multiple places and the name of the feature changes in configuration you would need to update all the strings in the codebase. It is easy to miss one and then end up with errors in the app.
In the documentation, the recommended approach is to define an enum containing the feature you want to control, for example:
public enum Features
{
Printing,
QuickQuotes,
OnlineChat
}
Now the code can be refactored to make use of the enum in conjunction with a nameof expression:
if (await _featureManager.IsEnabledAsync(nameof(Features.Printing)))
{
ViewData["PrintMessage"] = "On";
}
else
{
ViewData["PrintMessage"] = "Off";
}
There are a number of things that can (and should) happen when using feature toggles/flags. One of which is that when a feature becomes permanent then the toggle and associated code should be removed. First this means removing it from configuration, which as I mentioned in part 1 will not actually break anything. The next step will be to remove it from the enum, doing this will actually break compilation (which is a good thing) if the enum value is referenced in a nameof expression. The next step is to fix compilation errors by removing the conditional code that relies on the flag/toggle.
In part 1 I mentioned the <feature> tag helper that can be used to enable/disable UI elements based on a flag:
<feature name="Printing">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Print">Print Preview</a>
</li>
</feature>
Notice in the preceding HTML that the name of the feature is still represented as a magic string “Printing”. If the enum value and config are removed, compilation will not beak if this HTML still exists. We can fix this by one again using nameof in the view:
<feature name="@nameof(Features.Printing)">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Print">Print Preview</a>
</li>
</feature>
Now if we removed Printing from the enum we get a compilation error and we won’t accidentally release an app to production with incorrect UI elements.
Be sure to check out my Microsoft Feature Management Pluralsight course get started watching with a free trial.
SHARE: