Serialization and retrocompatibility
Hi. So i'm working on a client that communicates with a server using serialization. I need the old client to be able to communicate with the new server.
My problem is some of the fields in my app were created with auto properties (get; set;) way. And i now have to change them to private _id and public Id.
I've looked around and found that for the auto properties, the compiler Will create a private field called <Id>kBackingField. This property is used for serialization.
For now i've been using a custom serialization where i create the <nameof(Id)>kBackingField.
This isnt very pretty and i've also Just discovered it gets even worse with inheritance. Is there a way to fix this retrocompatibility problem in an easier way ?
22 Replies
Discord ruined the format but those undrline are because it is "k__BackingField"
That's why we have code blocks
What format does it serialize to/from?
If it's something like JSON or Protobuf, the issues with case can be overcome
Binary
Ah, oof
I guess your best bet would be a source generator that generates properties from fields
🤔
What do you mean ?
My goal is just to have serialize/deserialize work when the old client communicates with those k__BackingField it only knows
what exactly are you using for the serialization
is this BinaryFormatter?
.
ye...
that app is older than me
your first concern should not be serialization compatibility, it should be removing BinaryFormatter from your protocol
i've already talked about that
for now i have to deal with the issue i mentionned here
to people in charge i mean
what you do is you implement ISerializable and manually extract the values using the old field names https://learn.microsoft.com/en-us/previous-versions/dotnet/fundamentals/serialization/binary/custom-serialization#implement-the-iserializable-interface
if you can't implement ISerializable there is another way of doing it
this is what i've been doing but it seem pretty tedious (especially with inheritance)
i was asking if there was a simpler way to do so
no
it should not be particularly more difficult with inheritance
you can call the base implementation of the serialization constructor/GetObjectData
alright then
if you have a lot of members to do this with, it could be worth whipping up a source generator
that SG would look for something like an OldNameAttribute on fields or props
and generate the necessary bits of ISerializable accordingly
any tutorial on what that is ? i've never eard of that
$ig
If you want to make an incremental source generator, please make sure to read through both the design document and the cookbook before starting.
it's an advanced topic
note that you'll need to be building with a recent .NET SDK (though your projects can still target older runtimes)
i think making the usage of BInaryFormatter more convenient is probably a bad use of time
the whole product is being rewritten by another team if you realy wonder
and they're moving away from BF?
yes
i still need to make the current product work in the meantime tho x)