views:

75

answers:

2

my ASP.NET application reads an xml file to determine which environment it's currently in (e.g. local, development, production).

It checks this file every single time it opens a connection to the database, in order to know which connection string to grab from the Application Settings.

I'm entering a phase of development where efficiency is becoming a concern. I don't think it's a good idea to have to read a file on a physical disk ever single time I wish to access the database (very often).

I was considering storing the connection string in Application["ConnectionString"]. So the code would be

public static string GetConnectionString
        {
            if (Application["ConnectionString"] == null)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(HttpContext.Current.Request.PhysicalApplicationPath + "bin/ServerEnvironment.xml");
                XmlElement xe = (XmlElement) xnl[0];

                switch (xe.InnerText.ToString().ToLower())
                {
                    case "local":
                        connString = Settings.Default.ConnectionStringLocal;
                        break;

                    case "development":
                        connString = Settings.Default.ConnectionStringDevelopment;
                        break;

                    case "production":
                        connString = Settings.Default.ConnectionStringProduction;
                        break;

                    default:
                        throw new Exception("no connection string defined");
                }
                Application["ConnectionString"] = connString; 
            }
            return Application["ConnectionString"].ToString();
        }

I didn't design the application so I figure there must have been a reason for reading the xml file every time (to change settings while the application runs?) I have very little concept of the inner workings here. What are the pros and cons? Do you think I'd see a small performance gain by implementing the function above?

THANKS

+7  A: 

Wow. Throw that in the bin.

All .config files (web or app.config) have a section dedicated to connection strings and these can be read using the ConfigurationManager.ConnectionStrings property.

For local and test environments, I simply set up three connection strings as follows

<connectionStrings>
    <add name="default.local" connectionString="etc ..>
    <add name="default.test" connectionString="etc ..>
    <add name="default" connectionString="etc ..>
</connectionStrings>

Determine which connection stirng to fetch based on another setting in the config file. A small utility function utilises this information to pick the correct connection string.

In this way, the config file stays the same across the different deployments apart from the environment setting.

EDIT: forgot to mention these values are cached in memory and the app restarts when they are changed (may be a problem, may be useful) Performance is absolutely fine going directly to the .config file through the ConfigurationManager class.

James Westgate
You should be able to determine what environment your application should use from the URL - say you have a development site. http://dev.mydomain.com, a test site http://test.mydomain.com and production http://www.mydomain.com - you can retrieve the host name, get the first part of the URL, and choose your connection string appropriately. A piece of cake.
Ken Ray
You can also just use a single connectionString name and change the value with the deployment tool.
Jeff Siver
@Jeff - agreed - this example is from an operational environment where only notepad is available.
James Westgate
ken ray -- very cool idea... i'll look into how i might be able to do that in a performant way.Jeff Silver -- I didn't know I could do this, i'll look in to that as well!James -- thanks for addressing the performance aspect. I'm feel kind of weird for not thinking about using app.config... I've been developing for asp for a couple years and have never used app.config for connection strings. THANKS GUYS!!!
HaterTot
@Ken - nice idea - I think Ill try that. And perhaps use the machine name or domain or something in a rich client / app.config scenario
James Westgate
+3  A: 

I totally agree with James Westgate. If you use the web.config, you don't have to take care about this issue.

In case that you can't use the web.config for any reason I would say that your approach is correct. Storing the value on the Application storage you're avoiding to read the connections string over and over again from disk.

If you need to change the connection string, you would have to restart the application. A connection string is not supposed to change everyday so this shouldn't be a problem.

BTW: I don't like very much the logic asking in which environment you are. That shouldn't be necessary at all...

Claudio Redi
This is good information... thanks for addressing my thoughts in a way that improves my overall understanding
HaterTot