xdtTransform
xdtTransform

Reputation: 2057

Switch between production and test Webservice

Switch between production and test Webservice.

I have 2 version for the same WebService definition. Each version has its own database url etc.

MyLib.FooWebServicePROD.FooWebService _serviceProd;
MyLib.FooWebServiceTEST.FooWebService _serviceTest;

For now to siwtch form one to the other I used the Rename option in Visual Studio.

I would like to wrap all my instance and definition in a layer of abstration so the programe will not be edited everytime.
So I made mine singleton public sealed class FooBarWrap but with a huge amount a duplication like:

public bool Close()
{
    if (_serviceProd != null)
    {
        _serviceProd.logout(guid);
        log4N.Info("Closing PROD");
    }
    if (_serviceTest != null)
    {
        _serviceTest.logout(guid);
        log4N.Info("Closing TEST");
    }
    return true;
}

public bool Login()
{
    try
    {
        log4N.Info("Connection to FooBar webservice...");
        if (isProd)
        {
            _serviceProd = new MyLib.FooWebServicePROD.FooWebService();
            _serviceProd.Timeout = System.Threading.Timeout.Infinite;
            _serviceProd.Logon(guid);
        }
        else {                    
            _serviceTest = new MyLib.FooWebServiceTEST.FooWebService();
            _serviceTest.Timeout = System.Threading.Timeout.Infinite;
            _serviceTest.Logon(guid);
        }
        log4N.Info("done");
        return true;
    }
    catch (Exception ex)
    {
        log4N.Info("failed !");
        log4N.Error("Echec connexion au webservice FooBar", ex);
        return false;
    }
}

Is there a simplier way to achieve this? Without the client having a reference to one or the other web service, and without the heavy code duplication?

if (FooBarWrap.Instance.Login()){
    //DoSomething   
    var ClientResult = FooBarWrap.Instance.SomeRequest()
}

Upvotes: 1

Views: 177

Answers (1)

matcheek
matcheek

Reputation: 5157

Is there a simplier way to achieve this? Without the client having a reference to one or the other web service, and without the heavy code duplication?

It is.

You could simply use conditional dependency injection where depending on the environment you are or any other condition like host name, port number or url path, you would get different implementation of the service interface.

A simple conditional dependency injection that depending on condition provides one or the other implementation of the same interface.

kernel.Bind<ISomeService>().To<SomeService1>();

kernel.Bind<ISomeService>().To<SomeService2>().When(x => HttpContext.Current[host|port|url path] == "some value");

Ninject calls that kind of injection contextual binding https://github.com/ninject/ninject/wiki/Contextual-Binding

Upvotes: 1

Related Questions