I have a webservice that can return data in several formats. For example json and xml. I am creating 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 instance:
List<Order> GetOrders(int customerId) string GetOrders(int customerId) XMLDocument GetOrders(customerId) Customer GetCustomer(int customerId) string GetCustomer(int customerId) XMLDocument GetCustomer(int customerId)
I have an idea about using api freely, where you would call the SetFormat () method, which would then return a common interface for the above methods. But I am fixated on what this interface will look like, since an implementation that returns serialized objects returns objects of different types.
Another simpler solution would be to simply use methods that return serialized objects, and then add an external parameter as follows:
List<Order> GetOrders(int customerId, out string data)
but this is not a very pleasant solution, I think ....
UPDATE
I preferred the non-generic solution suggested by Sjoerd, I made my problem too complicated. This is what I ended up with:
public class ServiceEntity { List<Order> GetOrders(int customerId).... } public class ServiceJson { string GetOrders(int customerId).... } public class ServiceXml { XmlDocument GetOrders(int customerId).... }
Then a quick service class:
public class Service : IService { .... public AsJson() { return new ServiceJson(); } public AsEntity() { return new ServiceEntity(); } public AsXml() { return new ServiceXml(); } }
Used as follows:
string json = Service.New().AsJson().GetCategories(1); List<Order> = Service.New().AsEntity().GetCategories(1);
Thanks for all the answers!