tags:

views:

69

answers:

1

We use .net binary serialization right now to serialize data to persist. It does the job but doesn't fell like it's intended to use for that purpose as changing data is not so trivial.

So i started looking into protobuf as it is designed around that purpose.

i am looking at protobuf-net to how to use it. Below are some thing i don't know yet. if you got an answer already it will save my day.

  • I don't care about .proto files as product is just in .net. So can i use protobuf-net without having .proto files.
  • If yes, can i later generate .proto file out of my c# classes if need port to other language
+1  A: 

Yes, protobuf-net works without .proto files. In fact, for protobuf-net the core was written long before the .proto support. All you need is something that allows it to obtain a unique number for each member you want to serialize. In "v2" (not released) you can do this externally, but in the available dll this means attributes. This can be the bespoke protobuf-net attributes, or you can use (for example) the DataContractSerializer attributes if that is more convenient:

[DataContract]
public class Foo {
    [DataMember(Order=1)]
    public int Abc {get;set;}

    [DataMember(Order=2)]
    public string Def {get;set;}
}

There is support for generating a .proto file (Serializer.GetProto) - however, there are some caveats:

  • protobuf-net supports inheritance; there is no definition of inheritance in the Google protobuf spec. If you think interop is a concern down the line, it may be best to avoid inheritance (although there is a way of interpreting it; probofuf-net always emits a valid protobuf stream)
  • other complex types may require interpretation; for example while float and double map directly to protobuf types (available in every implementation), that is not true of decimal and DateTime; if you think interop is an issue, it may be worth simplifying this from the start - for example deciding that you are going to send a DateTime, perhaps send a long that represents the offset into an epoch instead? easy to interpret in any implementation.
Marc Gravell
Thanks for great detailed answer
mamu