Feature Flags in .Net
In DevOps era, where we want to do continuous releases and where feature branches are created only for pull requests and not for long lived feature development. We need to use feature flags.
Feature flagging allows developers to take full control of their feature lifecycles independent of code deployments. Application features can be enabled or disabled without requiring code to be reverted or redeployed.
It also enables us to deploy the features to only certain users of the application Instead of rolling out features at once to all the users of the application.
We can implement feature toggle without using third party tools by just having the Feature Toggle specified in the web.config and checking the visibility of the feature based on the value specified in web.config.
This has a drawback that it does not provide a compile time check in case we do not specify the corerct Feature Toggle values and hence these issues only are reported in run time
So we can use open source library Feature Toggles and paid library LaunchDarkly
Feature Toggles
We can use the FeatureToggle nuget package available to implement feature toggle in our application
https://www.nuget.org/packages/FeatureToggle/
We need to add the feature toggle configuration value in web.config as specified below
<appSettings>
<add key="FeatureToggle.HideAlbumsFeatureToggle" value="false" />
</appSettings>
Create a class and inherit based on the type of toggle as shown below. This class will know how to read the configuration value from web.config
using FeatureToggle.Toggles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcMusicStore.FeaturetoggleSwitches
{
public class HideAlbumsFeatureToggle : SimpleFeatureToggle
{
}
}
Next we will get initialize the class in a property as shown below
public HideAlbumsFeatureToggle HideAlbumsFeatureToggle {
get
{
return new HideAlbumsFeatureToggle();
}
}
Next we will enable or disable the feature using the FeatureEnabled property as shown below
<ul id="album-list">
@foreach (var album in Model)
{
if (album.HideAlbumsFeatureToggle.FeatureEnabled)
{
<li><a href="@Url.Action("Details", "Store",
new { id = album.AlbumId })">
<img alt="@album.Title" src="@album.AlbumArtUrl" />
<span>@album.Title</span> </a>
</li>
}
}
</ul>
LaunchDarkly
You can also control the feature flag centrally and can be managed by non-developers and which fully integrates with VSTS by using the LaunckDarkly extension specified here https://blog.launchdarkly.com/tag/vsts/
Senior Member Of Technical Staff at VMware
7 年Will try this out.. thanks for the post !
?? Product Owner Mobiliteit @ Independer | ?? Connector of people, always on, gets energy from creative, valuable and efficient solutions!
7 年Jaco van der Nat