IList<Item> Collection Class accessing database

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-03-23T14:19:38Z Indexed on 2010/03/23 14:23 UTC
Read the original article Hit count: 332

Hi,

I have a database with Users. Users have Items.

These Items can change actively. How do you access the items in a collection type format? For the user, I fill all the user properties at the time of instantiation. If I load the user's items at the time of the instantiation, and the items change, they will have old data.

I was thinking, maybe I need an ItemCollection class and have that a field/property apart of the user class, that way to traverse all the user's items I could use a foreach loop.

So, my question is, what is the best practice/best way of accessing the items from a database using some sort of collection? On accessing the particular Item, it needs to get the latest database information, and when the user does do a foreach loop, the latest item information must be available.

I.e. What I'm trying to do

Console.WriteLine(User.Items[3].ID); returns 5.
//this updates the item information and saves it to the database.
User.Items[3].ID = 13; 

//Add a new item to the database.
User.Items.Add(new Item { id = 17});

foreach (Item item in User.Items) {
    //this would traverse all items in the database.
    //not some cached copy at the time of instantiation of the user.
}

© Stack Overflow or respective owner

Related posts about collections

Related posts about database