Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
StructureMap magic to make AppSettings suck less.
public class FeatureToggleRegistry : Registry
{
public FeatureToggleRegistry()
{
Scan(x =>
{
x.TheCallingAssembly();
x.Convention<SettingsConvention>();
});
}
}
public class SettingsConvention : IRegistrationConvention
{
public void Process(Type type, Registry registry)
{
if (type.Name.EndsWith("Settings") && type.IsConcreteWithDefaultCtor())
{
registry.For(type).Use(typeof(SettingsInstance<>).CloseAndBuildAs<Instance>(type));
}
}
}
public class SettingsInstance<T> : Instance where T : class, new()
{
protected override string getDescription()
{
return "{0} from {1}".ToFormat(typeof(T).Name, typeof(AppSettingsProvider).Name);
}
protected override object build(Type pluginType, BuildSession session)
{
return session.GetInstance<AppSettingsProvider>().SettingsFor<T>();
}
}
Be sure to throw this in your config file
<appSettings>
<add key="FeatureSettings.SpecialFeatureEnabled" value="true"/>
<add key="FeatureSettings.SomeOtherFeatureEnabled" value="false"/>
</appSettings>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment