Home > DeveloperSection > Forums > Using values from AppConfig file in C#
Samuel Fernandes
Samuel Fernandes

Total Post:154

Points:1082
Posted on    January-28-2014 11:58 PM

 C# C# 
Ratings:


 1 Reply(s)
 802  View(s)
Rate this:

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:

ConfigurationManager.AppSettings[""].ToString()



Pravesh Singh

Total Post:411

Points:2881
Posted on    January-29-2014 12:53 AM

Hi Samuel,

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"]


Don't want to miss updates? Please click the below button!

Follow MindStick