"使用"具有几乎相同功能的两个不同库

dis*_*kid 8 .net c# soap web-services

我正在使用SOAP Web服务.Web服务为其每个客户指定单独的服务URL.我不知道他们为什么那样做.它们的所有功能和参数在技术上都是相同的.但是,如果我想编写一个服务程序,我必须知道每个公司是否有意.这意味着对于一家名为"apple"的公司,我必须使用以下使用声明:

using DMDelivery.apple;
Run Code Online (Sandbox Code Playgroud)

另一个叫做"橙色"

using DMDelivery.orange;
Run Code Online (Sandbox Code Playgroud)

但我希望我的程序可以为所有这些程序工作,并将公司名称或服务参考点作为参数.

更新:如果我必须为每个客户编写一个单独的应用程序,那么我将不得不通过每一个小的更改保持所有这些应用程序彼此更新,这将是随着客户数量增加而导致的低效工作.

谁能想到解决方案?我将不胜感激.

pol*_*ran 3

如果您的所有服务都有一个基本契约(接口),您可以使用一种工厂来实例化您的具体服务,并且仅在客户端代码(调用代码)中引用您的接口。

//service interface
public interface IFruitService{
  void SomeOperation();
}

//apple service
public class AppleService : IFruitService{
  public void SomeOperation(){
    //implementation
  }
}
Run Code Online (Sandbox Code Playgroud)

例如,有一种工厂类(您可以将您的using语句放在这里)

public static class ServiceFactory{
  public static IFruitService CreateService(string kind){
    if(kind == "apple")
      return new AppleService();
    else if(kind == "orange")
      return new OrangeService();
    else
      return null;
  }
}
Run Code Online (Sandbox Code Playgroud)

在您的调用代码中(您只需using为包含您的接口的命名空间添加一条语句):

string fruitKind = //get it from configuration
IFruitService service = ServiceFactory.CreateService( fruitKind );
service.SomeOperation();
Run Code Online (Sandbox Code Playgroud)

您还可以使用依赖注入原理。