Exposing Children From Persistent Storage
-
Hello all... If you had a parent object and wanted to expose its children from persistent storage, would you do it by exposing the children as if the parent was a List or just a few methods for getting the count and a range of child objects... or is there another method I might not be aware of. The children must be editable and retain their data in persistent storage. There could be thousands of children as well. Secondly, if you were to have a function that did something like "Take money from parent and give to child" would you put this function on the parent, child or as a static function?
-
Hello all... If you had a parent object and wanted to expose its children from persistent storage, would you do it by exposing the children as if the parent was a List or just a few methods for getting the count and a range of child objects... or is there another method I might not be aware of. The children must be editable and retain their data in persistent storage. There could be thousands of children as well. Secondly, if you were to have a function that did something like "Take money from parent and give to child" would you put this function on the parent, child or as a static function?
If you are using .NET 2, then I would look at generic collections to store your items:
public class Parent { List<Child> _children = new List<Child>(); public int Add(Child value) { _children.Add(value); } public void LoadAll() { // Read the data from the database into something like a datareader // And populate the child before adding it into the collection... } public void Save() { foreach (Child child in _children) { child.Save(); } } } public class Child { // Add the properties... // Some methods... public void Save() { // Save to database.. } // Fill the child record. public static Child Fill(IDataRecord row) { Child child = new Child(); child.Id = Convert.ToInt32(row["Id"].ToString()); // Id is a property of Child. return child; } }
This is just a sample of the type of approach you might take. It isn't intended to be directly coded from. -- modified at 4:37 Tuesday 5th December, 2006
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.