vendredi 13 octobre 2017

3 layer architecture in a Winform App

I'm trying to implement this architecture for the first time in a Winform. So I have a simple but very important question for me. Take a simple exemple. I want the form to retrieve a user list and to allow a modification of the phone number. I have this for the first step (simplified and I normally use interfaces)

public Form1()
{
    InitializeComponent();
    UserService _userService = new UserService();
    listBoxUsers.DataSource = _userService.GetAllUsers();
}
class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Phone { get; set; }
}
class UserService
{
    UserRepository _userRepository=new UserRepository();
    public Dictionary<int, string> GetAllUsers()
    {
        DataTable dtbl= _userRepository.AllUsers();
        //Some code here
        return dict;
    }
}
class UserRepository
{
    public DataTable AllUsers()
    {
        //Sql query
        return dtbl; 
    }
}

Now by selecting a user in the lisbox, I'm able to display some information as the Phone number. When I'm changing the phone number, I need a method called UpdatePhoneNumber to update the sql database. But, where to place it? User or UserService (I don't talk about the sql query, just the logic) And after that, how to access (here or somewhere else in the app) to this user property to display it in the form? Directly with _user.Id (User must be instanciated in the form) or implement a _userService.Id which retrieve User.ID (in this case Form knows only UserService class). Many thanks for your precious help

Aucun commentaire:

Enregistrer un commentaire