views:

80

answers:

1

My superior once told me that a bad way of calling a web service or a wcf service is treating it like its a referenced assembly i.e. instantiate a class and call the methods.

I don't see any other way of doing it though. Are there some best practices that I should be following when I interact with external web services / services or is this fine:

public class ServiceProxyManager : IServiceProxyManager
{
    private Service1Soap _externalService;

    public ServiceProxyManager()
    {
        _externalService = new Service1SoapClient();
    }
}
+2  A: 

It's cleaner if you inject the web service into the code that uses it. That way you can write tests by mocking out the interface. I've had a lot of success with that approach in the past.

Of course something will have to create an interface of the service, but that doesn't have to be the code that calls into it...

Jon Skeet
I'm sorry I don't understand what you mean by injecting the web service into the code that uses it.
TheLearner
Instead of creating it in the constructor to `ServiceProxyManager`, accept it as a parameter. Wrap it in an interface so you can use anything which "looks like" the service, including mocks.
Jon Skeet
Oh ok yeah I have done so. But otherwise do you think calling it like a class is fine?
TheLearner
What do you mean by "calling it like a class"? How else would you call it?
Jon Skeet
I guess my superior was trying to say he doesn't like using the proxy classes that visual studio generates. But I don't see what the problem with them is.
TheLearner
Try to find out how he'd do it instead.
Jon Skeet