版本化序列化文件 [英] versioning serialized files

查看:96
本文介绍了版本化序列化文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作的应用程序将一个文档(类型为IDocument)序列化到磁盘。从那里还有一个应用程序,我可以打开该文档(IDocument实现IPrintDocument)来查看。



我们假设我已经写了一个IDocument到磁盘,然后一周后,一个字段将被添加到IDocument对象中。写入文件的程序和打开它们的程序都使用这个新的版本IDocument进行更新。当我尝试打开以前的IDocument版本时,它会断开(我假设 - 没有机会检查,我在向前看)。是否有一种已知的模式来减轻这种问题?

解决方案

是 - 使用一个序列化机制,容忍版本控制



可以预见的是,我将建议使用 Google的协议缓冲区,其中有至少两个 可行 .NET实现。只要你小心,协议缓冲区都是向后兼容的,你可以使用旧的代码读取一个新的消息,反之亦然,旧的代码仍然可以保存它不明白的信息。 p>

另一个替代方案是XML,无论是使用.NET的内置XML序列化还是不使用。就我所知,内置序列化在版本控制方面并不是特别灵活。


I've got a working app that serializes a document (of type IDocument) to disk. From there there's another app that I've made that can open that document (IDocument implements IPrintDocument) for viewing.

Let's assume that I've written an IDocument to disk, and then a week later a field gets added to the IDocument object. Both the program that writes the files and the one that opens them are updated with this new 'version' of IDocument. It will then break (I assume - haven't had a chance to check, I'm looking ahead here) when trying to open the previous IDocument version. Is there a known pattern that alleviates this kind of problem?

解决方案

Yes - use a serialization mechanism which is tolerant to versioning.

Predictably enough, I'm going to suggest using Google's Protocol Buffers, for which there are at least two viable .NET implementations. So long as you're careful, Protocol Buffers are both backward and forward compatible - you can read a new message with old code and vice versa, and the old code will still be able to preserve the information it doesn't understand.

Another alternative is XML, whether using .NET's built-in XML serialization or not. The built-in serialization isn't particularly flexible in terms of versioning as far as I'm aware.

这篇关于版本化序列化文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆