用于序列化数据的实体已更改。如何为新实体升级序列化数据?

发布于 2024-10-11 23:00:17 字数 620 浏览 4 评论 0原文

我有一堆简单的实体实例,我已将它们序列化到文件中。将来,我知道这些实体的结构(即,也许我会将 Name 重命名为 Header 或其他名称)。问题是,我不想丢失所有这些旧文件中保存的数据。 的正确方法是什么?

  1. 将旧实体中的数据加载到新实体中
  2. 升级旧文件,以便它们可以与新实体一起使用。

注意:我认为我坚持二进制序列化,而不是 xml 序列化。

提前致谢!

编辑:所以我对我所描述的案例有一个答案。我可以使用 dataContractSerializer 并执行类似操作

[DataMember("bar")]
private string foo;

并更改代码中的名称并保留用于序列化的相同名称。但是以下其他情况又如何呢:

  1. 原始实体具有可以序列化的新成员
  2. 原始实体中的一些序列化成员被删除
  3. 一些成员的功能实际上已更改(假设原始类有一个 FirstName 和 LastName 成员,并且它已被重构为只有一个结合了两者的 FullName 成员)

为了处理这些,我需要某种解释器/翻译器反序列化类,但我不知道应该使用什么

I have a bunch of simple entity instances that I have serialized to a file. In the future, I know that the structure of these entities (ie, maybe I will rename Name to Header or something). The thing is, I don't want to lose the data that I have saved in all these old files. What is the proper way to either

  1. load the data from the old entities into new entities
  2. upgrade the old files so that they can be used with new entities

Note: I think I am stuck with binary serialization, not xml serialization.

Thanks in advance!

Edit: So I have an answer for the case I have described. I can use a dataContractSerializer and do something like

[DataMember("bar")]
private string foo;

and change the name in the code and keep the same name that was used for serialization. But what about the following additional cases:

  1. The original entity has new members which can be serialized
  2. Some serialized members that were in the original entity are removed
  3. Some members have actually changed in function (suppose that the original class had a FirstName and LastName member and it has been refactored to have only a FullName member which combines the two)

To handle these, I need some sort of interpreter/translator deserialization class but I have no idea what I should use

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆梦 2024-10-18 23:00:17

如果您使用过 BinaryFormatter,那么请注意,这是一个 field 序列化器,而不是属性序列化器;您可以通过不更改字段名称来解决它。除非它是自动实现的属性,在这种情况下你不能。

老实说,如果您想要灵活地改变类型,BinaryFormatter 是一个糟糕的选择。基于契约的序列化器在这里更加灵活。例如,XmlSerializer 和 DataContractSerializer 允许您通过属性控制名称。

如果你想要二进制文件,我会选择 protobuf-net (也许是因为我写了它......) - 这里没有名称 - 只是数字标识符。但 protobuf 格式是 Google 专门设计的,旨在允许 API 的轻松升级。

当然,您也可以将 DTO 视为永久合同;在这种情况下,请考虑使用 v1 DTO、v2 DTO 等。这不是我自己倾向于做的方式,但绝对是一种选择。

I you have used BinaryFormatter, then note that this is a field serializer, not a property serializer; you can hack around it by not changing the field names. Unless it is an auto implemented property, in which case you can't.

To be honest, BinaryFormatter is a poor choice if you want the flexibility to mutate the types. A contract-based serializer is much more flexible here. For example, XmlSerializer and DataContractSerializer allow ou to control the names via an attribute.

If you want binary, I would go with protobuf-net (perhaps because I wrote it...) - here there are no names - just numeric identifiers. But the protobuf format was designed by Google specifically to allow painless upgrade of APIs.

Of course, you might also look at a DTO as permenant contract; in which case consider having a v1 DTO, a v2 DTO etc. Not the way I tend to do it myself, but definitely an option.

梦年海沫深 2024-10-18 23:00:17

无论您使用哪种序列化机制,重命名属性都是一项重大更改。二进制序列化的问题是您无法轻松地将文件升级为新格式,而使用文本格式序列化则更容易完成任务。

No matter what serialization mechanism you use, renaming a property is a breaking change. The problem with binary serialization is that you cannot easily upgrade the files to the new format which would be an easier task with text format serialization.

别再吹冷风 2024-10-18 23:00:17

您需要编写一个程序,将

  • 数据反序列化为旧版本的实体,
  • 将旧版本的实体转换为新版本的实体,
  • 将新版本的实体序列回文件。

如果您已将其序列化为 XML,您可能可以编写 XSLT 来直接进行所需的更改。

You'll need to write a program that

  • deserializes the data into the old version of the entity
  • transforms the old version of the entity into the new version of the entity
  • serializes the new version of the entity back to the file.

If you've serialized it to XML, you could probably write an XSLT to make the required changes directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文