简体   繁体   English

XML-Serialization和需要的公共属性

[英]XML-Serialization and the need public properties

in my Silverlight 4 application, I save my object via DataContractSerializer XML-Serialization - which is quite easy: 在我的Silverlight 4应用程序中,我通过DataContractSerializer XML-Serialization保存我的对象 - 这非常简单:

public byte[] SerializeModel(ServiceModel model)
{
    System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(ServiceModel));
    System.IO.MemoryStream ms = new System.IO.MemoryStream();

    serializer.WriteObject(ms, model);

    byte[] bytes = ms.ToArray();
    ms.Close();

    return bytes;
}

... and loading them easily via: ...并通过以下方式轻松加载:

public ServiceModel DeserializeModel(string stream)
{
    System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(ServiceModel));
    System.Xml.XmlReader reader = System.Xml.XmlReader.Create(new System.IO.StringReader(stream));

    object result = serializer.ReadObject(reader);

    return (ServiceModel)result;
}

(Error Handling ommited) (错误处理ommited)

The problem for me is, that I need to make the data to be saved (or better: to be loaded) public properties with getter and setter. 对我来说问题是,我需要使用getter和setter来保存数据(或者更好:加载)公共属性。 That leads to a loss of control over data integrity. 这导致失去对数据完整性的控制。 Ie I have a collection of objects and I want to control what objects are to be added or removed. 即我有一组对象,我想控制要添加或删除的对象。 I would need to subclass or re-implement the collection, changing Add and Remove methods and whatever else method I'd need to control. 我需要子类化或重新实现集合,更改添加和删除方法以及我需要控制的任何其他方法。 But making it private keeps me from using the DataContractSerializer. 但是将其设为私有使我无法使用DataContractSerializer。

Any suggestions how to keep it simple but keeps the control over the objects within the class? 有什么建议如何保持简单,但保持对类内对象的控制?

Thanks in advance, 提前致谢,
Frank 坦率

Typically what I do in these situations is to keep my serialization objects as POCOs (just gets/sets) and then once i deserialize them I will do whatever additional checks i need to make and throw/error handle as appropriate. 通常我在这些情况下做的是将序列化对象保持为POCO(只是获取/设置),然后一旦我反序列化它们,我将做任何我需要做的额外检查和抛出/错误句柄。

So this means I have my "interface" for serialization which is just the POCO, and then once I deserialize it I do any consistency checks and load/adapt into my entities. 所以这意味着我有序列化的“接口”,它只是POCO,然后一旦我反序列化它,我做任何一致性检查并加载/适应我的实体。 Where I work we do this with our web services, where we have our domain entities and our interface classes. 在我工作的地方,我们使用我们的网络服务,我们拥有域实体和接口类。 Once we get the request we adapt it to our business entities and then pass it into the business layer and reverse the process for results. 一旦我们收到请求,我们就会将其调整到我们的业务实体,然后将其传递到业务层并反转结果流程。

I kind of see serialization as crossing a layer and thus all input should be validated and then adapted to keep coupling at a minimum. 我认为序列化是跨越一层,因此所有输入都应该被验证,然后适应以保持耦合最小化。

But that's just my $0.02 但那只是我0.02美元

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM