Help needed with DataAcess Application Block

Topics: Data Access Application Block
Oct 22, 2007 at 7:12 PM
First of all, sorry for my english, I'm from Spain. Here's the issue:

I'm making an application divided in 3 tiers (Presentation layer, business logic layer and dataaccess layer).
Presentation layer is an (c#) web project, and another layers are class library projects.

I want to use DataAccess Application block in dataaccess layer, i'm storing connection string in app.config in my data class library project. But when I use dataaccess application block:

Database db = DatabaseFactory.CreateDatabase("AcuamaCS");

my connection string is not found. BUT IF I STORE CONNECTION STRING IN WEB.CONFIG PRESENTANTION LAYER ALL WORKS OK!, BUT I don't want to store connection string in presentation layer, connection string must be in data access layer....

any help will be appreciated
Oct 22, 2007 at 7:57 PM

I'm afraid that is the way .NET configuration works; configuration belongs to the application, not to a layer/assembly.
If your data access layer was encapsulated in a service the the service's configuration would hold the connection strings.

Oct 23, 2007 at 7:39 AM
My data access layer is not in a service. Store connection string in web.config in presentation layer is the correct way?
Oct 24, 2007 at 7:15 AM
Edited Oct 24, 2007 at 7:16 AM
In a word yes.
At runtime, the thing that actually runs is the web application, which then includes the complied assemblies.
It is logical that its configuration will be used by all the assemblies, because (in theory, at least) you could use the same Data Access Layer in another application, with different configuration.
Oct 24, 2007 at 2:24 PM
I'm not sure if this is what you are looking for but, I see two possible additional approaches you could use:

  1. Write your own version of DatabaseFactory.cs, if you look at the actual code it is very simple (I like simple!). The difference you would need is to use your own instantiation of IConfigurationSource (most likely it would be FileConfigurationSource pointing to your custom configuration). This would then allow you to control which file you wanted to use.
  2. If you know what databases you are going to support then you could use the Database object directly and pass in the connection string (from what ever source you want). Obviously this negates the benefit of having the configuration done via the factory.

Hopefully this offers some alternatives,

Note I've not personally tried option 1, but from a code review perspective it makes sense :-)