问题
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).
回答1:
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))
......
}
回答2:
Thanks to the comments made by Steve (accepted as my answer) I was able to resolve the issue. But I thought I would include my adaption of his code in case it was helpful to anyone else:
In my Class Library
public static class Alpha
{
public static string ConnectionString { get; set; }
public static SqlConnection DatabaseConnection()
{
SqlConnection linkToDB = new SqlConnection(ConnectionString);
return linkToDB;
}
The linkToDB is then used throughout the Classes in my library.
And then in the WinForms Application I set the connection string on the Load event of my start up login in form as follows:
private void LoginInForm_Load(object sender, EventArgs e)
{
Alpha.ConnectionString = @"...connection string goes here...";
}
来源:https://stackoverflow.com/questions/17008616/where-to-store-connection-string