C# 复合模式和数据库访问
假设我有一个复合类PharmaProduct
(它代表制药公司的产品层次结构)和它的数据库表。我想到了两种将数据加载到 PharmaProduct 对象中的方法。
(1) 实例化对象时构造整个对象树。对树进行更改,并通过对树应用递归循环来保留这些更改(这实际上是 C# DataSet
的工作方式)。
(2)加载节点。 加载其他节点
PharmaProduct GetParent()
仅当调用或 时才
List<PharmaProduct> GetChldren()
(实际上执行直接数据库访问)。对节点进行更改。只保存该节点。
这种类型的表可能有一千个条目,具体取决于制药公司生产的产品类型。所以在这种情况下,我认为第一种方法会太笨拙(而且还消耗内存)。
如果出现任何复合模式问题,我实际上应该如何进行数据库访问?
Suppose I have a composite class PharmaProduct
(which represents the product hierarchy of a pharmaceutical company) and a database table for it. I have thought two ways to load the data into a PharmaProduct
object.
(1) Construct the entire object-tree when an object is instantiated. Make changes to the tree and persist those changes by applying recursive loop to the tree (This is actually the way C# DataSet
works).
(2) Load a node. Load other nodes only if
PharmaProduct GetParent()
or,
List<PharmaProduct> GetChldren()
are called (which actually do the direct database access). Make change to the node. Only save that node.
This type of tables may have a thousand entries, depending on how many types of items a pharmaceutical company manufactures. So in that case, the 1st approach will be too clumsy (and also memory consuming) I think.
How should I actually do the database access in case of any Composite Pattern problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下
代理
模式。使用它,您可以将 PharmaProductProxy 对象放入树中,这些对象具有与 PharmaProduct 相同的接口,但在访问它们时会延迟加载它们自己。Take a look at the
Proxy
pattern. Using it, you would putPharmaProductProxy
objects in the tree that have the same interface asPharmaProduct
, but lazy load themselves when they are accessed.