Skip to content

Instantly share code, notes, and snippets.

@rarous
Created June 11, 2013 15:19
Show Gist options
  • Save rarous/5757736 to your computer and use it in GitHub Desktop.
Save rarous/5757736 to your computer and use it in GitHub Desktop.
Feature toggles support in Windsor container
using System;
using System.Linq;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
namespace Project.Installers {
public static class FeatureToggles {
static readonly string[] OnStates = { "on", "enabled" };
static readonly IRegistration FeatureOff = new SkipRegistration();
public static IRegistration FeatureToggle(this IRegistration feature, Parameter toggle) {
if (IsEnabled(toggle.Value))
return feature;
return FeatureOff;
}
public static Predicate<Type> IsEnabled(Parameter toggle) {
if (IsEnabled(toggle.Value))
return _ => true;
return _ => false;
}
static bool IsEnabled(string value) {
return value == null || OnStates.Contains(value.Trim().ToLowerInvariant());
}
}
class SkipRegistration : IRegistration {
public void Register(IKernelInternal kernel) { }
}
}
Component.
For<IService>().
ImplementedBy<Service>().
FeatureToggle(Dependency.OnAppSettingsValue("FeatureToggle")),
Classes.
FromAssemblyInDirectory(new AssemblyFilter(".")).
BasedOn<ServiceBase>().
If(FeatureToggles.IsEnabled(Dependency.OnAppSettingsValue("FeatureToggle"))).
WithServiceBase(),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment