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?

+2


source to share


5 answers


Typically you add the connection string to the config file, and the config file used is the one used for the executing assembly. For a website, this will be web.config.



Why is your presentation tier needed to access the connection string?

+4


source


Your connection string is usually in and accessible from web.config. You probably should never hardcode a connection string unless you have any other choice (and I can't think of any situations where you haven't).



+2


source


If the presentation layer requires a connection string, then you have a flaw in your design.

+2


source


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.

+2


source


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();

      

0


source







All Articles