Reputation: 1007
In our web project, we have a custom Database Connection class that creates the SqlConnection object and get the DB connection string for us from the web.config.
While this is nice, there's one problem. All projects inside our web project depend on that web.config being there so that this class can work.
There are needs to use this db connection from our DL in other solutions or projects that should not require a web.config such as a Console project that is trying to use lets say some of the DL methods from our web project to do some data manipulation.
Consequently, when I add some of our BL project methods to my console solution, it's looking to create the DB connection but I have no web.config in my Console application of course so it bombs out.
Is there a better way to manage the creation of the SqlConnection than: a) putting that class in a web project b) making that connection reliant on web.config keys
so that non web based projects can use the BL without that BL referencing and being reliant on a connection that is reliant on ultimately keys in a web.config?
Upvotes: 1
Views: 576
Reputation: 14875
In your console app those DB connection settings need to go into App.Config, however you clearly don't want to have them stored in both App.Config and Web.Config, so you can use the following technique.
1) Move the DN configuration settings to a single DBSettings.config file
2) Reference that config from Web.Config and App.Config as required
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="DBSettings.config">
...
</configuration>
This technique is detailed here.
Upvotes: 1