开发者

How do you wrap and access the config files from your applications?

开发者 https://www.devze.com 2022-12-17 17:49 出处:网络
Well, the question speaks for itself. I use to have a static Config class in my project that lazy-load the data from the config file like this :

Well, the question speaks for itself. I use to have a static Config class in my project that lazy-load the data from the config file like this :

    private static string _foo;
    public static string Foo
    {
        get
        {
            if (string.IsNullOrEmpty(_foo))
                _foo = ConfigurationManager.AppSettings["foo"];
            return _foo开发者_如何学编程;
        }
        set
        {
            _foo = value;
        }
    }

That way in my tests I can just set up the Config class the way I want and it won't need to access the filesystem. I also parse to specific types, or to Enums for example.

Do you use a better approach? please share!


I am used to the Settings : System.Configuration.ApplicationSettingsBase.

  1. It supports strong typing and
  2. is already encapsulated so you don't need to worry, just access the values
  3. and it is also integrated with the IDE.
  4. and it supports both Application and User settings
  5. You can use custom settings for the test project

For more info please look at http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx


What makes you think you need to use lazy loading? Are you assuming there would be a performance problem if you accessed the config each time? Try it and find out? You may find that it's fast enough. You may even find that the data are cached internally.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号