Using values from AppConfig file in C#
selenium = new DefaultSelenium(
ConfigurationManager.AppSettings["TestMachine"].ToString(),
4444,
ConfigurationManager.AppSettings["Browser"].ToString(),
ConfigurationManager.AppSettings["URL"].ToString()
);
Is there an efficient way to do this, instead of repeating:
开发者_如何学GoConfigurationManager.AppSettings[""].ToString()
I think a better idea is to write a wrapper class to everything that deals with configuration, especially if you write tests. A simple example might be:
public interface IConfigurationService
{
string GetValue(string key);
}
This approach will allow you to mock your configuration when you need it and reduce complexity
So you could proceed with:
public void SelTest(IConfigurationService config)
{
var selenium = new DefaultSelenium(config.GetValue("TestMachine"),
4444, config.GetValue("Browser"), config.GetValue("URL"));
}
or you could inherit your configuration from a List and reduce the typing to:
config["Browser"]
Yes, you can do
ConfigurationManager.AppSettings[""]
As it is already a string.
If you're using ConfigurationManager.AppSettings["Something"] at multiple places, you should create a static Config class, that reads your AppSettings via static properties.
You can go to the properties of the project and add Settings, then read it with: Properties.Settings.Default.Property
I always create a config class per application which wraps access to the app/web.config file and exposes the config entries as properties. E.g. something like this:
public static class MyConfig
{
/// documentation of config entry
public static string Browser
{
get { return Read("Browser", "some default value"); }
}
/// documentation of config entry
public static int Port
{
get { return int.Parse(Read("Browser", "80")); }
}
public static string Read(string entry, string defaultValue)
{
var entry = ConfigurationManager.AppSettings[entry];
return string.IsNullOrEmpty(entry) ? defaultValue : entry;
}
}
This has several advantages:
- I can define default values (e.g. if a config entry is optional/missing)
- I can expose numerical/boolean config entries in the correct type (int, bool)
- I can document all config entries in a central place
If you want have strong type reference, you can inherit from ConfigurationSection
/ ConfigurationElementCollection
and ConfigurationElement
.
You can specify default value to ConfigurationElement
with [ConfigurationProperty("key", IsRequired = true, DefaultValue = "*^%)(@")]
and validator like [StringValidator(MinLength = 3)]
etc.
Write a helper function to get the parameter. You need to check if the key is actually present in the first place for good coding practices.
The only "more efficient" manner to pull from a config file is to consume the entire section and then iterate through what you desire. As you end up with looping code, it is unlikely to be more efficient than the method you have now.
One pattern to use to simplify is to create an "App settings" Singleton and load at Application Load. You essentially create a generic hashtable (dictionary, etc.) of string, string, so you can accomplish lookups more easily. But there is still the overhead of ripping through the app settings section.
You need to be a bit more creative with the class name perhaps but you could do something along the lines of:
class ConfigManager
{
public static string GetSetting(string name)
{
return ConfigurationManager.AppSettings[name].ToString();
}
}
精彩评论