views:

293

answers:

5

I've created a DAL using the entity framwerok. I've also created a business service layer and a presentation layer(web app). My common sence is telling me the connection string should by only in the DAL but the presentation layer needs the connection string too.

So what's the best-practice for this situation ? Is there any way to have the connection string only in the DAL?

+2  A: 

Your connection string usually resides in and is accessed from the web.config. You probably shouldn't ever hardcode a connection string unless you have no other choice (and I can't think of any situations where you wouldn't).

Scott Lance
+3  A: 

You usually put the connection string in a config file, and the config file that is used is the one for the executing assembly. For a website it's going to be web.config.

Why does your presentation layer need access to a connection string?

Charlie
+2  A: 

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

Austin Salonen
Unless it is a connection string editor <g>
RichardOD
@RichardOD -- I about put that caveat in there and now I have to upvote the obvious. lol
Austin Salonen
+2  A: 

The data abstraction layer should be the only place your data is accessed. Your presentation layer should use the business services layer and the business services layer should use the DAL for data access. Thus you won'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.

Bryan S.
I understand the theory but it just didnt work like this in my case. In my DAL i'm using the Entity framework.This layer is referenced to the business layer.IN this layer i have a L2E query returning a IEnumerable<>. This query is called from the presentation layer and assigned as a datasource for a gridview. The strange think is i need the connection string in the presentation layer else it won't work. I also needed to reference the DAL in the presentation or else it wouldn't work. I'm doing somthing wrong but i don't know what ...
A: 

I have a DAL that takes a connection string as a parameter when it is instantiated. The layer using the DAL is responsible for getting it. This way the DAL doesn't change if it is in a Windows or web app. My DAL is also only instantiated once per application using the singleton pattern, so the overhead of retrieving the connection string is paid only 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();
JBrooks