如何将对象的中间层连接到由数据集组成的数据层?
我有一个包含多个相关对象的中间层和一个使用具有多个数据表和关系的数据集的数据层。
我想在我的一个对象(父对象)上调用 Save 方法,并将其私有变量数据转换为 DataRow 并添加到 DataTable 中。 一些私有变量数据实际上是其他对象(子对象),每个对象都需要调用自己的 Save 方法并持久保存自己的变量数据。
我如何将它们“结合”在一起? DataSet 的哪些部分应该在 ParentObject 中实例化,哪些部分需要传递给 ChildObjects,以便它们可以将自己添加到数据集中?
另外,如何将两个表的关系连接在一起?
我看到的 Order OrderDetail 关系的示例创建了 OrderRow 和 OrderDetailRow,然后调用 OrderDetailRow.SetParentRow(OrderDetail)
我认为这对我不起作用,因为我的 Order 和 OrderDetail (使用它们的示例命名)位于不同的类中,并且例如,这一切都发生在大喇叭方法中。
谢谢你, 基思
I have a middle tier containing several related objects and a data tier that is using a DataSet with several DataTables and relationships.
I want to call a Save method on one of my objects (a parent object) and have its private variable data transformed into a DataRow and added to a DataTable. Some of the private variable data are actually other objects (child object) that each need to have their own Save method called and their own variable data persisted.
How do I "lace" this together? What parts of a DataSet should be instantiated in the ParentObject and what needs to be passed to the ChildObjects so they can add themselves to the dataset?
Also, how do I wire the relationships together for 2 tables?
The examples I have seen for an Order OrderDetail relationship create the OrderRow and the OrderDetailRow then call OrderDetailRow.SetParentRow(OrderDetail)
I do not think this will work for me since my Order and OrderDetail (using their examples naming) are in separate classes and the examples have it all happening in a Big Honking Method.
Thank you,
Keith
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会再开始争论数据集是好还是坏。 如果您继续使用它们,请考虑以下事项:
嗯,这就是理论。 我们怎样才能做到这一点? 一种方法是创建以下工件:
OrderMap 是您管理订单与数据集关系的位置。 在内部,它可以使用哈希表或字典。
OrderRepository 是您获取订单的地方。 存储库将从某处获取包含所有关系的数据集,构建订单及其所有订单详细信息,并将订单/数据集关系存储在 OrderMap 中。
只要 Order 处于活动状态,OrderMap 就必须保持活动状态。
订单包含所有订单详细信息。
将订单传递到存储库并让它保存。 存储库将从地图中获取数据集,根据订单更新订单表,并迭代所有订单详细信息以更新订单详细信息表。
检索并保存:
OrderRepository.GetOrder() 内部:
OrderRepository.Save() 内部:
最后一些注意事项:
在应该收集垃圾的时候,
并且内存将被释放。
I will not start another debate whether datasets are good or evil. If you continue to use them, here are something to consider:
Well, that's the theory. How can we do that? One way is to create the following artifacts:
The OrderMap is where you manage the Order to Dataset relationships. Internally, it could use a Hashtable or a Dictionary.
The OrderRepository is where you get your Orders from. The repository will get the dataset with all relations from somewhere, build the Order with all its OrderDetails, and store the Order/Dataset relationship in the OrderMap.
The OrderMap must be kept alive as long as the Order is alive.
The Order contains all OrderDetails.
Pass the order to the repository and let it save it. The repository will get the dataset from the map, update the Order-table from the order and iterate all order-details to update the OrderDetail-table.
Retrieve and save:
Inside OrderRepository.GetOrder():
Inside OrderRepository.Save():
Some final notes:
garbage-collected when it should,
and memory will be freed.
因此,我现在正在做的是将 DataSet 的引用和父级 DataRow 的引用传递到子对象的 Save 方法中。
这是一段小代码,展示了我正在做的事情的概念。
让我知道这看起来如何。 这是一个可用的模式还是我错过了一些关键的东西?
谢谢你,
基思
So, What I am doing right now is passing a reference to the DataSet and a reference to the DataRow of the parent into the Save method of the Child Object.
Here is a little code showing the concept of what I am doing.
Let me know how this looks. Is this a usable pattern or am I missing something critical?
Thank you,
Keith