Master C#

How to implement a feature toggle in C#

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

How to implement feature toggling in C#
Scigola (CC0)

Master C#

Show More

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:

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

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”);
                Console.WriteLine(“The feature is turned off”);

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

Copyright © 2017 IDG Communications, Inc.