Home > Mobile >  Can gRPC.Net recognize the same reference type during serialization?
Can gRPC.Net recognize the same reference type during serialization?

Time:02-05

Will the same reference type be identified and compressed during encoding in net gPRC? I now have a many-to-many relationship, such as students and teachers, I directly define a

repeated Teacher Teachers = 1;
message Teacher{
  int Id = 1;
  repeated Student Students = 2;
}

Then just include the student data in the teacher(Will this cause the same student to be serialized for multiple transfers?), or do you need to maintain an association relationship yourself and then reconstruct the association relationship on the client?

repeated Teacher Teachers = 1;
repeated Student Students = 2;
message Student{
  int Id = 1;
  repated int StudentId = 2;

}

Is the same object still the same after being referenced by multiple lists and serialized and deserialized?

CodePudding user response:

Regarding your first question Will the same reference type be identified and compressed during encoding in .NET gPRC? for the following message format you posted:

repeated Teacher Teachers = 1;
message Teacher{
  int Id = 1;
  repeated Student Students = 2;
}

I am pretty sure the answer here is NO. The protobuf compiler protoc generates C# code-behind files from the defined protobuf messages for serialization and deserialization. The repeated field Teachers that you defined will be represented in the code-behind files by the RepeatedField Type, which will then be used to serialize and deserialize the Teachers list to the binary wire format.

So what will be generated by protoc is a RepeatedField of the type Teacher, that contains a RepeatedField of the type Student. At this point there is already no many-to-many relationship, basically all you have in memory is a list of teachers, and each teacher links to a list of students that has its own memory allocated, and if a student appears twice in different student lists, the student object will also be twice in memory in different locations even though they have the same Id values (and therefore multiple references and memory locations already exist for identical student objects).

So even before the binary serialization for gRPC starts and before any data has been transferred, you already have no many-to-many relationship in the student and teacher lists in the code that is used for serialization and deserialization.


Regarding your main question Can gRPC.Net recognize the same reference type during serialization? the answer is that gRPC will serialize objects with the references that are defined in the code-behind files. And in this case, there is no many-to-many relationship defined within them, and student objects with the same Id in different lists will be duplicated in different memory locations and have different references. So if you want such a relationship, you have to model protobuf messages that create such relationships in the code-behind files, otherwise you have to manually reconstruct them after deserialization like you described.

  • Related