为外部系统集成定义接口

Defining an interface for external system integration

我想将我的应用程序与 X 个外部系统集成。与每个外部系统的集成将具有相同类型的操作,但将在单独的 类.

中处理

因此,目标是定义一个接口,确保所有集成 类 符合特定操作。例如

public interface IOrderIntegration
{
   //I want to define the ImportOrder action here, so that all future integrations conform
}

但每个外部系统都有自己封闭的SDK(不可编辑)需要参考。例如

public class EbayOrderIntegration : IOrderIntegration
{
    void ImportOrder(Ebay.SDK.Order order)
    {
        //Logic to import Ebay's order
    }
}


public class AmazonOrderIntegration : IOrderIntegration
{
    void ImportOrder(Amazon.SDK.Order order)
    {
        //Logic to import Amazon's order
    }
}

有没有办法在这种情况下仍然使用接口来确保所有集成都执行特定操作?或者另一种模式?

这是泛型发挥作用的地方:

public interface IOrderIntegration<T>
{
   void ImportOrder(T order);
}

public class EbayOrderIntegration : IOrderIntegration<Ebay.SDK.Order order> 
{ 
    void ImportOrder(Ebay.SDK.Order order order)
    {
        // ...
    }
}

不同于 HimBromBeere 的答案的另一种方式(顺便说一句,很好的答案!)。请注意,这只有在您可以在订单级别进行抽象时才有效:

public class OrderIntegration
{
    public void ImportOrder(IOrder order)
    {
        // Only possible if you can abstract all the logic into IOrder
    }
}

public interface IOrder
{
     // Abstract here the order logic
}

public class EbayOrder : IOrder
{
    public EbayOrder(Ebay.SDK.Order order)
    { .. }
}

public class AmazonOrder : IOrder
{
    public AmazonOrder(Amazon.SDK.Order order)
    { .. }
}

HimBromBeere 的答案和我的答案之间的选择将取决于您想要(并且可以!)抽象您的不同提供者以及您想要如何使用您的 API。