C
C#•3w ago
osadriofary

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
osadriofary
osadriofaryOP•3w ago
Discord ruined the format but those undrline are because it is "k__BackingField"
Angius
Angius•3w ago
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
osadriofary
osadriofaryOP•3w ago
Binary
Angius
Angius•3w ago
Ah, oof I guess your best bet would be a source generator that generates properties from fields
osadriofary
osadriofaryOP•3w ago
🤔 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
reflectronic
reflectronic•3w ago
what exactly are you using for the serialization is this BinaryFormatter? .
osadriofary
osadriofaryOP•3w ago
ye... that app is older than me
reflectronic
reflectronic•3w ago
your first concern should not be serialization compatibility, it should be removing BinaryFormatter from your protocol
osadriofary
osadriofaryOP•3w ago
i've already talked about that for now i have to deal with the issue i mentionned here to people in charge i mean
reflectronic
reflectronic•3w ago
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
osadriofary
osadriofaryOP•3w ago
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
reflectronic
reflectronic•3w ago
no it should not be particularly more difficult with inheritance you can call the base implementation of the serialization constructor/GetObjectData
osadriofary
osadriofaryOP•3w ago
alright then
jcotton42
jcotton42•3w ago
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
osadriofary
osadriofaryOP•3w ago
any tutorial on what that is ? i've never eard of that
jcotton42
jcotton42•3w ago
$ig
MODiX
MODiX•3w ago
If you want to make an incremental source generator, please make sure to read through both the design document and the cookbook before starting.
jcotton42
jcotton42•3w ago
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)
reflectronic
reflectronic•3w ago
i think making the usage of BInaryFormatter more convenient is probably a bad use of time
osadriofary
osadriofaryOP•3w ago
the whole product is being rewritten by another team if you realy wonder
jcotton42
jcotton42•3w ago
and they're moving away from BF?
osadriofary
osadriofaryOP•3w ago
yes i still need to make the current product work in the meantime tho x)

Did you find this page helpful?