views:

112

answers:

3

Hello,

I have a .net solution having a reference hierarchy like this MyWinApp->ServerCore->DataAccess where the last two are class libraries and myWinApp is a windows app.

Now, each time I want to run this project on different servers I need to rebuild the project since I couldn't manage to separate the configuration file(app.config) of DataAccess project that has connection string related configurations.

How can I separate database configurations from the application code? I tried build action options but it doesn't work :S What might be the most feasible solution?

Thanks in advance

+1  A: 

For my DAL projects I typically have a DataLayer.dll.config file but I actually add the ConnectionString entries to my Program.exe.config file. I don't know that you need anything special for this. Possibly just reference a static readonly string or something from the DataLayer.dll in the Program.exe?

Or, do you really want to have separate configuration files? If so then you can use the ConfigurationManager class to open a loose configuration file with the OpenMappedMachineConfiguration method. See MSDN ref here: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedmachineconfiguration.aspx

Wil P
Thank for your help! I combine your suggestion with Min's;)
Aleyna
+2  A: 

The configuration should most likely go with the MyWinApp project. The configuration file goes with what is executing. So if you make an installer for your application, it'll have a configuration file called MyWinApp.exe.config that was created from your App.Config.

Basically the app.config with your Datalayer.dll doesn't really do much.

What you might want to do is look at how the configSource property works for configuration files in .net here: http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx and see if this is what you're looking for. This lets you set a configSource for your connection strings that you can change per machine.

All I do is setup a simple xcopy before a deploy and I have the correct configuration settings before deploying an asp.net app. If you have to package an installer, copying the correct files before building the installer should do the trick as well.

Min
Nice, I didn't think of just using the config source. That's a good idea.
Wil P
+1 yup, the "configSource=......" can be a real life saver!
marc_s
That's great! Thank you really much ;)
Aleyna
A: 

I think you can use applicationSettings to include configuration for specific referenced assemblies:

For example:

<configuration>
 <applicationSettings>
     <ProjectName.Properties.Settings>
        <setting name="ConnectionString" serializeAs="String">
           <value>YourConnectionStringHere</value>
        </setting>
     </ProjectName.Properties.Settings>
  </applicationSettings>
</configuration>

Where "ProjectName" is the name of the reference you need to configure. Each project can have it's own app.config with the above applicationSettings entry and a value specific to the project itself.

PabloC
You can do this same thing by just adding the connection string to the ConnectionStrings config section withing the programs exe file. No need to use a Settings file to do this.
Wil P
You're right, that's a lot better specifically for connection strings, but wouldn't it still be applicable for other configurations settings?
PabloC
Thanks Pablo but I could get your point in so doing!
Aleyna