Reputation: 85789
I have a configuration, for example, hostname of external server to store files (like images and PDFs), stored in settings files. These settings are stored in a class library called CommonSettings. The value of this path may change depending the environment the application is deployed. This is:
"localhost"
"qa-external-files"
"prod-external-files"
Then I have to access in code to such settings but I need to decouple the value of the settings per environment from the code. For example, I would want to achieve something like this (tests in Console Application using C#):
using CommonSettings;
String externalServerHostName = CommonSettings.Properties.Setting.Default.ExternalServerHostName;
Console.Write(externalServerHostName);
//prints localhost when I execute this from my machine
//prints qa-external-files when I execute this from the qa environment
In Java I can handle this scenario by creating folder structure and have a folder per environment, then each folder containing the related settings inside a file (properties file) and using an automated build tool (maven) to generate the library containing only the properties files according to the environment I want/need.
Can I have a similar approach using .net? Or should I use a build tool that handles this for me?
Upvotes: 1
Views: 607
Reputation: 4464
Would it be possible to store your configuration settings in an app.config file instead of a class library? If so, you can apply transformations to the app.config depending on which environment you are in. You are right that the transformation should probably be handled during build and not during runtime.
If you have an ASP.Net application, web.config transforms are much easier:
http://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx
If you are using WPF or something not ASP, it is a bit more work to do transforms on app.config. Luckily, there is an VSIX file for Visual Studio that will help with this:
Upvotes: 1