‎2011 Feb 10 3:38 PM
In a program I'm writing we are consuming webservices from another server. There are two webservices, one on a DEV server, and another on a PRD server.
What I'm wondering is if there is a way I can use the same variable name, but change it's TYPE based on if the program is being used in the DEV or PRD system.
Pseudo-example
if sy-sysid = 'DEV'
data: my_webservice type ref to Z_DEV_WS_CLASS
else
data: my_webservice type ref to Z_PRD_WS_CLASS
endif
I do know I could use different variable names, but with all the structures and such that go with them, I was wondering if this could be done or something else to make it easier to read instead of huge if thens where i'm only changing variable names.
Any suggestions would be appreciated.
Thanks,
Curtis
‎2011 Feb 10 4:03 PM
Hi Curtis,
I am surprised that you have two different webservices on your DEV and PRD server? Do you mean to say that there are two different WSDL (one for dev and one for production with just different input/ output?)if the purpose of webservice is same, it does not seem normal that webservice would have the different name and different structures (associated with it).
I am not sure if you can use the variable name... may be you can try out the field symbols... I would try to see if I can get a single webservice for both DEV and PRD
How are you consuming webservice? are you converting the webservices to proxy as per the link below? meaning you are creating two proxies for the two WSDLS?
http://help.sap.com/saphelp_nw04/helpdata/en/bf/d005244e9d1d4d92b2fe7935556b4c/frameset.htm
‎2011 Feb 10 4:03 PM
Hi Curtis,
I am surprised that you have two different webservices on your DEV and PRD server? Do you mean to say that there are two different WSDL (one for dev and one for production with just different input/ output?)if the purpose of webservice is same, it does not seem normal that webservice would have the different name and different structures (associated with it).
I am not sure if you can use the variable name... may be you can try out the field symbols... I would try to see if I can get a single webservice for both DEV and PRD
How are you consuming webservice? are you converting the webservices to proxy as per the link below? meaning you are creating two proxies for the two WSDLS?
http://help.sap.com/saphelp_nw04/helpdata/en/bf/d005244e9d1d4d92b2fe7935556b4c/frameset.htm
‎2011 Feb 10 4:16 PM
This seems to be a easy question but it's not
You can try this:
DATA o_data TYPE REF TO object.
DATA: v_object TYPE char10.
IF sy-sysid = 'DEV'.
v_object = 'CLASS1'.
ELSE.
v_object = 'CLASS2'.
ENDIF.
CREATE OBJECT o_data TYPE (v_object).
o_data will be a generic object, and you can create if making reference to your devclass or prdclass.
Hope it helped!
‎2011 Feb 10 4:34 PM
What Mauricio has suggested is of course very correct but would require a generic access of any method/attribute in each class. This then starts to be troublesome as components must be addressed indirectly.
The ideal way would be having an interface which both classes would implement, so that you could access different objects (of those two classes) by means of this interface w/o knowledge of which one in fact you are accessing. This would ahere to program to an interface. not an implementation design rule. Unfortunately not sure if you can do that for Web Services as the classes are generated by ESR automatically.
Regards
Marcin
‎2011 Feb 10 4:42 PM
Totally agree that method acesses would be rather difficult by what I suggested.
You would have to call methods like this: CALL METHOD o_object->('METHOD_NAME').
‎2011 Feb 11 6:09 PM
I've done something similar recently in Visual Studio.NET using VB (the concept is the same though). My two webservices were the same but one was called inside my intranet and the other accross the Internet.
I made an interface (I'll call it ZIF) which contained methods and attributes for the needed functionality of the web services.
Implementing ZIF, I had ZLOCAL and ZGLOBAL. In the implementations of the ZIF methods, ZLOCAL called the needed functionality of the generated class for the local web service and ZGLOBAL did the same for the Internet service.
Based on an if statement, I either created an instance of ZLOCAL or ZGLOBAL but I used it purely as a ZIF instance, letting me not care which web service I was using. This approach will let you avoid using dynamic programming statements such as
CALL METHOD o_object->('METHOD_NAME').which can be problematic sometimes.
Edited by: Erik Peterson on Feb 11, 2011 1:09 PM