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:
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:
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