Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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?:
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 9-Mar-12 7:06am
Edited 9-Mar-12 7:56am
v2
Comments
André Kraak at 9-Mar-12 12:56pm
   
Edited question:
Added pre tags

1 solution

Rate this: bad
good
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.
 
#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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 280
1 Sergey Alexandrovich Kryukov 279
2 CPallini 205
3 Maciej Los 197
4 Afzaal Ahmad Zeeshan 160
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 9 Mar 2012
Copyright © CodeProject, 1999-2014
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