Where to put the connection string in an n-tier application?
I created a DAL using the framwerok entity. I also created a business service tier and a presentation tier (web application). My general gist is telling me that the connection string should only be in the DAL, but the presentation layer requires a connection string.
So what's the best fit for this situation? Is there a way to only have a connection string in the DAL?
source to share
The data abstraction layer should be the only place your data accesses. The presentation tier must use the business service tier and the business service tier must use the DAL to access data. This way, you don't need direct access to the data source from the presentation layer, and the only place for the connection string will be in the DAL.
source to share
I have a DAL that takes a connection string as a parameter when it is created. The layer using the DAL is responsible for getting it. Thus, the DAL does not change if it is in Windows or a web application. My DAL is also only created once per application using the singleton pattern, so the overhead of getting the connection string is only paid once.
public sealed class cApp
{
static readonly cDB _cDB = new cDB(
ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString);
public static cDB DB
{
get
{
return _cDB;
}
}
}
Then in code I can use it like:
GridView1.DataSource = cApp.DB.GetStages(id);
GridView1.DataBind();
source to share