If I want to create a 3-Layer ASP.Net application (Presentation Layer, Business Layer, Data Access Layer), where is the best place to create the Connection objects?
So far I used a helper class in my Presentation Layer to create an IDbCommand from the ConnectionString in the web.config on each page and passed it on to the DAL classes/methods.
Now I am not so sure, if this part shouldn’t also be included in the DAL somehow, because it obviously is part of the Data Access. The DAL is in a separately compilated project, so I dont have access to the web.config and cannot access the connection string (right?).
What is the best practice here?
Short answer:
The Connection objects, which represent a dependency to the database, should be created in (and known only to) the Data Access Layer.
Long answer:
When you say “3-Tier” do you really mean “tier” or do you mean “layer”? The former suggests a hard boundary, such as a service layer, between each tier. The latter is simply a logical separation within a single application context. Also, define in what way the DAL is “a separately compiled project”? It can access the config file for whatever application context is running the code. If it is its own tier, it would have some kind of service or something which has a config. If it’s just a layer, it can access the application’s main config.
Ideally, anything that’s tied to and/or dependent on the database should exist only in the DAL. The rest of the application domain shouldn’t have to worry about the database.