Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6727517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:03:32+00:00 2026-05-26T10:03:32+00:00

I have webservice that can return data in multiple formats. For example json and

  • 0

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)

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.

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….

UPDATE

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(); }
}

Used like this:

string json = Service.New().AsJson().GetCategories(1);
List<Order> = Service.New().AsEntity().GetCategories(1);

Thanks for all the replies!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:03:32+00:00Added an answer on May 26, 2026 at 10:03 am

    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)
    

    I bet almost none!

    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)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webservice that when called without specifying a callback will return a
I have a WebService that returns XML in a SOAP response: <?xml version=1.0 encoding=utf-8
I have a webservice method that reads a photo and returns its byte data.
I have a list of objects that I return using webservice. And I try
I'm developing an asp.net webservice application to provide json-formatted data to a widget that
I have a web service that I can only hit if I'm logged into
I have an ASP.NET web service that I can access via a windows program
I hope you can help me. I have a web service that needs to
I have a webservice that exists in an asp.net website. When I publish the
I have a webservice that is called by up to 10 clients. The webservice

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.