对 SOAP Web 服务调用使用更多 OOP 方法而不是 switch 语句 [英] Using a more OOP method instead of switch statements for SOAP web service calls

查看:60
本文介绍了对 SOAP Web 服务调用使用更多 OOP 方法而不是 switch 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有更好的方法来完成我在下面的工作.我们有多个 (12) SOAP Web 服务,全部来自同一供应商.所以所有的调用都是相同的(例如 getclaim()、editclaim()、addclaim() 等)

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.svchttps://trustonline.delawarecpf.com/tows/webservicepcc.svc

但是我们将所有 ws 调用放在单独的文件中(超级冗余),每个 Web 服务一个.所以我想弄清楚如何将它们组合成一个文件.

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.

我认为可以在下面做这样的事情,我有一个 switch 或 if 语句来确定 web 服务和每个服务的用户对象,但这似乎有点非面向对象,所以我想看看有更好的方法吗?

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);
    }

推荐答案

如果您要大量使用该逻辑,不妨将其放入另一个工厂并调用它.

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.

我可能会考虑将 int 更改为 enum.类似的东西:

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();
        }
    }
}

这篇关于对 SOAP Web 服务调用使用更多 OOP 方法而不是 switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆