开发者

Best datatype to return for XML serialized data

I am creating a generic Android to C# tcp stack. On the C# side I would like to implement an interface called ITcpSerializable.

The result of this serialization would then be sent over my开发者_如何学编程 tcp connection as raw xml.

The current definition is as such.

public interface ITcpSerializable
{
    StringBuilder Serialize();
}

However what I am wondering is what is the best return type for a method like Serialize() when you expect that on some occasions the dataset may be rather large.

It seems like there are so many alternatives for return types for such a method. Stream, textreader, xmldocument, perhaps even byte[] etc... What would be the best one??

PS: I realize this is somewhat subjective but am genuinely in need of some advice.


If you are developing a component to be used by third-party then you better stick to the serialization pattern of .NET, google for an article of that.

I believe the answer will be to return byte[] but maybe you will learn more about the pattern if you will read an article.

If you are developing for yourself stick to whats working for you.


Since you are talking TCP, I would use a Stream pattern, not a return type - i.e.

void Read(Stream source);
void Write(Stream destination);

This is then format independent. However, for "separation of concerns" I would favour a separate serializer than objects that also know about serialization... Either it's job is to be an object model, or it's job is to know how to serialize things; rarely both.

Personally, on a socket I'd use something denser than XML though; I have very strong tendencies towards protobuf (but as an implementor (protobuf-net) I'm biased).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜