Protobuf-net implicit contracts
Does anyone have a working example where protobuf-net can be used as a replacement for BinaryFormatter? Is it possible?
Actually I just need a serializer for one post type that looks like
public class A {
public B[] Bs { get;set; }
public C[] Cs { get;set; }
}
All types are defined in another assembly and have many properties. Is it possible to automatically generate proto-contracts with all public properties included for class A and other used types (B, C), so something like
var formatter = ProtoBuf.Serializer.CreateFormatter<A>()
only works?
source to share
First, protobuf-net is not intended and does not claim to be a 100% replacement for BinaryFormatter
. It has several different functions.
Could you think a little? There is support in principle, ImplicitFields
but currently this is only available through [ProtoContract]
and it cannot be done with RuntimeTypeModel
, which is a bit painful and included in my list. Although, I must point out that I find implicit fields a bit risky and should only be done if you know the internals of the DTO will not change! But: to answer your question, you can iterate over the types you expect and add them to the model manually:
static void Main()
{
Prepare(typeof(A), typeof(B), typeof(C));
// if you really want to use IFormatter...
var formatter = RuntimeTypeModel.Default.CreateFormatter(typeof (A));
var obj = new A {Bs = new B[] {new B()}};
using (var ms = new MemoryStream())
{
formatter.Serialize(ms, obj);
ms.Position = 0;
var clone = formatter.Deserialize(ms);
}
}
static void Prepare(params Type[] types)
{
if(types != null) foreach(var type in types) Prepare(type);
}
static void Prepare(Type type)
{
if(type != null && !RuntimeTypeModel.Default.IsDefined(type))
{
Debug.WriteLine("Preparing: " + type.FullName);
// note this has no defined sort, so invent one
var props = type.GetProperties();
Array.Sort(props, (x, y) => string.Compare(
x.Name, y.Name, StringComparison.Ordinal));
var meta = RuntimeTypeModel.Default.Add(type, false);
int fieldNum = 1;
for(int i = 0 ; i < props.Length ; i++)
{
meta.Add(fieldNum++, props[i].Name);
}
}
}
Note that use IFormatter
is completely unnecessary here; you can also use RuntimeTypeModel.Default.Serialize(...)
or Serializer.Serialize<T>(...)
.
As a footnote: I would advise defining models in a more ... repeatable way. For example:
RuntimeTypeModel.Default.Add(typeof(A)).Add("Bs", "Cs");
RuntimeTypeModel.Default.Add(typeof(B)).Add("Foo");
RuntimeTypeModel.Default.Add(typeof(C)).Add("Bar", "Blap", "Blop");
source to share
This is the final version of the Prepare function that I used:
static void Prepare(params Type[] types)
{
foreach (var type in types)
{
if (type != null && !RuntimeTypeModel.Default.IsDefined(type))
{
if (type.Namespace.StartsWith("System"))
return;
Debug.WriteLine("Preparing: " + type.FullName);
// note this has no defined sort, so invent one
var props = type.GetProperties();
Array.Sort(props, (x, y) => string.Compare(
x.Name, y.Name, StringComparison.Ordinal));
var meta = RuntimeTypeModel.Default.Add(type, false);
int fieldNum = 1;
for (int i = 0; i < props.Length; i++)
if (props[i].CanWrite)
{
meta.Add(fieldNum++, props[i].Name);
if (!RuntimeTypeModel.Default.IsDefined(props[i].PropertyType))
if (props[i].PropertyType.HasElementType)
Prepare(props[i].PropertyType.GetElementType()); //T[]
else if (props[i].PropertyType.IsGenericType)
Prepare(props[i].PropertyType.GetGenericArguments()); //List<T>
else
Prepare(props[i].PropertyType);
}
}
}
}
In case anyone is interested, this is the result of my test (serialization + deserialization):
BinaryFormatter 10000 messages: 2.131s 5028 bytes/msg
Json.NET Bson 10000 messages: 1.679s 1071 bytes/msg
MetSys Bson 10000 messages: 1.581s 1035 bytes/msg
Protobuf 10000 messages: 0.145s 109 bytes/msg
MsgPack 10000 messages: 0.844s 106 bytes/msg
Notes:
- The MetSys.Bson library does not handle the decimal type.
- The MsgPack library has problems with read-only properties.
Thanks Marc for your help and such a great library.
source to share