简体   繁体   English

使用具有两种相同但名称不同的两种类型的API。 如何重用我的代码?

[英]Consuming an API which has a two types which are the same but have different names. How can I reuse my code?

I'm consuming a web service which returns two types which are exactly the same but are called two different things (yeah, great API I know...). 我正在使用一个Web服务,该服务返回两种类型完全相同但被称为两种不同的东西(是的,我知道很好的API ...)。 One type's called SearchKMQueryResponse , the other is called TopSolutionsKMQueryResponse . 一类被称为SearchKMQueryResponse ,另一种是所谓TopSolutionsKMQueryResponse I am mapping the output from these types into my own model classes and have to use two methods which take different parameter types, but return the same type. 我将这些类型的输出映射到我自己的模型类中,并且必须使用两种采用不同参数类型,但返回相同类型的方法。 The two API types inherit the object type, so I cannot pass in a base type as a parameter. 这两种API类型都继承了object类型,因此我无法将基本类型作为参数传递。 They do not expose interfaces via the API so I'm screwed there too. 它们不通过API公开接口,所以我也被搞砸了。

So is there an elegant solution to stop repeating myself in the below code? 因此,有没有一种优雅的解决方案可以停止在下面的代码中重复我自己? ... ...

EDIT: I'm using .Net v3.5 for this solution. 编辑:我正在为该解决方案使用.Net v3.5。

      public static KmSearchResponse Map(SearchKMQueryResponse response)
    {
        if (response == null)
        {
            return null;
        }
        else
        {
            var myResponse = new KmSearchResponse()
            {
                CountTotal = long.Parse(response.model.instance.resultlist.resultlist[0].Value),
                Message = response.message,
                Status = (MyProject.Model.StatusType)response.status,
            };

            for (var startID = 2; startID < (myResponse.CountTotal * 5); startID += 5)
            {
                myResponse.Results.Add(
                    KmSearchResponseResultsMapper.Map(
                    response.model.instance.resultlist.resultlist, startID)
                    );
            }

            myResponse.Count = myResponse.Results.Count;

            return myResponse;
        }
    }

    public static KmSearchResponse Map(TopSolutionsKMQueryResponse response)
    {
        if (response == null)
        {
            return null;
        }
        else
        {
            var myResponse = new KmSearchResponse()
            {
                CountTotal = long.Parse(response.model.instance.resultlist.resultlist[0].Value),
                Message = response.message,
                Status = (MyProject.Model.StatusType)response.status,
            };

            for (var startID = 2; startID < (myResponse.CountTotal * 5); startID += 5)
            {
                myResponse.Results.Add(
                    KmSearchResponseResultsMapper.Map(
                    response.model.instance.resultlist.resultlist, startID)
                    );
            }

            myResponse.Count = myResponse.Results.Count;

            return myResponse;
        }
    }

I assume SearchKMQueryResponse and TopSolutionsKMQueryResponse are generated WCF data contracts. 我认为SearchKMQueryResponseTopSolutionsKMQueryResponse生成WCF数据契约。 Those are generated as partial classes, so you can write another partial for them where you apply an interface: 它们是作为部分类生成的,因此您可以在应用接口时为它们编写另一个部分:

IQueryResponse
{
    SomeModel model { get; set; }
    SomeMessage message { get; set; }
    SomeStatus status { get; set; }
}

public partial class SearchKMQueryResponse : IQueryResponse { }
public partial class TopSolutionsKMQueryResponse : IQueryResponse { }

Now you can change your mapper to accept an interface as input: 现在,您可以更改映射器以接受接口作为输入:

KmSearchResponse Map(IQueryResponse response)

My gut reaction is to say "leave it as is" - you don't control the API, so there may come a day when the different classes contain different properties. 我的直觉是说“按原样”-您不控制API,因此可能会有一天不同的类包含不同的属性。 If that day ever comes, you'll have to unpick the code where you've 'merged' the two together. 如果有一天,您将不得不取消将两者“合并”在一起的代码。

You have also not specified whether the types of the properties within each of the API's response classes are the same, or whether they too are separate classes using the same naming system. 您还没有指定每个API响应类中的属性类型是否相同,或者它们是否也是使用相同命名系统的单独类。 If they are different, then no form of base class or interface is going to help. 如果它们不同的,那么没有基类或接口的形式是要帮助。

I would create two extra constructors for KmSearchResponse, one accepting a parameter of type SearchKMQueryResponse and the other accepting a parameter of type TopSolutionsKMQueryResponse. 我将为KmSearchResponse创建两个额外的构造函数,一个接受一个SearchKMQueryResponse类型的参数,另一个接受一个TopSolutionsKMQueryResponse类型的参数。 Each constructor can then extract out the fields it needs. 然后,每个构造函数都可以提取出所需的字段。

you could use the dynamic keyword, but you wont get any intellisense. 您可以使用dynamic关键字,但不会获得任何智能感知。

http://www.hanselman.com/blog/C4AndTheDynamicKeywordWhirlwindTourAroundNET4AndVisualStudio2010Beta1.aspx http://www.hanselman.com/blog/C4AndTheDynamicKeywordWhirlwindTourAroundNET4AndVisualStudio2010Beta1.aspx

EDIT: Alternatively you could write a strongly typed wrapper for each class and share a base type 编辑:或者,您可以为每个类编写一个强类型包装器并共享基本类型

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重用稍有不同的代码 - How to reuse a code which is slightly different 我有 3 个不同的类,具有相同的方法名称。 需要在使用前检查并应用它们的正确实例c# - I have 3 different classes, with the same method names. Need to check before usage and apply the right instance of them c# 如何对具有相同成员的不同类型参数使用相同的函数? - How can I use the same function for different type parameters which have the same member? 如何使一个路由指向两个不同的控制器端点,该端点在WEB Api 2中接受不同的参数 - How to have a Route which points to two different controller end points which accepts different arguments in WEB Api 2 使用多个服务引用(不同类型,相同方法名称)在单元测试中重用代码 - Reuse code in a unit test using several Service References (different types, same method names) 当两个列表具有不同的类型时,如何合并它们? - How can I merge two lists when they have different types? 首先使用不同的列名配置一对一的映射EF代码。 流利的API - Configure one-to-one mapping EF code first with different column names. Fluent API 是否可以创建一个字典,其中的值可以具有多种数据类型? (另外我如何遍历一个数组作为我的值之一) - Is it possible to create a dictionary in which values can have multiple data types? (Also how do I iterate through an array as one of my values) 如何重用相同的变量,但用于不同的类型? - How to reuse the same variable, but for different types? 我如何使方法泛型返回不同的类型,但接受相同类型的实例? - How do I make the method/s generic which returns different types, but accepts the same type of instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM