需要有关 C# 中 XML 序列化的多对一关系的良好设计方面的帮助
假设我有一些数据,其形式为
Customer1
Name
Address
Order1
ID
Products
Product1
ID
Product2
ID
Customer2
...
使用以下类来表示它,
class Customer
{
public String name { get; set; }
public String Address {get; set;}
public List<OrderInfo> Orders { get; set; }
}
class Order
{
public int ID { get; set; }
public List<Product> Products { get; set; }
}
class Product
{
public int ID { get; set; }
}
我想使用 XML 序列化来读取/写入数据,而不是使用实体框架和 SQL。使用实体框架,我能够定义多对一关系,以便每个产品只会在数据库中存储一次,并且每个订单只会引用它。根据我的理解,将上述代码与 XML 序列化结合使用时,每个产品都会存储许多副本(在内存和 XML 文件中)。
为了解决这个问题,我可以只存储产品 ID 列表,而不是产品本身,然后将单独的“产品”集合序列化到另一个 XML 文件 - 但这对我来说并不是一个干净的解决方案。
另一个想法是拥有一个包含客户和产品列表的 RepositoryClass。然后,客户只需要存储产品 ID,当您从存储库请求“客户”时,它将处理查找和构建上述对象,这样我的其余代码就不必担心数据的存储方式。这听起来是一个更好的解决方案吗?你会怎么做?
该解决方案的另一个要求是,我希望尽可能轻松地将解决方案连接到 GUI 以直接编辑属性(这实际上就是为什么我希望有一种方法将其全部存储在一个 XML 文件中,我可以将其绑定到WPF 窗口)。
预先感谢您的想法和建议!
Assuming I have some data in the form of
Customer1
Name
Address
Order1
ID
Products
Product1
ID
Product2
ID
Customer2
...
Using the following class to represent it
class Customer
{
public String name { get; set; }
public String Address {get; set;}
public List<OrderInfo> Orders { get; set; }
}
class Order
{
public int ID { get; set; }
public List<Product> Products { get; set; }
}
class Product
{
public int ID { get; set; }
}
I'd like to use XML serialization to read/write the data instead of using the entities framework and SQL. With the entities framework I'd be able to define a many to one relationship so that each product would only be stored once in the database and each order would just reference it. Using the above code with XML serialization it's my understanding that there would be many copies of each product stored (both in memory and the XML file).
To fix that I could just store a list of product IDs instead of to products themselves and then just serialize a separate collection of 'products' to another XML file - but that doesn't strike me as a clean solution.
Another idea would be to have a RepositoryClass which contained a list of customers and products. The customers would then only need to store the productIDs and when you requested a 'customer' from the repo it would handle the looking up and building the above objects so the rest of my code wouldn't have to worry about how the data was stored. Does that sound like a better solution? How would you do it?
The other requirement for the solution it that I want it as easy as possible to hook up the solution to GUI to edit the properties directly (that's actually why I wish there a way to store it all in one XML file that I could just bind to a WPF window).
Thanks in advance for your thoughts and advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内存中不会存储任何副本,因为您可以实例化特定产品一次并从此以后引用该产品。
xml 的情况稍微复杂一些。 xml 规范确实对此有一个解决方案,但我不确定 .NET 框架对此的支持程度如何。您可以拥有这样的 xml 文档:
验证 XML 解析器将验证
id
在文档中是唯一的,并且每个ref
属性实际上引用一个id
(如果您指定需要此信息的 DTD 或 XML 架构)。当然,您仍然需要构建一些解析此结构的东西,但您不再有重复的 xml 数据。
There would be no copies stored in memory because you could instantiate a specific product once and refer to that product from then on.
The xml case is a little more complicated. The xml specification does have a solution for this, but I'm not sure how well the .NET framework supports this. You could have an xml document like this:
A validating XML parser will validate that
id
's are unique within the document and that eachref
attribute actually refers to anid
if you specify a DTD or XML schema that requires this.Of course you'd still have to build something that parses this structure but you no longer have duplicate xml data.
.NET 中的 DataContractDeserializer 有一个名为preserveObjectReferences 的构造函数参数。如果此参数设置为 true(默认为 false),则保留对象的标识。有关详细信息:http://msdn.microsoft.com/en-us/library /ms731073.aspx
The DataContractDeserializer in .NET has a constructor argument called preserveObjectReferences. If this argument is set to true (default is false) the identity of the objects is preserved. For more information: http://msdn.microsoft.com/en-us/library/ms731073.aspx