Created
December 17, 2013 04:31
-
-
Save droyad/8000065 to your computer and use it in GitHub Desktop.
CombiningSettingsReader
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 CombiningSettingsReader : ISettingsReader | |
| { | |
| private readonly ISettingsReader[] _settingsReaders; | |
| public CombiningSettingsReader(params ISettingsReader[] settingsReaders) | |
| { | |
| _settingsReaders = settingsReaders; | |
| } | |
| public string ReadValue(string key) | |
| { | |
| return _settingsReaders.Select(r => r.ReadValue(key)).FirstOrDefault(v => v != null); | |
| } | |
| public IEnumerable<string> AllKeys | |
| { | |
| get { return _settingsReaders.SelectMany(r => r.AllKeys).Distinct(); } | |
| } | |
| } | |
| public class ConnectionStringsSettingsReader : ISettingsReader | |
| { | |
| private readonly Lazy<Dictionary<string, string>> _settings; | |
| public ConnectionStringsSettingsReader() | |
| { | |
| _settings = new Lazy<Dictionary<string, string>>(ReadSettingsFromConfigFile); | |
| } | |
| public string ReadValue(string key) | |
| { | |
| string value; | |
| _settings.Value.TryGetValue(key, out value); | |
| return value; | |
| } | |
| public IEnumerable<string> AllKeys | |
| { | |
| get { return _settings.Value.Keys; } | |
| } | |
| private Dictionary<string, string> ReadSettingsFromConfigFile() | |
| { | |
| return ConfigurationManager.ConnectionStrings.OfType<ConnectionStringSettings>() | |
| .ToDictionary(k => k.Name, k => k.ConnectionString); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment