Are there any Tutorials for Protobuf-net?

10,553

Solution 1

Dictionary<TKey,TValue> should largely just work (in "v1" at least; I haven't written that yet for "v2").

I fully admit that the documentation is... sparse. Things that leap to mind:

  • Getting Started (and there are a few other pages in the wiki)
  • there are a host of things in the trunk; the "Examples" project doubles as the unit-tests, so may help
  • my blog

I have a lot happening at the moment (I've just changed jobs, etc), but my priorities are:

  • get a stable "v2" beta released, at least the core feature set
  • improve the documentation

Time is my biggest enemy. But if you have a specific scenario, feel free to ping me and I'll try to get back to you ASAP.

Re "can I simply plug it in"; that depends on your code ;-p

It needs some way of determining a unique number for each member that you want to serialize. The simplest way to do this is via attributes; it supports any of [XmlElement(Order=n)], [DataMember(Order=n)] or [ProtoMember(n)]. If you already have at least one of these (LINQ-to-SQL includes [DataMember], for example) then it may simply work.

There are options to automatically infer the numbers, but that is brittle and not recommended. Only use this if you know you never need to add more members (it orders them alphabetically, so adding a new AardvarkCount will break everything).

In "v2" (unreleased, but works), you can now handle the metadata independently of the types - i.e. you can use protobuf-net with POCO, unattributed types. You can also bypass the constructors etc (WCF-style). Much more flexible, if you want it. The attribute approach is supported too, of course.

Solution 2

Funnily enough, I just answered another question with a link to protobuf-net.

I found it relatively simple to do, and Marc is very responsive via email. (Hehe, sorry if I'm inviting people to spam you with questions!)

For documentation, I did mention that it was a bit sparse. But here's what I was using it for:

I had an XML based messaging system that was a bit too verbose. So I wanted to replace the messages with something that would encode the same information, but much more compact. Protobuf-net turned out to be perfect for this, and I've not replaced my xmlserialzer-based module with a proto-based one.

It was pretty simple. I went through my project, replacing [XmlInclude] and similar attributes with [ProtoInclude], and I of course replaced the xml serializer with the proto serializer. One or two issues regarding which types could be serialized were resolved with Marc's help, and not very much code refactoring.

Share:
10,553

Related videos on Youtube

akshaykarthik
Author by

akshaykarthik

Updated on April 22, 2022

Comments

  • akshaykarthik
    akshaykarthik about 2 years

    I have been reading about protobuf-net and it is amazing!

    Are there any tutorials that I could use? (More specifically for Dictionary<TKey,TValue> and contracts for generics)

    Are there any tips associated with it?

    Could I simply plug it into my current codebase or are there any changes I need to do?

  • akshaykarthik
    akshaykarthik almost 14 years
    Hey thanks Marc. So, using Dictionary<TKey,TValue> works in v1 and v1 is production ready right? I am not really on any constraint to use the latest version or anything. Only thing is, does it work in .net 4.0? And how thread safe is it?
  • Marc Gravell
    Marc Gravell almost 14 years
    @AKRamkumar "v1" is indeed the version available for download as a precompiled dll. It should be fully thread-safe, and should work fine in .NET 4.0. The "v2" release has many advantages, but it is not essential to switch - they are fully wire-compatible.
  • akshaykarthik
    akshaykarthik almost 14 years
    So usage of Dictionary<TKey,TValue> would work in v2? Would any type for TKey or TValue work or would I be restricted to specific types. (Sorry if I sound noobish, I am not that good a programmer)
  • Marc Gravell
    Marc Gravell almost 14 years
    @AKRamkumar - It will work in "v2" - it just doesn't yet ;p The TKey / TValue must be either recognisable as contracts (via attributes etc), or one of a range of types with inbuilt support (all the common types - int, string, etc). In "v1" only classes are supported for contracts; this gets better in "v2".