Created
          March 26, 2012 13:26 
        
      - 
      
- 
        Save madd0/2205035 to your computer and use it in GitHub Desktop. 
    Provides access to configuration files for applications that can be used on the cloud and on premise.
  
        
  
    
      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
    
  
  
    
  | namespace Madd0.Azure | |
| { | |
| using System; | |
| using System.Configuration; | |
| using System.Globalization; | |
| using Microsoft.WindowsAzure.ServiceRuntime; | |
| /// <summary> | |
| /// Provides access to configuration files for applications that can be used on the cloud and on premise. | |
| /// </summary> | |
| public class ApplicationConfiguration | |
| { | |
| /// <summary> | |
| /// Holds the singleton instance of the class. | |
| /// </summary> | |
| private static readonly ApplicationConfiguration instance = new ApplicationConfiguration(); | |
| /// <summary> | |
| /// Prevents a default instance of the <see cref="ApplicationConfiguration"/> class from being created. | |
| /// </summary> | |
| private ApplicationConfiguration() | |
| { | |
| this.ConnectionStrings = new SettingsBag(s => ConfigurationManager.ConnectionStrings[s].ConnectionString); | |
| this.AppSettings = new SettingsBag(s => ConfigurationManager.AppSettings[s]); | |
| } | |
| /// <summary> | |
| /// Gets the current instance of the class. | |
| /// </summary> | |
| public static ApplicationConfiguration Current | |
| { | |
| get | |
| { | |
| return instance; | |
| } | |
| } | |
| /// <summary> | |
| /// Gets a connection string by providing indexed access to the application's settings. | |
| /// </summary> | |
| public SettingsBag ConnectionStrings { get; private set; } | |
| /// <summary> | |
| /// Gets a value from <c>appSettings</c> section by providing indexed access to the application's settings. | |
| /// </summary> | |
| public SettingsBag AppSettings { get; private set; } | |
| /// <summary> | |
| /// Retrieves the value of a setting in the application's configuration. | |
| /// </summary> | |
| /// <typeparam name="T">The expected type of the value.</typeparam> | |
| /// <param name="settingName">Name of the setting.</param> | |
| /// <returns>The value of a setting in the application's configuration.</returns> | |
| public static T GetConfigurationSettingValue<T>(string settingName) | |
| where T : IConvertible | |
| { | |
| return ApplicationConfiguration.GetConfigurationSettingValue<T>(settingName, null); | |
| } | |
| /// <summary> | |
| /// Retrieves the value of a setting in the application's configuration. A fallback container | |
| /// derived from <see cref="ApplicationSettingsBase"/> can be used to retrieve the setting | |
| /// if the current environment is not a <see cref="RoleEnvironment"/>. | |
| /// </summary> | |
| /// <typeparam name="T">The expected type of the value.</typeparam> | |
| /// <param name="settingName">Name of the setting.</param> | |
| /// <param name="fallbackSettings">The fallback settings.</param> | |
| /// <returns>The value of a setting in the application's configuration.</returns> | |
| public static T GetConfigurationSettingValue<T>(string settingName, SettingsBase fallbackSettings) | |
| where T : IConvertible | |
| { | |
| object value = null; | |
| if (fallbackSettings == null || RoleEnvironment.IsAvailable) | |
| { | |
| value = RoleEnvironment.GetConfigurationSettingValue(settingName); | |
| } | |
| else | |
| { | |
| value = fallbackSettings[settingName]; | |
| } | |
| return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture); | |
| } | |
| /// <summary> | |
| /// Provides access to keyed settings from a role environment or by calling a fallback callback. | |
| /// </summary> | |
| [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible", Justification = "Type must be public for parent type properties to be public and nested to be easily redistributable.")] | |
| public class SettingsBag | |
| { | |
| /// <summary> | |
| /// Holds the callback to be used in case the environment is not a RoleEnvironment. | |
| /// </summary> | |
| private Func<string, string> _fallbackCallback; | |
| /// <summary> | |
| /// Initializes a new instance of the <see cref="SettingsBag"/> class. | |
| /// </summary> | |
| /// <param name="fallbackCallback">The fallback callback.</param> | |
| internal SettingsBag(Func<string, string> fallbackCallback) | |
| { | |
| this._fallbackCallback = fallbackCallback; | |
| } | |
| /// <summary> | |
| /// Gets the <see cref="System.String"/> with the specified setting name. | |
| /// </summary> | |
| /// <param name="settingName">The <see cref="C:System.String"/> key of the entry to locate.</param> | |
| public string this[string settingName] | |
| { | |
| get | |
| { | |
| string result = null; | |
| // Check whether the application is executing within a role environment (Azure/Local fabric) | |
| if (RoleEnvironment.IsAvailable) | |
| { | |
| // Get value from CsDef | |
| result = RoleEnvironment.GetConfigurationSettingValue(settingName); | |
| } | |
| else | |
| { | |
| // Get value from app.config | |
| result = this._fallbackCallback(settingName); | |
| } | |
| return result; | |
| } | |
| } | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment