开发者

Silverlight ItemsSource and web service

I have a simple Silverlight app which simply sets a DataGrid's ItemsSourc开发者_开发技巧e to the result of a web service. This all works OK, but now I am trying to make it more abstract. I would like to pass a parameter to the web service and return either a List<Customer> or a List<Product> for example depending on the parameter.

But I just can't seem to find a way to pass back an abstract list. It seems it has to be a list of a specific type. Is this right?

Thanks,

AJ


The web service definition must know the structure of what is being returned so that it knows what fields to include in the SOAP envelope and so that the subscriber to that service knows what to expect. If you want to return one or the other you should use 2 different services or create a type that can represent both and return a collection of that, then you'd have to wrap it on the client side so that you can hide the details that don't relate to the specific use.

For example,

struct CustomerOrProduct
{
   ItemType type = ItemType.Customer; // This indicates the type that the struct is currently representing

   string name;     // This is for products and customers
   string address;  // This is for customers only
}

Obviously this struct needs fleshing out with property accessors, etc. but it should give you the idea. However, I don't recommend doing it this way. Instead you should use separate services for each type you want to retrieve. After all, if you know enough to pass the needed type as a parameter, you know enough to call a different service.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜