c# - Reusable Function Code For Same-Named Objects From Different Namespaces? -


i trying write 'wrapper' code 2 web services have different namespaces, contain classes same name , definition in each. write function code use in wrapper classes each web service without copying code each of them. instance, return object name each:

using websvc1; wrapperclassws1 {  // root class inherits system.web.services.protocols.soaphttpclientprotocol   websvc1root _root = new websvc1root;    public mynonwsobj getobjectbyname(string whichname)   {     // uses getws1objectbyname retrieve 'real' object     //   turns dumbed down object use     //   calling code knows nothing web services     // identical code wrapperclassws2     mynonwsobj myobj = new mynonwsobj();     commonobj returnedobj = this.getws1objectbyname(string whichname);     myobj.name = returnedobj.name;     myobj.revision = returnedobj.revision;     myobj.description = returnedobj.description;     return myobj;   }    private commonobj getwsobjectbyname(string whichname)   {     // identical code wrapperclassws2     // function object native websvc1root ,     //   retrieved object have identical name/definition     //   1 websvc2root     return _root.getobject(whichname);   } }  using websvc2; wrapperclassws2 {  // root class inherits system.web.services.protocols.soaphttpclientprotocol   websvc2root _root = new websvc2root;    public mynonwsobj getobjectbyname(string whichname)   {     // uses getws2objectbyname retrieve 'real' object     //   turns dumbed down object use     //   calling code knows nothing web services     // identical code wrapperclassws2     mynonwsobj myobj = new mynonwsobj();     commonobj returnedobj = this.getws1objectbyname(string whichname);     myobj.name = returnedobj.name;     myobj.revision = returnedobj.revision;     myobj.description = returnedobj.description;     return myobj;   }    private commonobj getwsobjectbyname(string whichname)   {     // identical code wrapperclassws1     // function object native websvc2root ,     //   retrieved object have identical name/definition     //   1 websvc1root     return _root.getobject(whichname);   } } 

edit2: functions return commonobj type each web service not need named differently in 2 wrapper classes. getwsobjectbyname code not identical due _root variable initialization there. fixed code above.

edit1: fixed errors in paragraph below , added example code above.

in real-life case, commonobj classes named same , have same definitions in both web service name spaces. functions web service objects have same name , definition, class definitions containing 'get web service object' functions have different names. have 1 definition each both getobjectbyname , getwsobjectbyname.

i thought using 1 class , defining module level variables objects each web service, setting them namespace-specific objects. keep of code common, haven't tried it. (i admit kind of crazy approach rooted in vb6 experience it's common practice).

to sure i'm clear: have written working code first web service. have same working code second web service copying code first wrapper class new class, changing 'using' statement, class name, , type of module level variable representing web services root class. solve today's problem while setting stage tomorrow's problem.

edit: had wrong data type on _root2 variable.

edit: search clarity continues... not writing web services , have no real input on how constructed. trying 'wrap' existing web services in code can use calling code/projects have no knowledge of, or reference web services.

// abstract class shared code abstract class wrapperclassws<t> // t enables generics on abstract class {     private abstract t _root { get; } // _root of type 't' (a generic type)      public mynonwsobj getobjectbyname(string whichname)     {         // ...     }      private commonobj getwsobjectbyname(string whichname)     {         // ...     } }  // implementation of ws1 wrapper class wrapperclassws1 : wrapperclassws<websvc1root> {     override websvc1root _root      {                   {              return ...; // return root namespace         }      }         }  // implementation of ws2 wrapper class wrapperclassws1 : wrapperclassws<websvc2root> {     override websvc2root _root      {                   {              return ...; // return root namespace         }      }         } 

edit: added generics abstract class implemented type can of 2 different _root classes.

if want share logic , code, you're going need keep stuff same, edited example generics can make _root variable 2 different types can't call variable '_root1' , '_root2' if want share logic unless roots fundamentally different variables , used in different ways. may want solid principles of programming. solid principles can give clues on how share logic , separate portions of programming not writing code hard manage later on.


Comments