Sunday, June 23, 2013

Using Mvc 4 Display Modes for Feature Toggles

It occurred to me that it's possible to use the new Mvc 4 feature Display Mode (meant for switching views for mobile versions) as a complement for Feature Toggles.

Obviously this can only switch out an entire view (or partial view) but it can be a nice alternative to putting lots of @if(featureEnabled) {} tags through your view.

Add the following code to your global.asax Application_Start (or create an App_Start module).

 DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("{NameOfFeature}")
     ContextCondition = (context => {true/false logic})

Then create 2 views - one for the feature being disabled and one for it being enabled - eg Index.cshtml and Index.{NameOfFeature}.cshtml.

This could also be used for similar concepts like permissions and a/b testing.