When you work with a team of developers on a large application across multiple projects, everyone is going to need access to unique configuration settings. Often this will give rise to each project having it's own separate configuration services, or worse everyone will just call the static ConfiguationManager directly.
How do you share a single configuration object across projects?
The answer is simple: extension methods! Just create a generic configuration service in your core library, and then have all of your other projects call it via extension methods. This ensures that you only have one testable, injectable, and extensible source for configuration. Additionally it keeps all of your configuration both discoverable and abstracted to their consuming project.
public interface IConfigService
{
T Get<T>(string key);
T Get<T>(string key, T defaultValue);
}
public static class ConfigServiceExtensions
{
private const string NotificationsKey = "AreNotificationsEnabled";
private const bool NotificationsDefault = true;
public static bool AreNotificationsEnabled(this IConfigService config)
{
return config.Get(NotificationsKey, NotificationsDefault);
}
}
Remember, every project can have it's own set of ConfigService extension methods. :)