Click here to Skip to main content
12,999,845 members (48,610 online)
Rate this:
Please Sign up or sign in to vote.

I am new in C# (VS2010, 4.0 .net framework), want to be able to have a class within my project that have the ability to choose between a QA versus a PROD web service HTTP/ASMX, I already created two web service references within my solution (one per env.), now I am doing the following in my code but maybe there is a better way, is this a good way to do this logic?:
EDITranslatorQA.EDITranslator proxyQA = null;
EDITranslatorQA.EDIOptionsDTO optionsQA = null;       
EDITranslatorPROD.EDITranslator proxyPROD = null; 
EDITranslatorPROD.EDIOptionsDTO optionsPROD = null; 
// Below checking ENV variable in the Cache class (defined in app.config):
if (Cache.CacheInstance.ENV == "QA")
  proxyQA = new EDITranslatorQA.EDITranslator();
  optionsQA = new EDITranslatorQA.EDIOptionsDTO();
                optionsQA.doTranslate = true;
                optionsQA.getAudit = true;
 else if (Cache.CacheInstance.ENV == "PROD")
 proxyPROD = new EDITranslatorPROD.EDITranslator();
 optionsPROD = new EDITranslatorPROD.EDIOptionsDTO();
                optionsPROD.doTranslate = true;
 var failCount = 0;
 var succeeded = false;
                while ((failCount < 2) && (!succeeded))
                        if (Cache.CacheInstance.ENV == "PROD")
                         EDITranslatorPROD.EDIResponseDTO output = proxyPROD.TransVal(System.Convert.ToString(input), optionsPROD);
                         succeeded = true;
                         return (System.Convert.ToString(output.native.ToString()));
                        else if (Cache.CacheInstance.ENV == "QA")
                         EDITranslatorQA.EDIResponseDTO output = proxyQA.TransVal(System.Convert.ToString(input), optionsQA);
                         succeeded = true;
                         return (System.Convert.ToString(output.native.ToString()));
                    catch (WebException wex)
                    {.....etc. }
Posted 9-Mar-12 6:06am
Updated 9-Mar-12 6:56am
André Kraak 9-Mar-12 12:56pm
Edited question:
Added pre tags

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I've seen it done this way. It's not a bad way to solve it. It's easy to change, but it means doubling the coding in places, which can get hairy.

I've also seen it done using compiler directives.

//use the QA service
//use the PROD service

You could also use Conditional Attribute. I haven't used this, but I've read that this is the preferred method by those who know about it.

Using this, you'd actually write one piece of code for QA and another for PROD, then on the QA piece you'd add this attribute. Here is some reading material from MS.

Let us know what you decide and how you use it. I would be interested. I just have never had a reason to use it/learn it myself. Just know about it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 9 Mar 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100