Thomas Bandt

Simple Feature Toggles For Xamarin Apps (And Everything Else)

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 Monday, March 9, 2015

Endless Use Cases

Think of:

  • Disabling features that are not completely ready before publishing a new release
  • Disabling features that don't work as expected in production
  • Enabling features only for some selected users (A/B testing)
  • Enabling features depending on purchased license
  • ...

Get Started

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 ...

An Interface To Mark Your Features

public interface IFeature { }

An Object That Holds The Information Whether A Feature Is Enabled Or Not

public class FeatureToggle<T> where T : IFeature
{
    public bool Enabled { get; private set; }

    public FeatureToggle(bool enabled)
    {
        Enabled = enabled;
    }
}

A Place To Register All Your Features

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;
    }
}

Something That Tells You Whether A Specific Feature Can Be Used And Executed

public static class Feature
{
    public static bool IsEnabled<T>() where T : IFeature
    {
        return FeatureToggleRegistry.ContainsEnabled<T>();
    }
}

That's All

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);
}

Sample Project

Find a simple sample project on GitHub (a Xamarin.iOS app).

What do you think? Drop me a line and let me know!