Protobuf-net is a very great binary serializer for .NET. I use it heavily on my project (memory, file, and over-the-wire serialization).
One of the confusions I recently had is that after I initialized a List, I assumed that it will be serialized into the stream. However, having a zero-length List, it always return null whenever protobuf deserializes the stream. I had seen some developers do a null checking or implement null-coalescing operator operations after they deserialize the stream and those null checking are scattered through-out the code (which violates the DRY principle and is ugly).
One very simple work-around for this is to provide a standard constructor and initialize the List on that constructor. Here is a sample unit test code which demonstrates that.
Special thanks to the protobuf-net author: Marc Gravell
No comments:
Post a Comment