Last active
December 13, 2015 20:19
-
-
Save CoreyKaylor/4969386 to your computer and use it in GitHub Desktop.
StructureMap magic to make AppSettings suck less.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FeatureToggleRegistry : Registry | |
{ | |
public FeatureToggleRegistry() | |
{ | |
Scan(x => | |
{ | |
x.TheCallingAssembly(); | |
x.Convention<SettingsConvention>(); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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