How to make the connection string visible throughout the solution (across layers)?
I have developed an Asp.net application using three tire architecture and i am reading the connetions string from web.conf file in the UI layer,
My problem is i have to pass the connection string as a parameter in all the function开发者_如何学Cs that are in the Bo layer.
Is there any work around for making the connection string visible throughout the Bo layer so that i dont need to send the connetions string as parameter in all my method calls.
Include the System.Configuration reference to all projects in which you want to access the connection string and use the following code to access your connection string.
ConfigurationManager.ConnectionString["MyConnection"].ConnectionString
It is advisable to use a helper class to access your connection string.
internal sealed class Utility
{
public static string MyConnectionString{
get{
return ConfigurationManager.ConnectionString["MyConnection"].ConnectionString;
}
}
}
This will help you to move your connection string from web.config to any other location in future without breaking your code.
精彩评论