Conditional compilation symbols as user preferences

122 views Asked by At

In my team's deployment environment, there are three endpoints for a certain REST data api: alpha, beta and production.

I have setup conditional compilation so that in RELEASE configuration, the project will definitely connect to the production api. In DEBUG configuration, however, the project may connect to any of the three endpoints according to the developer's preference.

Currently, the developer sets this preference by commenting out the endpoints not needed.

However, different developers will wish to connect to different endpoints during development, so this system doesn't work well with git and a team. How can I change the setup so that the api endpoint is selected according to a developer preference (and not committed to git) in DEBUG configuration only?

    const string api_prod = "https://api.mydomain.com/api/";
    const string api_beta = "https://betaapi.mydomain.com/api/";
    const string api_alpha = "https://alphaapi.mydomain.com/api/";

#if DEBUG
    //static readonly string BaseUrl = api_prod;
    //static readonly string BaseUrl = api_beta;
    static readonly string BaseUrl = api_alpha;
#else
    static readonly string BaseUrl = api_prod;
#endif
2

There are 2 answers

2
Łukasz Zwierko On

You should use external configuration files and make their inclusion dependent on compilation configuration. Take a look at slow cheetah https://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5

Keeping hardcoded uris is never a good idea.

1
Peter Duniho On

I agree with the other answer in that it would be better to use .config or similar mechanism to store the URLs. That said, as you point out that doesn't really address the question you're asking. It's something you should fix, but fixing it wouldn't change the basic question.

You're not really clear on what "developer preference" is here. However, there are at least a few different approaches you could use:

  1. Environment variable. Developer sets it once, and then the program checks the value to determine which URL to use.
  2. Application settings. Developer sets it once (for convenience, preferably via some part of the program UI), winds up stored in the Designer-controlled .config file under AppData.
  3. Some custom configuration file. This can be anywhere you like (e.g. %USERPROFILE%\Documents\MyProgam\custom.settings.config), as long as the program knows where to look.

This is not an exhaustive list. If you have something more specific in mind, you need to explain that in your question.