views:

93

answers:

2

We probably need to write our own serializers for the classes in our (largeish) C# app - BinarySerializer is too slow and verbose, and protobuf-net has problems with interface-based properties (of which we have loads).

Does anyone have any good tips and/or warnings? I suspect we should be using BinaryWriter and BinaryReader, but we haven't done much bit-fiddling in C# yet, and any gotchas would be appreciated!

Similarly, does anyone know of a hand-rolled serializer with source code I could look at?

A: 

Consider Type Convertors. We use them extensively with ViewState and they work great.

Fahad
not to go on a tangent - but injecting serialized objects into ViewState is the most horrible thing i've heard today
Pete Amundson
Pete - Thats what ASP.net also does to maintain state accross post backs. Thats what ViewState is for.
Fahad
+3  A: 

Tips / warnings? It can be a serious amount of code. I suggest it would be easier to write some simple code that transforms your existing model into a simple DTO that can be serialized trivially by your choice of existing serialization API. Having a DTO layer (separate to the domain entities) also allows much simpler maintenance - you can refactor your domain entities without breaking the serialized data.

The options for doing it yourself (keeping in mind that you emphasised that the model is fairly complex, and you aren't bit-fiddlers):

  • write type-specific serialization manually; very easy to get wrong, very laborious
  • write a general purpose library; crazy amounts of work solving all the edge cases

And that is after you have figured out a serialization format that works robustly, extensibly, and supports the scenarios you need (inheritance, etc).

Marc Gravell
We are not *C#* bit-fiddlers; In fact, I'm not any kind, but my colleagues certainly are.
Joel in Gö
I don't see how a DTO layer would speed up serialization. The problem presumably lies with the inherent wordyness of the .Net BinarySerializer, together with all the funky reflection and so on it must do to work generically. That would not change with a DTO layer, would it?
Joel in Gö
@Joel - well, perhaps if the DTO was designed to be suitable for protobuf-net ;p
Marc Gravell
Nice try ;) But I think the amount of effort needed for a DTO layer would be about the same as for a straightforward homebrew serialization... after all, if I am sending the data to another class, it might as well be via a StreamWriter or something.
Joel in Gö