Skip to content

jensandresen/togglr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Togglr

Just another feature toggle library for .net (mvc)

How it works

Each feature toggle is based on the mono state pattern. This means that you can (and should if needed) instantiate your feature toggles where ever you need to verify that a given feature is enabled or disabled. You just "new up" the targeted feature toggle and access its "IsEnabled" property. The mono state pattern enables that two or more instances of the same feature toggle class will return the same value from its "IsEnabled" property.

The framework will use the name of the feature toggle class as an identifier when retrieving the stored feature toggle state. It will ignore "FeatureToggle" in the end of an identifier. This means that a class named "SampleFeatureToggle" will have "Sample" as its identifier by default.

Feature toggle values are stored in a file located in your projects App_Data folder. This is the default configuration that you get out-of-the-box. The file format is very simple (see the section "Default file format" for more details). You add new feature toggles (and their value) to this file when ever you create new feature toggles. But you are not stuck with the default configuration. You can store the feature toggle values where ever you want and then just create a value provider that plugges into the Togglr-framework (see the section "Custom value provider" for more details).

Usage

Each feature toggle is defined as a c# class that inherites from the "FeatureToggle" -class provided by Togglr-framework.

Implementation of a sample feature toggle class:

public class SampleFeatureToggle : FeatureToggle
{

}

...and you're done!

If you're using the default configuration then you also need to add the following line to FeatureToggles.txt located in your App_Data folder:

Sample=on

You now have a feature toggle called "Sample" that is turned on. To turn it off just change the above line in FeatureToggles.txt to:

Sample=off

You can override the default identity of your feature toggles (which is the name of the class) by doing the following:

public class SampleFeatureToggle : FeatureToggle
{
  public override string Id
  {
    get { return "the new identifier"; }
  }
}

You can also override the mono state pattern and create your own implementation of how the value of a feature toggle is retrieved. In the following example an always-on feature toggle has been created:

public class SampleFeatureToggle : FeatureToggle
{
  public override bool IsEnabled
  {
    get { return true; }
  }
}

You can also temporarily override the value of a feature toggle in a web-request by added a parameter to the requested url. This enables you to get feedback on a given feature by sending a link to a friend/customer where a given feature is temporarily turned on. To do that you just add the parameter "EnableFeature" to the requested url and then you write a comma seperated list of the feature toggle identifiers that you wish to temporarily enable. In the following example the "Sample" feature toggle has been disabled (noted as Sample=off in the FeatureToggles.txt file):

http://your-application-url?EnableFeature=Sample

A request to the url above will temporarily enable the "Sample" feature toggle. You can then send that link to reviewers/approvers/etc.

And remember - you can seperate the identifiers by a comma like this:

http://your-application-url?EnableFeature=Sample1,Sample2,Sample3

The Togglr framework comes with extensions for the HtmlHelper class that is defined in the MVC framework. This lets you define the markup for your features in the following ways:

  1. Add your feature as a partial view and instead of calling
@Html.Partial("partialViewName)

you would do

@Html.PartialAsFeature("partialViewName", new SampleFeatureToggle())
  1. Add your feature as "inline" by defining it inside an if-scope like this:
@if (Html.IsFeatureEnabled(new SampleFeatureToggle()))
{
  <h1>this is markup for the sample feature</h1>
}

You have to include the following using-statement to your views:

using Togglr.Extensions

...I would suggest that you add the namespace to your web.config though.

Default file format

Each feature toggle is defined on its own line in the text file. The simple format of a feature toggle line is [identifier]=[on|off] as shown here:

Sample1=on
Sample2=off

You can also add comments to the file by starting the line with a #-sign as shown here:

# front page widgets
Sample1=on
Sample2=off

# about page widgets
Sample3=on
Sample4=off

Custom value provider

If you want to store your feature toggle values in a database you just create your own implementation of IFeatureToggleValueProvider and plug that into the Togglr engine when your application starts.

public class CustomValueProvider : IFeatureToggleValueProvider
{
  public FeatureToggleValue GetById(string id)
  {
    // access your database here...
    // return an instance of FeatureToggleValue
  }
}

Plug the value provider into the Togglr engine by creating a new configuration and apply that to the engine:

IConfiguration cfg = new ConfigurationBuilder()
  .WithValueProvider(new CustomValueProvider())
  .Build();

TogglrEngine.ApplyConfiguration(cfg);

...done.

About

A feature toggle library for .net

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages