简体   繁体   English

存储/序列化对象列表

[英]Storing/serializing list of objects

I oftem run into this case of what is the correct way to store a list of objects as a property in a class and how to properly serialize them into XML. 我碰到这种情况,这是将对象列表作为属性存储在类中的正确方法是什么,以及如何正确地将它们序列化为XML。

For instance, we have a TabGroup class which will contain zero or multiple Tabs. 例如,我们有一个TabGroup类,它将包含零个或多个Tab。

Is it better to a have a list of Tabs property or a list of references to Tabs? 拥有Tabs属性列表或Tabs引用列表是否更好? Provided that Tabs are identified by slugs which are unique. 假设标签由唯一的块标识。

List<Tab>

List<string>

In the end it comes down to 最后归结为

  1. Serializing only the whole TabGroup graph (which will contain all its Tabs and their content) 仅序列化整个TabGroup图(将包含其所有Tabs及其内容)
  2. Serializing Tabgroups and Tabs indenpendently and maintaing them separate and referenced through list of slugs in the serialized Tabgroup graph. 独立地序列化选项卡组和选项卡,并使其分开并通过序列化的选项卡组图中的段列表进行引用。

Most notable pro of 1: 最引人注目的职业玩家1:

  • Tabgroup in its entirety is persisted in one serialized file, keeping the datastore structure simple. Tabgroup整体上保存在一个序列化文件中,从而使数据存储结构保持简单。

Most notable con of 1: 最值得注意的缺点1:

  • each time an update is made to one of the contained Tabs, Tabgroup must be updated (reserialized) too. 每次对其中一个选项卡进行更新时,也必须更新(重新序列化)选项卡组。

Most notable pro of 2: 最著名的职业玩家2:

  • updating tabs does not require reserialization of Tabgroup (at least when nothing was added or removed) since the references stay the same; 更新标签页不需要重新序列化标签页组(至少在未添加或删除任何内容时),因为引用保持不变; so only the updated Tab has to be serialized again. 因此只有更新的Tab才需要再次序列化。

Most notable con of 2 (this is the main reason why I am writing this) 最引人注目的2(这是我写这篇文章的主要原因)

  • individual Tab files can be deleted in filestore but list of references remains the same, so errors/exceptions occur when viewing/rendering Tabgroups; 可以在文件存储中删除单个选项卡文件,但是引用列表保持不变,因此在查看/呈现选项卡组时会发生错误/异常; complex logic would have to be implemented to render out something like "Tab was removed from datastore in unsupported way, remove it from the Tabgroup also?" 必须实现复杂的逻辑才能呈现出类似的信息,例如“以不受支持的方式从数据存储区中删除了标签,还从选项卡组中删除了标签吗?”

What do you suggest to tackle this problem? 您对解决这个问题有何建议? I will accept the answer that will cover a wide array of implications. 我将接受涵盖广泛含义的答案。 Please note that we are talking only about XML persistence here, obviously in SQL we have little room to experiment since Tabgroups and Tabs would normally be in separate tables anyway (with a one-many relationship between them). 请注意,这里我们仅谈论XML持久性,显然在SQL中,我们没有什么实验的余地,因为Tabgroups和Tabs通常无论如何都位于单独的表中(它们之间是一对多的关系)。

Unless you have some very compelling reason why complicating the data store is a good idea, you should typically go with keeping it simple. 除非您有非常令人信服的理由来简化数据存储是个好主意,否则通常应保持简单。 Secondly, having read the entire post twice, I do not really understand what your question is. 其次,阅读了整篇文章两次,我真的不明白您的问题是什么。

I'm not quite sure what your problem is, but if you are asking whether your design should return a List<Tab> or List<string> where each string represents a link to a tab, then I would argue for List<Tab> . 我不太确定您的问题是什么,但是如果您要问设计是返回List<Tab>还是List<string> ,其中每个字符串代表指向选项卡的链接,那么我会建议使用List<Tab> You can lazy load the entire structure except for the ID or whatever you were using for a link if loading is an issue. 您可以延迟加载整个结构,但要避免加载ID或链接所用的任何内容(ID除外)。 Generally it just makes things easier to get what you were looking for directly out of an object instead of having to get a list of links and load all of the links individually. 通常,它使从对象中直接获取所需内容变得更加容易,而不必获取链接列表并单独加载所有链接。

Without more information specific to the actual problem, I doubt anyone would be able to help you more than that other than to give some long winded pros/cons based on assumed circumstances. 如果没有更多有关实际问题的信息,我怀疑除了根据假定的情况给出一些长远的优点/缺点之外,任何人都可以为您提供更多帮助。

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

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