I am building up a business logic layer (using Adapter) that I want to use in a system that has both windows and web based elements.
In my business logic layer I use the command
DataAccessAdapter LLBLAdapter = new DataAccessAdapter(this.ConnectionString,false,
CatalogNameUsage.ForceName,new ConfigSetting().GetConfigSetting("DBCatalog"));
to create a connection to the database
which works fine for the Windows application because I have an XML file called Configuration.cfg that I hold in the root folder of the application. This file holds the information (DBServer, DBCatalog, DBUid and DBPwd) that I need to connect to the database. Therefore if the database moves I just edit this file in notepad. Obviously the DBUid and DBPwd fields are encrypted in this file.
ConfigSetting().GetConfigSetting("DBCatalog") is just a small class.method that I have built that opens the XML file and returns the value of the key passed, in this case the value of the key "DBCatalog".
public class ConfigSetting
{
public ConfigSetting()
{
// Constructor code
}
public string GetConfigSetting(string strKey)
{
DataSet myDataSet = new DataSet();
string myReturnValue = "";
// Attempt to locate the configuration.cfg file
try
{
myDataSet.ReadXml(System.Windows.Forms.Application.StartupPath + "\\Configuration.cfg");
foreach (DataRow row in myDataSet.Tables[0].Rows)
{
if (row["Key"].ToString() == strKey)
{
myReturnValue = row["Value"].ToString();
}
}
return myReturnValue;
}
catch (Exception ex)
{
throw new Exception("There is a problem reading the file " +
System.Windows.Forms.Application.StartupPath + "\\Configuration.cfg. " + ex.Message); }
}
}
this.ConnectionString uses the ConfigSetting Class to retrieve and then return the full connection string to the SQL Server database.
My question is that although this approach works perfectly for a windows app, how can I make it work for a web app too - can I simply put the Configuration.cfg file in the root of the web site and use it as it is - I guess the answer to this is NO because the command
System.Windows.Forms.Application.StartupPath
cannot be used to locate the root of a web application.
So how might I inform my ConfigSetting class that the request is comming from a web application and therefore it should return the values from the Web.Config file instead? Is there a way to do this so that I dont have to change the comand
DataAccessAdapter LLBLAdapter = new DataAccessAdapter(this.ConnectionString,false,
CatalogNameUsage.ForceName,new ConfigSetting().GetConfigSetting("DBCatalog"));
in my business logic layer.
OR, am I completely missing a trick - how do other people achieve the same goal?
Thanks in advance