Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

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?:
C#
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))
                {
                    try
                    {
                        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
Updated 9-Mar-12 6:56am
v2
Comments
André Kraak 9-Mar-12 12:56pm    
Edited question:
Added pre tags

1 solution

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.

#IF DEBUG
//use the QA service
#ELSE
//use the PROD service
#ENDIF

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.

https://msmvps.com/blogs/peterritchie/archive/2011/11/24/if-you-re-using-if-debug-you-re-doing-it-wrong.aspx

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.

http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx
http://msdn.microsoft.com/en-us/library/aa664622(v=vs.71).aspx

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900