How do you wrap and access the config files from your applications?
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
.
- It supports strong typing and
- is already encapsulated so you don't need to worry, just access the values
- and it is also integrated with the IDE.
- and it supports both Application and User settings
- 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.
精彩评论