定义外部系统集成的接口

时间:2017-05-10 13:22:02

标签: c# oop design-patterns interface integration

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

因此,目标是定义一个接口,以确保所有集成类符合某些操作。 e.g。

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

但是每个外部系统都有自己需要引用的封闭SDK(无法编辑)。 e.g

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

在这种情况下,有没有办法继续使用接口来确保所有集成都执行某个操作?或许还有另一种模式?

2 个答案:

答案 0 :(得分:1)

这是泛型来玩的地方:

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

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

答案 1 :(得分:0)

除了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的anwser和我之间的选择将取决于你想要的(并且可以!)抽象你的不同提供者以及你想如何使用你的API。