1

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I dont want to go into details of the application structure, which is badly designed anyway. My main question is: Should I access the ConnectionString from the DAL? Wouldnt that create a dependency to the Website that uses the DAL?
@atticae: Without too many details, yes and no. The dependency in the form of the config file is simply that "in order to use this library, you must have these configuration options set." This is pretty standard for a library that can be used in multiple environments and application contexts, and is expected. The DAL can have hard-coded defaults perhaps, to be used in the absence of config values. But it's not unreasonable for a library which handles infrastructure/environment dependencies to expect to be configured with regards to those dependencies.
0

I think it ought to be in a service layer, the one that fulfills the use cases. The object that knows about units of work ought to be the one to create the connection, set the transactional level, fulfill the use case, and clean up whatever is necessary.

It shouldn't be in the presentation layer, because if you change presentation technology you have to redo the connection creation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.