How to serialize an IList<T>?
Asked Answered
P

3

15

I've got an OR mapper (iBatis.Net) that returns an IList.

// IList<T> QueryForList<T>(string statementName, object parameterObject);
var data = mapper.QueryForList<Something>(statement, parameters);

I'm trying to use it in an webservice an want to return the data 1:1. Of course I can't return IList in a WebMethod, because it is an interface and therefore not serializable.

I found that the mapper is really returning an List. But I'm afraid to cast it to List because of course the mappers inner workings could change in future versions (and it just feels dirty).

So should I ...

a) return new List<Something>(data);

b) return (List<Something>)data;

c) // your solution here

Thanks a lot!

Pulley answered 21/1, 2009 at 9:9 Comment(0)
R
16

If it really is a List<T> but you want to protect against change and have it still work, then the most performant solution will be to attempt to cast it to a list, and if that fails then create a new list from its content, e.g.

var data = mapper.QueryForList<T>(statement, parameters);
var list = data as List<T> ?? new List<T>(data);

However, you mention that you can't return an interface because it's a web service. This may have been true with ASMX and the XmlSerializer class, but if you build the web service using WCF and use the DataContractSerializer then it will happily serialize collection interfaces (both as inputs to and outputs from the service). That type of change may be somewhat larger than you're looking for though!

Ronni answered 21/1, 2009 at 9:28 Comment(2)
wow.. thats kool. i didnt know u could co that with WCF. rock on!Weinert
Thank you! For this project I will not switch to WCF but it's good to know for future reference. Also I just recognized: I really love the ?? syntax ;-)Pulley
D
2

Why should you serialize IList :) Just use it as a source for your own collection and serialize it:

var data = mapper.QueryForList<T>(statement, parameters);
var yourList = new List<T>(data);
//Serialize yourList here ))
Doublecheck answered 21/1, 2009 at 9:19 Comment(1)
Because if the IList is already a List, it is faster to use it instead of pumping the items into a new List.Pulley
L
0

I don't think you need a c). It should be pretty safe to use solution a).

This solution depends on the public api of IBatis.net. Good api's don't change their public api unless it's really the only solution to a mission critical problem

Hope this helps.

Landslide answered 21/1, 2009 at 9:17 Comment(3)
The fact that the IList returned is a List is not part of the public API; this is the all point of using interfaces.Elvia
Not what i said. returning ilist is part of the public apiLandslide
norbertB, yes, solution a) is safe, but if the IList contains many elements it will be slow, also ... b) whould be the fastest but is not safe, hence my question for a solution c).Pulley

© 2022 - 2024 — McMap. All rights reserved.