joydip_kanjilal
Contributor

How to implement a feature toggle in C#

how-to
Dec 11, 20174 mins
C#Small and Medium BusinessSoftware Development

Take advantage of feature toggles to manage application features without having to maintain multiple source code branches

Feature toggling is a nice concept that facilitates trunk-based development. You can use feature toggles (also known as feature switches, feature flags, and feature flippers) to test a new feature in the main branch even before it is ready for release. Feature flags have many other uses as well, such as hiding advanced features from users of a free or basic version of an application. 

Feature toggles allow you to keep your production-ready code base and your development code base more closely in sync. Depending on the dynamicity and durability of a feature, you can draw on different kinds of toggles such as release toggles, experimental toggles, permissioning toggles, and ops toggles, to name a few.

This article presents an overview of feature toggling and how we can adopt this technique to manage features in our applications without having to manage and maintain and (even more daunting) eventually merge multiple source code branches.

Feature toggle libraries for .Net

Implementing feature toggling isn’t difficult. There are many feature toggle libraries in .Net to choose from including NFeature, nToggle, FeatureToggle, and FeatureSwitcher. Of these, I prefer using the open source FeatureToggle library written by Jason Roberts. FeatureToggle is simple, easy to use, and extensible. You can take advantage of this library to implement standard or custom feature toggling in your application with ease. Note that you can use FeatureToggle with .Net Core applications as well.

Install FeatureToggle

To get started using FeatureToggle, open Visual Studio and click on File -> New -> Project. Create a new console application project and save the project with a name. 

Now, install FeatureToggle via the NuGet Package Manager console using the following command:

PM> Install-Package FeatureToggle

Alternatively, you can select your new project in the Solution Explorer window, right-click the Manage NuGet Packages option, browse for the FeatureToggle package, and install it from NuGet Package Manager.

Use FeatureToggle to create a custom feature

Now that you have included the FeatureToggle package in your project, here’s how you can start building a feature by extending the SimpleFeatureToggle class of the FeatureToggle library.

public class CustomFeature : SimpleFeatureToggle
    {
          //TODO: Some code
    }

I will skip the code for the custom feature here and instead focus on how this feature can be configured and used. The SimpleFeatureToggle class extends the IFeatureToggle interface, and the CustomFeature class extends the SimpleFeatureToggle class. 

The feature toggle flags can reside in a file or a database. In this example, we will store the feature toggle flags in the application’s configuration file. Here is how you can define the features you want to turn on or off in the configuration file:

 <appSettings>
        <add key="FeatureToggle.CustomFeature" value="True" />
 </appSettings>

In the code snippet above, the value True for the FeatureToggle.CustomFeature key indicates that our custom feature is turned on. Again, note that CustomFeature is our custom feature toggle class that extends the SimpleFeatureToggle class mentioned earlier.

Use FeatureToggle to turn a feature on or off

The SimpleFeatureToggle class will read the configuraton information specified in your application’s configuration file and turn that feature on or off. The following code snippet shows how it all works.

static void Main(string[] args)
        {
            var feature = new CustomFeature();
            if (feature.FeatureEnabled)
            {
                Console.WriteLine(“The feature is turned on”);
            }
            else
            {
                Console.WriteLine(“The feature is turned off”);
            }
            Console.Read();
        }

When you run the above code snippet, the appropriate message will be displayed to indicate whether the custom feature we implemented is turned on or off in the application’s configuration file.

If you have many features to manage, a better method is to use dependency injection. By using an IoC container in your application to handle the features, you can register all of the feature toggles and then take advantage of dependency injection to inject the features whenever they should be turned on.

Feature toggle pros and cons

While feature toggling helps you to solve many of the merge problems and maintenance issues associated with managing multiple source code branches, it can make the code more fragile, less secure, and harder to test and maintain over time.

Because feature toggling adds complexity to your application’s code, it is a good practice not to enable a feature that is in early development or incomplete. Note that feature toggles should be removed after the feature has been tested, released, and proven stable. 

For more on the subject, I recommend reading this article on FeatureToggle by Martin Fowler

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author