统一访问Web服务 [英] Accessing web services uniformly

查看:88
本文介绍了统一访问Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个Web服务,这三个服务是由三个不同的供应商开发的,并且具有不同的URL;输入参数和输出参数.它们都是ASMX Web服务.它们用于从第三方关系数据库中删除记录,例如我提供了一个personID,并且从一个系统中删除了一个人,并且所有链接到该人的东西都被删除了.在另一个系统中,我提供了一个订单ID,并且删除了与该订单相关的所有内容.

I have three web services, which were developed by three different vendors and have different URLs; input parameters and output parameters. They are all ASMX web services. They are used to delete records from third party relational databases e.g. I supply a personID and a person is deleted from one system and everything linked to the person. In another system I supply an order ID and everything linked to the order is deleted.

我有几种选择:

  1. 创建一个包装器类,该类负责访问Web服务;提供公共输入参数并接受公共输出参数.这堂课有很多责任.
  2. 创建三个包装器类,即为每个Web服务创建一个包装器
  3. 修改Visual Studio生成的代理

哪种方法最好?

推荐答案

我建议允许Visual Studio自动生成适当的代理类.然后,我将为每个Web服务实现一个包装器类,以便所有包装器类都可以实现相同的接口.例如,您可以创建一个通用的界面,如下所示:

I would recommend allowing Visual Studio to automatically generate the appropriate proxy classes. I would then implement a wrapper class for each web service so that all of the wrapper classes could implement the same interface. For instance, you may make a common interface that looks like this:

Public Interface IPersonBusiness
    Sub DeletePerson(personId As String)
End Interface

然后,假设您有两个Web服务.首先,我们将其称为WebService1,它具有Delete方法,该方法获取人员ID,后跟删除时间.第二个Web服务,我们将其称为WebService2,具有一个DeletePeople方法,该方法采用一个人员ID数组.我们可以使用上面的通用接口包装这两个Web服务,如下所示:

Then, lets say you had two web services. The first, we'll call it WebService1, has a Delete method which takes a person ID followed by the deletion time. The second web service, we'll call it WebService2, has a DeletePeople method which takes an array of person ID's. We could wrap both of these web services using the above common interface, like this:

Public Class WebService1Wrapper
    Implements IPersonBusiness

    Public Sub New(proxy As WebService1)
        _proxy = proxy
    End Sub

    Private _proxy As WebService1

    Public Sub DeletePerson(personId As String) Implements IPersonBusiness.DeletePerson
        _proxy.Delete(personId, Date.Now)
    End Sub
End Class

Public Class WebService2Wrapper
    Implements IPersonBusiness

    Public Sub New(proxy As WebService2)
        _proxy = proxy
    End Sub

    Private _proxy As WebService2

    Public Sub DeletePerson(personId As String) Implements IPersonBusiness.DeletePerson
        _proxy.DeletePeople({personId})
    End Sub
End Classs

除非您确实需要,否则我将避免编写您自己的代理代码.例如,如果您需要基于一些外部设置来动态调用任何Web服务,这些外部设置会告诉您要传递的方法名称和参数等,那么值得研究.

I would avoid writing your own proxy code unless you really need to. For instance, if you needed to dynamically call any web service based on some external settings which tell you the method name and parameters to pass, or something like that, then it would be worth looking into.

我还将避免将调用任何Web服务的所有逻辑放在单个包装器类中.这样做会使代码变得不必要地丑陋和混乱,尤其是在将来需要添加其他Web服务的情况下.

I would also avoid putting all of the logic to call any of the web services into a single wrapper class. Doing so will make the code unnecessarily ugly and confusing, especially if you need to add additional web services in the future.

这篇关于统一访问Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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