Hi Otis -- thanks for the quick reply.
We tried putting the code that sets the connection in the static constructor, but that led to some problems with the components stepping on eachothers toes, so to speak. DBUtils gets the connection string of the most recently referenced class.
// Problem if we set connection strings in static constructors
AppInfo app = new AppInfo(); // DBUtils now has connection string #1 (req'd by AppInfo)
MyBll.SomeStaticMethod(); // DBUtils now has connection string #2 (req'd by MyBll)
app.Refresh(); // EXCEPTION AppInfo class tries to use conneciton string #2 to access DB.
So we added the conneciton setting code to the constructors so that whenever an instance of an object is created, it can set the connection string for its purposes. And because the AppInfo class is most often called by other classes, we set it up to clean up after itself by resetting the connection string after it uses it to what it was originally.
string temp = DbUtils.ActualConnectionString;
DbUtils.ActualConnectionString = _AppInfoConnectionString;
// Do DAL access stuff...
DbUtils.ActualConnectionString = temp;
This allows something like this:
AppInfo app = new AppInfo(); // DBUtils now has connection string #1 (req'd by AppInfo)
MyBll bll = new MyBll(); // DBUtils now has connection string #2 (req'd by MyBll)
app.Refresh(); // AppInfo sets string to #1 temporarily, then resets it to original string
MyBll.SomeMethod(); // Uses connection string #2 because AppInfo has reset it.
We get the config file error even when DAL objects are referenced, even if we're not going to the database. So we've been putting the above set/reset code around all references to DAL objects to get around it.
// Throws an exception if connection string is empty
foreach ( MyTableEntity e in MyTableEntityCollection ) {
// Do something...
}
So we need to do this:
string temp = DbUtils.ActualConnectionString;
DbUtils.ActualConnectionString = _AppInfoConnectionString;
foreach ( MyTableEntity e in MyTableEntityCollection ) {
// Do something...
}
DbUtils.ActualConnectionString = temp;
It looks like what we might need to do is disable the code in DBUtils.CreateConnection() that checks to see if ActualConnectionString is empty (because we'll never be going to a config file for this info). I don't know what affect this would have on the rest of the code... Thoughts?