开发者

Benefits of .Net's AppSettingsReader vs ConfigurationManager for reading application configuration settings

开发者 https://www.devze.com 2023-01-11 16:03 出处:网络
Is there a substantial difference between the AppSettingsReader开发者_C百科 class and the AppSettings member of the ConfigurationManager class in .Net 3.5?

Is there a substantial difference between the AppSettingsReader开发者_C百科 class and the AppSettings member of the ConfigurationManager class in .Net 3.5?

I'm building out some legacy code and a previous developer used AppSettingsReader.GetValue(), whereas I am more prone to use ConfigurationManager.AppSettings.Get().

Looking at the internals, AppSettingReader seems to be more typesafe, but its usage seems slightly more verbose. If the app settings I'm retrieving are well-known and fairly static, is there any benefit to using AppSettingsReader?


Not really. Internally AppSettingsReader just used the ConfigurationManager.AppSettings.

There is basically just a bit of fluff around checking that it exists, checking that what is going on is ok etc.

One thing is though that it will throw an exception when there is no element in the AppSettings..

    // From CTOR
    this.map = ConfigurationManager.AppSettings;

    ....
    public object GetValue(string key, Type type)

       ...

       string item = this.map[key];
       if (item == null)
       {
           throw new InvalidOperationException(SR.GetString("AppSettingsReaderNoKey", new object[] { key }));
       }

I think that most people will just use ConfigurationManager but the real answer is 'wahtever floats your boat'.

0

精彩评论

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