In my database I have 2 tables:
id | name | city | street
-------------------------------
1 | John | London | Street1
2 | Will | London | Street1
3 | Adam | LA | Street1
id | uid | phone
------------------
1 | 1| 12345
2 | 1| 23456
3 | 2| 16505
4 | 3| 65909
5 | 3| 68902
6 | 3| 15905
I need to select that data and store into List (or Dictionary as user Id is unique)
My uses class looks like so:
public class User
{
//lock
private static readonly object userLock = new object();
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Street { get; set; }
public List<string> Phones { get; private set; }
public User()
{
Phones = new List<string>();
}
public void AddPhone(string phone)
{
lock (userLock)
{
Phones.Add(phone);
}
}
}
Idea is to load those users info into application, add/remove/modify phones and save those changed/removed/added.
I found idea for solution here: https://stackoverflow.com/a/11687743/965722, but I imagine this as doing one query to get users list and then for each user I need separate query to get his phones.
How should I do my loading?
Can I do it with only one query?
How should I populate my result collection?
Should I use List or Dictionary? Then I could remove Id from User class and have it as key inside dictionary.
I’m using .NET 3.5 and would like to avoid Entity Framework or ORM’s.
Make a class Phone and change the User property accordingly:
Make a PhoneRepository:
And a UserRepository:
You can greatly expand and improve on this pattern with, for example, custom filter parameters (instead of different functions for the various filters) and property setters that make sure the user and user.Phones keep referring to the same userID, but that is sort of beyond the scope of your question.