I consider continuous delivery a cool thing, particularly but not only in mobile app projects. It's nice to have the client see and touch new features as soon as they're ready. But sometimes it could be useful to disable those features. This is when Feature Toggles come into play.
Published on Mon, March 09, 2015
Think of, e.g.:
The idea behind Feature Toggles is nothing new and has been described by Martin Fowler more than four years ago for example. There are also tons of libraries out there, for example FeatureSwitcher (see the alternatives list there, too).
But to get things started, you don't need a more or less complex library. All what's needed, is ...
public interface IFeature { }
public class FeatureToggle<T> where T : IFeature
{
public bool Enabled { get; private set; }
public FeatureToggle(bool enabled)
{
Enabled = enabled;
}
}
public static class FeatureToggleRegistry
{
public static List<object> Features { get; private set; }
static FeatureToggleRegistry()
{
Features = new List<object>();
}
public static void Add<T>(bool enabled) where T : IFeature
{
Features.Add(new FeatureToggle<T>(enabled));
}
public static bool ContainsEnabled<T>() where T : IFeature
{
var feature = Features
.FirstOrDefault(f => f.GetType() == typeof(FeatureToggle<T>))
as FeatureToggle<T>;
return feature != null && feature.Enabled;
}
}
public static class Feature
{
public static bool IsEnabled<T>() where T : IFeature
{
return FeatureToggleRegistry.ContainsEnabled<T>();
}
}
To be honest, you could of course simplify it even a bit more, for example by merging Feature and FeatureToggleRegistry. But I like the clarity of that separated objects.
However, all you need to do now is to find a starting point in your application to register your features, for example the AppDelegate object in an iOS app.
public class BlueController : UIViewController, IFeature
{
}
// ...
FeatureToggleRegistry.Add<BlueController>(false);
// ...
if (Feature.IsEnabled<BlueController>())
{
PresentViewController(new BlueController(), true, null);
}
else
{
PresentViewController(new RedController(), true, null);
}
Find a simple sample project on GitHub (a Xamarin.iOS app).