FeatureToggle
FeatureToggle is a popular feature toggle package that has a good architecture, regular updates, a training video, doesn't use magic strings, and is extensible. The documentation can be found
here.
FeatureToggle Review
Multiple platforms: .NET Desktop/Server, Windows phone, Windows Store
No magic strings
No default fallback values - throws exception
Flexible provider model to allow for swapping out of parts.
Straight forward to use
Extensible via Providers and Custom Toggles.
Downside
I don't like that I have to specify the connection string that I want to use for each toggle. It would be nice if it could use a default connection string instead. It seems a bit clunky to specify a key for the feature toggle AND one to point to the database connection string. Perhaps that can be changed.
Support Configurations
- Compiled
- Local configuration (app.config / web.config or App.xaml)
- Centralized SQL Server
Architecture
Built-in Toggle classes use providers to get configurations out specific sources such as database, configuration file, etc.
Strongly typed objects => Compile time checks to make sure it is completely removed.
Built-in Toggles
- AlwaysOnFeatureToggle
- AlwaysOffFeatureToggle
- SimpleFeatureToggle
- EnableOnOrAfterDateFeatureToggle
- EnableOnOrBeforeDateFeatureToggle
- EnableBetweenDatesFeatuerToggle
- EnableOnDaysOfWeekFeatureToggle
- RandomFeatureToggle - could use for random a/b testing
- SqlFeatureToggle - toggle from value in SQL Server database
Installation
Nuget: FeatureToggle
NOTE: FeatureToggle.Core is installed when FeatureToggle is installed.
Overview of Usage
For each feature that a feature toggle is required, a new class that inherits from one of the built-in Toggle classes is required.
Compiled Toggle Usage
public class MyFeature : AlwaysOnFeatureToggle {}
In place where want to use it, create a new instance of the MyFeature class.
i.e.
public MyFeature MyFeature1 = new MyFeature();
Can add to ViewModel to use on Razor page.
i.e.
@if (Model.MyFeature1.FeatureEnabled)
{
....html here
}
Config File Usage
In this example it would be the same as above, except we change the class we inherit from so that we can get the value from the config file. Note, the ViewModel and Razor page did not need to change even though we are changing what the logic is for hiding the feature.
i.e.
public class MyFeature : SimpleFeatureToggle{}
We do need to add it to the configuration file. We just need to add a new key to the appSetting. It must follow the convention such that the key starts with "FeatureToggle." and concatenated to the name of our class (MyFeature in this case).
i.e.
<appSetting>
<add key="FeatureToggle.MyFeature" value="true"/>
</appSettings>
SqlFeature Toggle Usage
We will continue our example as before, except we need to also tell the FeatureToggle where the database is.
In a database create a table called Toggles
ToggleName nvarchar(100) not null (primary key)
Value bit not null
NOTE: The table and column names or types are not important since we will write the SQL to access it later.
We need to add the connection string to the list of connection strings in the web.config.
<connectionString>
<add name="MyDB" connectionString="typical connection string here" />
</connectionString>
Insert a record with the ToggleName being the name of our class (MyFeature in this case) and value = True.
We need to specify the connection string in the web.config. We put it in as an appSetting key-value as
<appSettings>
<add key="FeatureToggle.MyFeature.ConnectionStringName" value="MyDB" />
<add key="FeatureToggle.MyFeature.SqlStatement" value="select value from Toggles where ToggleName = 'MyFeature'" />
</appSettings>
Removing a Toggle
Delete the class. Rebuild project. Review each compiler error. Remove from web.config.
Creating a Custom Toggle
Custom Toggles are good when the value is based on some business logic.
Continuing on the examples...
public class MyBusinessLogicToggle : IFeatureToggle
{
public bool FeatureEnabled { get { return businessLogicHere;}}
}
Change our Feature Toggle so that it inherits from the custom feature toggle. This can be reused with multiple feature toggles;
public class MyFeature : MyBusinessLogicToggle {}
Creating a Custom Provider
The base class for Feature Toggles has a property called ToggleValueProvider. Setting this value to a custom Provider allows us to change the default provider to a custom one.
We can create a custom provider by creating a subclass of a existing provider.
i.e.
public MyProvider : IBooleanToggleValueProvider
{
public bool EvaluateBooleanToggleValue(IFeature toggle)
{
return logicHere;
}
}
Some Alternatives
References