问题描述
我有可以以多种格式返回数据的Web服务.例如json和xml.我正在针对此Web服务构建一个简单的C#API,我希望这些方法能够从json,raw json或raw xml返回完全序列化的对象.例如:
I have webservice that can return data in multiple formats. For example json and xml. I'm building a simple C# api against this webservice and I would like the methods to be able to return either fully serialized objects from json, raw json or raw xml. For example:
List<Order> GetOrders(int customerId)
string GetOrders(int customerId)
XMLDocument GetOrders(customerId)
Customer GetCustomer(int customerId)
string GetCustomer(int customerId)
XMLDocument GetCustomer(int customerId)
我有一个关于做一个流畅的api的想法,您可以在其中调用SetFormat()方法,然后该方法将为上述方法返回一个通用接口.但是我一直停留在该接口的外观上,因为返回序列化对象的实现会返回不同类型的对象.
I have an idea about doing a fluent api where you would call a SetFormat() method which would then return a common Interface for the above methods. But I'm stuck on how that interface would look like, since the implementation that returns serialized objects returns objects of different types.
另一个更简单的解决方案是只具有返回序列化对象的方法,然后添加如下这样的out参数:
Another simpler solution would to just have the methods that return serialized objects and then add an out paramater like this:
List<Order> GetOrders(int customerId, out string data)
但是我认为这不是很好的解决方案....
but that's not very nice solution I think....
更新
我更喜欢Sjoerd建议的非通用解决方案,这使我的问题过于复杂.这就是我最终要做的:
I prefered the none-generic solution that Sjoerd suggested, I had over-complicated my problem. This is what I ended up doing:
public class ServiceEntity {
List<Order> GetOrders(int customerId)....
}
public class ServiceJson {
string GetOrders(int customerId)....
}
public class ServiceXml {
XmlDocument GetOrders(int customerId)....
}
然后像这样的流利的服务课程:
Then a fluent service class like this:
public class Service : IService {
....
public AsJson() { return new ServiceJson(); }
public AsEntity() { return new ServiceEntity(); }
public AsXml() { return new ServiceXml(); }
}
像这样使用:
string json = Service.New().AsJson().GetCategories(1);
List<Order> = Service.New().AsEntity().GetCategories(1);
感谢所有回复!
推荐答案
尝试使用泛型很好,但是泛型不是灵丹妙药!
It's nice to try to use generics, but generics are not a silver bullet!
在这种情况下,我想知道与非通用代码相比,您将节省多少代码:
In this case, I wonder how much code would you save compared with the non-generic:
List<Order> GetOrdersAsList(int customerId)
string GetOrdersAsString(int customerId)
XMLDocument GetOrdersAsXml(customerId)
我敢打赌几乎没有!
如果您决定采用非通用方法,则很可能会在内部以以下方式结束:
And in case you decide on the non-generic approach, most likely it would end up internally as:
List<Order> GetOrders(int customerId)
string OrdersToString(List<Order> orders)
XMLDocument OrdersToXml(List<Order> orders)
然后,可以将后两种方法移到单独的类中,从而导致您的 GetOrders()
与格式分离.
Then the latter two methods could be moved to separate classes, resulting in the fact that your GetOrders()
is uncoupled from the format.
在我看来,这比在这种情况下尝试使用泛型好得多而且更清洁!
That seems to me a much better and cleaner approach than trying to use generics in this case!
更新:别误会,我喜欢泛型,并且在许多情况下,泛型使代码更易读.其他几个答案也很有趣,因为它们显示了达到此目的的技术.因此,我建议对其进行研究:它们在其他情况下可能很有用.但是在这种情况下,到目前为止,每个提出的解决方案都存在实际缺陷.这就是为什么我建议在这种情况下使用非泛型.
UPDATE: Don't get me wrong, I like generics and in many cases they make code more readable. Several other answers are very interesting as they show techniques to achieve that. So I recommend to study them: they might be useful in other cases. But in this case, each proposed solution so far has a practical drawback. That's why I recommend non-generics in this case.
这篇关于具有不同返回类型的C#通用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!