Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

c# - Using a more OOP method instead of switch statements for SOAP web service calls

I'm trying to figure out if there is a better way to accomplish what I have below. We have multiple(12) SOAP web services all from the same vendor. So all the calls are the same (ex. getclaim(), editclaim(), addclaim(), etc, etc)

ex. of two web service urls

https://trustonline.delawarecpf.com/tows/webservicefk.svc https://trustonline.delawarecpf.com/tows/webservicepcc.svc

But we have all of the ws calls in separate files (super redundant), one for each web service. So I'm trying to figure out how to combine them into a single file.

I think can do something like this below, where I have a switch or if statement that determines the web service and the User object from each service, but this seems a little non obj oriented, so I'm looking to see if there is a better way?

Here is what I'm currently doing, but looking for a better way.

    // initializing the web services and fetching some data at the end
    public void InitWebService(string webserviceUrl, int webServiceType)
    { 
        BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
        EndpointAddress endpoint = new EndpointAddress(webserviceUrl);

        ChannelFactory channelFactory = null;
        switch(webServiceType)
        {
            case 1:
                channelFactory = new ChannelFactory<WebServiceAWI>(binding, endpoint);
                break;
            case 2:
                channelFactory = new ChannelFactory<WebServiceBG>(binding, endpoint);
                break;
            etc.
        }            

        var webservice = channelFactory.CreateChannel();
        var user = null; // CAN'T HAVE NULL HERE
        switch(webServiceType)
        {
            case 1:
                user = WebservicereferenceA.User();
                break;
            case 2:
                user = WebserviceReferenceB.User();
                break;
            etc.
        }

        user.UserName = webservice.EncryptValue("someone");
        user.Password = webservice.EncryptValue("password");

        // get some data
        var result = webservice.AttorneysGet(user);
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're going to be using that logic a lot, you may as well put it into another factory and call on that instead.

I'd probably consider changing the int up for an enum. Something like:

public enum ServiceType
{
    A,
    B
}

public class ServiceFactory 
{
    public ChannelFactory CreateChannelFactory(ServiceType type, BasicHttpBinding binding, EndpointAddress endpoint) 
    {
        switch(type) 
        {
            case ServiceType.A: 
                return new ChannelFactory<WebServiceAWI>(binding, endpoint);
            case ServiceType.B:
                return new ChannelFactory<WebServiceBG>(binding, endpoint);
            default:
                throw new NotSupportedException();
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...