Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to store Connection String

I am creating a Class Library which contains all the custom classes I regularly use in the applications I develop. This library is compiled and added as a Reference in each of my applications, enabling me to call on the custom classes contained within the library.

The custom classes in my library include multiple layers of inheritance, but all ultimately have their origins in a base class I call 'Alpha'. In that class I also hold my connection string so that it is inherited by all other classes in the library. Many of the classes make significant use of ADO to communicate with a SQL database, and so having the connection string available to all the classes is essential.

The issue I have, is that each Application I want to use my Class Library with, has its own database and so needs a connection strings unique to each. Hence the connection string cannot be contained in my class library, because that would mean all the applications I use my class library with will be looking for the same SQL database. However, if I remove the connection string from the 'Alpha' class, then the Class Library will not compile because all its methods which utilise ADO.NET are looking for that Connection String.

Its a bit like a chicken and egg scenario.

i.e. I need the connection string in the Alpha class in order to compile the class library. i.e. the connection string cannot be in the class library because I need it to be unique to each application

The only solution I can think of for this, is to give every method in my class library that uses ADO, a parameter to hold the connection string which can then be passed in from the applications code. But this does not seem like a very concise way of overcoming this problem.

Ideally what I want is a single location in each application to store the connection string. I then need some way of referring to this location in my class library even though at the time of writing my class library that location will not be available (because it is inside the individual applications).

like image 749
PJW Avatar asked Oct 22 '22 08:10

PJW


1 Answers

Usually the connection string is constant for the lifetime of your application, so you could extract it from the app.config of the actual client application and make a requirement to use your library that the client application pass this string to your library.

So I have resolved creating a global helper class in my library where I store the connection string passed from the client application, then my internal library methods use this global property to read the connection.

public static class DatabaseHelper
{
     public static DbConnectionString {get;set;}
}

then the client application during its startup calls

DatabaseHelper.DbConnectionString = ConfigurationManager.ConnectionStrings["MyDbCon"].ConnectionString;

while in my library

public DataTable CustomerTable()
{
    using(SqlConnection cn = new SqlConnection(DataBaseHelper.DbConnectionString))
    ......
}
like image 69
Steve Avatar answered Oct 28 '22 20:10

Steve