将序列化对象迁移到新版本
我想将数据库中以前序列化的对象迁移到新架构。
我以前的对象。
Public interface MyReport
{
string Id { get; set;}
string Name { get; set;}
Dictionary<string, string> PropColl { get; set;}
}
但由于某些原因,我们不得不进行界面更改
Public interface IMarkme
{
}
Public interface MyReport<T> where T : Imarkme
{
string Id { get; set;}
string Name { get; set;}
T ExtendedProp { get; set;}
}
Public NewProp : Imarkme
{
/// some code here
}
所以如您所见,我的界面已被修改,我想将基于 MyReport 序列化的序列化对象迁移到 MyReport 有人可以向我提供一些建议,告诉我应该编写哪种实用程序,这可以帮助我实现将序列化对象迁移到新的修改后的接口版本。
谢谢, 股份公司
I would like to migrate me previously serialized objects in database to new schema.
My previous object.
Public interface MyReport
{
string Id { get; set;}
string Name { get; set;}
Dictionary<string, string> PropColl { get; set;}
}
But for some reasons we had to make interface changes
Public interface IMarkme
{
}
Public interface MyReport<T> where T : Imarkme
{
string Id { get; set;}
string Name { get; set;}
T ExtendedProp { get; set;}
}
Public NewProp : Imarkme
{
/// some code here
}
So as you can see my interface has been modified and I would like to migrate my serialized objects which were serialized based on MyReport to MyReport
Can someone provide me some input as what kind of utility I should aim to write which can help me achieve migrating my serialized object to new modified interface version.
Thanks,
AG
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近实际上做了类似的事情,我创建了一个简单的控制台应用程序,能够将一些序列化对象从一个版本转换为另一个版本。我只是使用了 dll 和反射的两个版本来读取和写入不同属性的值。也许你会发现这对你的灵感很有帮助;)
I have actually done something similar recently, where I have created a simple console application to be able to transform some serialized objects from one version to another. I have simply used both versions of dlls and reflection to read and write the values of different properties. Probably you'll find this helpful as an inspiration ;)
1) 编写一个实用程序来读取旧对象定义中的序列化对象。
2) 该实用程序以非序列化方式将对象写入数据库(即每个字段中都有一条数据,等等......)。
不要养成序列化对象并将对象存储在持久存储中的某个位置以供稍后检索的习惯。序列化不是为此而构建的。
您曾经遇到过过去 C 程序员的问题:他们会在内存中创建一个结构,并将该结构保存到文件中。然后结构体的成员会发生变化,并且他们会想知道如何读回它,因为数据的编码方式不同。
然后出现了数据库格式、INI 文件等,专门为了满足这种需求,因此以一种格式保存数据,然后能够无错误地读取它。
所以不要再犯过去的错误。创建序列化是为了促进短期二进制存储以及通过 TCP/IP 传输对象的能力。
最坏的情况是,将数据存储为 XML,而不是序列化的二进制流。另外,据我所知,MS 不保证能够从另一个版本读取来自一个版本的 .NET 的序列化数据。尽可能将您的数据转换为可读的格式。
1) Write a utility that reads the serialized objects in the old object definition.
2) The utility writes your objects into the DB in a non-serialized manner (ie, with one piece of data in every field, etc...).
Don't get into the habit of serializing objects, and storing the somewhere in persistent storage for retrieval (much) later. Serialization was not built for that.
You have run into the problem of C programmers in the old days: they would create a struct in memory, save that struct into a file. Then the struct's members would change, and they woudl wonder how to read it back, since the data was encoded differently.
then along came database formats, INI files, and so on, specifically to address this need, so saving data in one format, and then being able to read it without error.
So don't repeat errors of the past. Serialization was created to facilitate short-term binary storage and the ability to, say, transmit an object over TCP/IP.
At worst, store your data as XML, not as serialized binary stream. Also, there is no assurance that I know about from MS that says that serialized data from one version of .NET will be able to be read from another. Convert your data to a legible format while you can.