Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two classes, same methods, one object?

Tags:

c#

I'm working on a C# project making use of Oracle's "Siebel CRM" product. Siebel provides two namesspaces and classes that are used to programatically connect to it's services depending on whether you want to connect to the server or connect to a client session:

SiebelBusObjectInterfaces.SiebelDataControl
TWSiebelLib.SiebelWebApplication

SiebelDataControl exposes a specific method for explicitly logging in to a server based resource:

bool SiebelDataControl.Login(string connString, string userName, string passWord)

All other methods are identical between the two classes..

I'm currently maintaining two version of my application - one for each of the distinct connectivity classes. As the functionality that I build get's ever more inclusive and complex, it's becoming a real struggle to maintain both applications separately. I have split out the connectivity piece into it's own class, but I still have to copy and paste code between applications as I develop.

Ideally, I want a single VS solution that has one instance of all my custom code and some logic that determines which of the two connectivity classes to use, decided by the user.

Theoretically, I want something like:

class SiebelAppWrapper {

    public Object m_SiebelAppInstance;
    private Boolean m_isConnected;
    private short m_conType;

    public static short SERVER_CON = 1;
    public static short CLIENT_CON = 2;

    public SiebelAppWrapper(short conType) {
        if (conType == SERVER_CON) {
            m_SiebelAppInstance = new SiebelDataControl();
          } else if conType == CLIENT_CON {
            m_SiebelAppInstance = new SiebelWebApplication();
        } 
        m_conType = conType;
    }
}

But, for obvious reasons, the compiler isn't happy with this.

I'm quite new to C# and .NET development in general, so I'm hoping that there is some way for me to incorporate both classes without replicating all of the dependend code.

Many thanks in advance for any help.

like image 971
mroshaw Avatar asked Dec 09 '22 08:12

mroshaw


2 Answers

From your description it sounds like a workable option would be to:

  1. Create an interface ISiebelWhatever that describes all the common functionality between the two classes that you are going to be using.
  2. Derive two types, one from each source class, and make these new types implement ISiebelWhatever.
  3. Use ISiebelWhatever as the type of m_SiebelAppInstance.

Of course this will only work if the source classes are not sealed and it won't help with methods that are not identical in signature. Furthermore, the common interface approach is somewhat obvious and this raises the question: is there any chance the source library already does this? Or perhaps the two classes share a common base? You should check first.

If there are methods that are not shared between the two classes but which you need to use, you will additionally have to decide on a common signature (declared in ISiebelWhatever) and make both of your subclasses implement it by appropriately forwarding to their existing non-shared methods.

like image 139
Jon Avatar answered Dec 11 '22 08:12

Jon


If you have a lot of method in common why don't you use inheritence or interfaces ?

like image 27
Anthony Raymond Avatar answered Dec 11 '22 08:12

Anthony Raymond