Reputation: 7750
I have connection string declared in [app|web].config and assemblies (for example DAL and Reporting) that relies on this connection string. What is the best thing for you to configure such assemblies:
Use hard-coded connection string name in [app|web].config connectionStrings section to let assembly retrieve it's configuration by hard-coded name. So there possibly would be two identical connection strings: "reportingServer" and "dataSource".
Use the only connection string in connectionStrings section with any name you like and configure DAL|Reporting assemblies to use this name via custom configuration sections. Now assemblies retrieve connection string name to use and then connection string data.
Configure connection string name via AppSettings hard-coded key. For example you should always have "reportingServerConnectionStringName" & "dataSourceConnectionStringName" keys in this case.
Something better what I missed...
Thank you in advance!
Upvotes: 2
Views: 1185
Reputation: 1166
You may look at this question for good practices:
Connection String Best Practices
Also, Enterprise Connection String Management in ASP.NET - Best Practice
http://weblogs.asp.net/jgalloway/archive/2004/05/11/129941.aspx
Hope it helps!
//Edit: Added the following
The primary advantage of having Config files is exactly the scenario that you are dealing with (multiple projects and centralised configuration).
Either ways, you would need to hard-code some information for your assemblies to be able to extract the required information from App/Web config.
For my practical purposes, I usually keep two connection strings ("reportingServer" and "datasourceServer") and if need be to make them dynamic, have two AppSettings keys to load the required connection strings dynamically.
A simple but effective way to enable dynamic loading of connection strings.
<appSettings>
<add key="reportingServer" value="reportingServerDev"/>
<add key="dataSourceServer" value="dataSourceServerDev"/>
</appSettings>
<connectionStrings>
<add name="reportingStaging" connectionString="proper connection string"/>
<add name="reportingDev" connectionString="proper connection string"/>
<add name="dataSourceStaging" connectionString="proper connection string"/>
<add name="dataSourceDev" connectionString="proper connection string"/>
</connectionStrings>
Hope this time i answered in the correct context.
Happy Configuring!
Upvotes: 1
Reputation: 10263
There is generally two ways I've handled connection strings. The first way is the way you have listed in your first bullet point.
The advantages are:
The disadvantages are:
The other technique I use is the same as the provider model used by ASP.NET Membership,etc
<configuration>
<connectionStrings>
<add name="SqlServices" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />
</connectionStrings>
<system.web>
<membership defaultProvider="SqlProvider">
<providers>
<add
name="SqlProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="SqlServices"
moreSettings="... other settings ..." />
</providers>
</membership>
</system.web>
</configuration>
The advantages are:
The disadvantages are:
Upvotes: 1