[英]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. 我认为
SearchKMQueryResponse
和TopSolutionsKMQueryResponse
生成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.