This is a discussion on Objects and Data Access Patterns Question within the Software Patterns forums, part of the Testing category; I know very little about the different patterns, but have a question whether particular patterns exist for two issues I ...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Objects and Data Access Patterns Question
I know very little about the different patterns, but have a question
whether particular patterns exist for two issues I have. I assume they do since my problem I would expect are quite common. Problem 1 is this: Say I have a user class (User) and subscription class (Subscription). The data for both the User and Subscription classes is kept in a database in two tables, Users and Subscriptions. Users are identified by ID (both a property of the Object and a field in the DB). Subscriptions are identified by ID (both a property of the Object and a field in the DB). A user can have many subscriptions, so the User class has a property called Subscriptions which is an array. The first question is regarding whether when I instantiate a User based on ID I should also populate the User.Subscriptions array. On the one hand, it is part of the definition of User. One the other hand, it requires another call to the DB to get all the subscriptions based on User.ID. Would it be better to add a GetSubscriptions method to populate the array on demand. This also has implications when calling a User.Update method, for example. In the Update method do I update the data in the Users table and also the Subscriptions? Or should I have a seperate method UpdateSubscriptions to update the Subscriptions table. I know it's not very complicated and possibly a matter of taste, but are there patters which answer these questions? The second question is regarding whether to store Subscription.IDs in the User.Subscriptions array or actual Subscription objects. If I store ID's I only have to go to the DB once when I get the subscriptions for the User. If I store Subscription objects themselves in User.Subscriptions, then when I get the subscriptions for the User, I also have to go to the DB for each and every Subscription object being instantiated. This seems like a lot of work when often I'll only need the IDs. On the other hand it seems much cleaner and OO like to store actual Subscription Objects. Thanks for any help. |