Question

I decided that returning datasets, working with datasets and datatables in a front end app is a rather bad idea. So with the magic of generics I am returning a generic list via Webservice call (not WCF just plain ol' asmx). That works dandy as long as you need to return one list per call to your webservice.

The scenario I am running into is that I have a more involved screen where there are a few dropdowns and so on. I need to bind those dropdowns to a generic list. Problem is that I don't want to make several web calls, one for each dropdown, to get my data. In the past I would have just returned a dataset and bound a dropdown to a specific table in the dataset.

So, it would be super to return multiple generic lists in one web call.

Things I've tried:

  1. Using a List of Lists
  2. Using a Collection of generic Lists
  3. Creating a base class and using a Collection of List<Base>, there are conversion issues there when trying to stuff objects into that List<Base>.
  4. Creating a class that has properties that return List<MyOneObject>, List<MySecondObject>, etc. That works but it's kinda messy... I have lots of classes and screens in the app where this would happen. I could create a class like this that has a properties of List<MyType> for each class, but like I said I think that may get out of control.

So at this point I have two options, #4 in the above list, or just return a dataset which I would prefer not to do :0)

Has anyone run into this before?

Was it helpful?

Solution

Well I don't think returning multiple lists is a good idea, but if you are going to do it, I would create a wrapper class with each list exposed as a property in that class.

Something like

class Wrapper
{
   public List<object1> Object1List {get;set;}
   public List<object1> Object2List {get;set;}

}

OTHER TIPS

The answer is #4, via a class, either that or multiple calls, which you would rather not do

Well it seems that I start all my answers today with a question? Why wouldn't you wanna do multiple calls behind the scene?

If you use one async call på dropdown/control you need to bind you'd get a more fluent user experience. (especially true for large independant lists) simply because if you only do one call all dropdown/conrtols will need to wait for all the other contrls data to be returned.

That being said: If the lists you're returning a close to each other in length youd could use a dictionary, using key to represent "property name" and the value to well represent the value. Very little code require and converting what ever the source is to a dictionary with .ToDictionary() is pretty straight forward (if the source is not IEnumerable implementing .ToDictionary() for that source is usually straight forward)

Given your scenario, I'd say returning the result in XML would be the best way to go. You could not only return as many lists as you wanted, but you can organize them in any way and most importantly this would be a languange-agnostic, universal solution. You could also transform your result set directly to xml.

If you return a .NET class you are limited to only .NET languages calling the service and if that changed you'd have to re-write the wrapper classes, etc. With XML you could call the service from any language. So if you are trying to de-couple your front-end app from your data/model layer, this would be a good route to take.

You can still deserialize the XML result into a .NET class if you wish, or just extract the data directly. LINQ to XML is fast and easy to use and would allow you to transform the result if need be.

<result>
  <dropdownA_item>
     <Value/>
  </dropdownA_item>
  <dropdownA_item>
  <dropdownA_item>
  <dropdownB_item>
  <dropdownB_item>
  <dropdwonC_item>
  // return as many different lists for as many different drop downs
</result>

// manually extract the data or deserialize into .net class
XElement result = GetResult();
dropdownBox[x].Items = result // you could even have an array of dropDownBoxes[x] and bind to all of them in one loop
    .Where(o => o.Name == "dropdown" + x)
    .Select(o => o.Element("Value").Value) );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top