序列化和版本 [英] Serialization and versioning

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

问题描述

我需要一些数据序列串。然后该字符串被存储在DB中的一个特殊的柱SerializeData。

I need to serialize some data to string. The string is then stored in DB in a special column SerializeData.

我已经创建了用于序列特殊的类。

I have created special classes that are used for serialization.

[Serializable]
public class SerializableContingentOrder
{
    public Guid SomeGuidData { get; set; }
    public decimal SomeDecimalData { get; set; }
    public MyEnumerationType1 EnumData1 { get; set; }
}



连载:

Serialization:

protected override string Serialize()
{
    SerializableContingentOrder sco = new SerializableContingentOrder(this);

    MemoryStream ms = new MemoryStream();
    SoapFormatter sf = new SoapFormatter();
    sf.Serialize(ms, sco);
    string data = Convert.ToBase64String(ms.ToArray());
    ms.Close();
    return data;
}



反序列化:

Deserialization:

protected override bool Deserialize(string data)
{
    MemoryStream ms = new MemoryStream(Convert.FromBase64String(data).ToArray());
    SoapFormatter sf = new SoapFormatter();

    SerializableContingentOrder sco = sf.Deserialize(ms) as SerializableContingentOrder;
    ms.Close();
    return true;
}

现在我想有版本的支持。如果我改变 SerializableContingentOrder 类,会发生什么。我希望能够在将来添加新的领域。

Now I want to have versioning support. What happens if I change SerializableContingentOrder class. I want to be able to add new fields in the future.

我必须切换到DataContract序列化?请给我一小段?

Do I have to switch to DataContract serialization? Please give me short snippet?

推荐答案

我强烈倡导的的存储的BinaryFormatter SoapFormatter 数据库中的数据;它是:

I strongly advocate against storing BinaryFormatter or SoapFormatter data in the database; it is:



  • 未版本宽容

  • 没有独立于平台的

的BinaryFormatter 是.NET程序集之间的数据传输OK (在推送),但我会建议一个更可预测的序列化。 的DataContractSerializer 是一个选项(这是 JSON 的XmlSerializer ) ,但我的不会使用 NetDataContractSerializer 所有上述同样的原因。我的的会倾向于使用protobuf网,因为这是有效的二进制已知有效的格式,独立于平台和版本宽容!

BinaryFormatter is OK for data transfer between .NET assemblies (at a push), but I would recommend a more predictable serializer. DataContractSerializer is an option (as is JSON or XmlSerializer), but I would not use NetDataContractSerializer for all the same reasons above. I would be tempted to use protobuf-net, since that is efficient binary in a known efficient format, platform independent and version tolerant!

有关例如:

[DataContract]
public class SerializableContingentOrder
{
    [DataMember(Order=1)] public Guid SomeGuidData { get; set; }
    [DataMember(Order=2)] public decimal SomeDecimalData { get; set; }
    [DataMember(Order=3)] public MyEnumerationType1 EnumData1 { get; set; }
}



连载:

Serialization:

protected override string Serialize()
{
    SerializableContingentOrder sco = new SerializableContingentOrder(this);   
    using(MemoryStream ms = new MemoryStream()) {
        Serializer.Serialize(ms, sco);
        return Convert.ToBase64String(ms.ToArray());
    }
}



反序列化:

Deserialization:

protected override bool Deserialize(string data)
{
    using(MemoryStream ms = new MemoryStream(Convert.FromBase64String(data)) {
        SerializableContingentOrder sco =
               Serializer.Deserialize<SerializableContingentOrder>(ms)
    }
    return true;
}

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

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