如何将中间层的对象连接到由DataSet组成的数据层? [英] How to wire a middle tier of Objects to a data tier consisting of a DataSet?

查看:143
本文介绍了如何将中间层的对象连接到由DataSet组成的数据层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几个相关对象的中间层和一个数据层,它使用DataSet和几个DataTable和关系。



我想调用一个Save方法我的对象之一(父对象),并将其私有变量数据转换为DataRow并添加到DataTable。一些私有变量数据实际上是其他对象(子对象),每个对象都需要自己的Save方法调用,并且它们自己的变量数据保持不变。



花边这一起?要在ParentObject中实例化哪些部分DataSet以及需要传递给ChildObjects的内容,以便他们可以将自己添加到数据集中?



此外,我如何联线两个关系在一起为2个表?



我看到的Order OrderDetail关系的示例创建了OrderRow和OrderDetailRow,然后调用OrderDetailRow.SetParentRow(OrderDetail)



我不认为这对我有用,因为我的Order和OrderDetail(使用他们的例子命名)是在不同的类,这些例子都发生在一个大的召唤方法。 p>

谢谢,
Keith

解决方案

我不会开始数据集是好还是坏的另一个争论如果您继续使用它们,请考虑以下事项:




  • 您需要保留原始数据集并进行更新,才能获得正确的插入和更新。

  • 您希望您的父母了解自己的孩子,但不要以其他方式。删除ParentTable。

  • 订单及其OrderDetails是一个汇总(来自域驱动设计),应该被视为一个整体。调用order.Save()应该保存所有内容。



那就是理论。我们该怎么做呢一种方法是创建以下工件:




  • 订单

  • OrderDetail

  • OrderRepository

  • OrderMap



OrderMap是您管理订单数据集关系。在内部,它可以使用Hashtable或Dictionary。



OrderRepository是您从中获取订单的地方。存储库将从某处获取具有所有关系的数据集,使用其所有OrderDetails构建Order,并将Order / Dataset关系存储在OrderMap中。



OrderMap必须只要命令活着就活着。
订单包含所有订单详细信息。



将订单传递到存储库并保存。存储库将从地图获取数据集,从订单更新订单表,并迭代所有订单详细信息以更新OrderDetail表。



检索并保存:

  var order = repository.GetOrder(id); 
repository.Save(order);

Inside OrderRepository.GetOrder():

  var ds = db.GetOrderAndDetailsBy(id); 
var order = new Order();
UpdateOrder(ds,order);
UpdateOrderDetails(ds,order); //创建和更新OrderDetail,将其添加到订单。
map.Register(ds,order);

Inside OrderRepository.Save():

  var ds = map.GetDataSetFor(order); 
UpdateFromOrder(ds,order);
foreach(var detail in order.Details)UpdateFromDetail(ds.OrderDetail,detail);

一些最后的笔记:




  • 您可以将地图实现为singelton。

  • 让地图使用弱引用。那么任何订单应该是
    垃圾回收,当它应该,
    和内存将被释放。

  • 您需要一些方法才能将OrderDetail与其表格相关联

  • 如果您有最轻微的升级到.NET 3.5的可能性,那么。 Linq to Sql或Linq to Entity将会删除你的一些痛苦。

  • 所有这一切都是由稀薄的空气造成的。我希望这不太准确。


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

解决方案

I will not start another debate whether datasets are good or evil. If you continue to use them, here are something to consider:

  • You need to keep the original dataset and update that, in order to get correct inserts and updates.
  • You want your parents to know their children, but not the other way. Banish the ParentTable.
  • An Order and its OrderDetails is an aggregate (from Domain Driven Design) and should be considered as a whole. A call to order.Save() should save everything.

Well, that's the theory. How can we do that? One way is to create the following artifacts:

  • Order
  • OrderDetail
  • OrderRepository
  • OrderMap

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:

var order = repository.GetOrder(id);
repository.Save(order);

Inside OrderRepository.GetOrder():

var ds = db.GetOrderAndDetailsBy(id);
var order = new Order();
UpdateOrder(ds, order);
UpdateOrderDetails(ds, order); // creates and updates OrderDetail, add it to order.
map.Register(ds, order);

Inside OrderRepository.Save():

var ds = map.GetDataSetFor(order);
UpdateFromOrder(ds, order);
foreach(var detail in order.Details) UpdateFromDetail(ds.OrderDetail, detail);

Some final notes:

  • You can implement the map as a singelton.
  • Let the map use weak references. Then any order should be garbage-collected when it should, and memory will be freed.
  • You need some way to associate an OrderDetail with its table-row
  • If you have the slightest possibility to upgrade to .NET 3.5, do it. Linq to Sql or Linq to Entity will remove some of your pain.
  • All of this is created out of thin air. I hope it's not too inaccurate.

这篇关于如何将中间层的对象连接到由DataSet组成的数据层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆