Search code examples
c#staticsettingsapplication-settings

Custom application settings


I want to be able to have a Settings class that is available throughout the entire application, however I still want to be able to test that the settings object gets populated. Most of the settings come from the registry.

At the moment I have this but I'm not sure where to populate that, so that I can mock the service used to populate it.

public class Program {
    public static Settings SystemSettings = new SystemSettings();

    public void Main(string[] args) {
         SystemSettings = new RegistryService().GetRegSettings();
    }
}

Any thoughts on this would be great.


Solution

  • Create a singleton class ,all class in your application can access to it

    public class Settings
    {
        private static Settings systemSettings;
        public static Settings SystemSettings 
        {
            get
            {
                if (systemSettings == null)
                    systemSettings = new Settings();
                return systemSettings;
            }
        }
        public int SettingValue1{ get; set; }
        private Settings()
        {
            SettingValue1 = 1;//from registery or somewhere
        }
    
    
    }
    

    ans use this class easily

    int k = Settings.SystemSettings.SettingValue1;