I'm programming a web page (some kind of social network) and I created the model using EF code first. The User entity looks like this:

public class User
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Name { get; set; }
    public DateTime Birth { get; set; }
    public Sex Sex { get; set; }
    public string Email { get; set; }
    public string AboutMe { get; set; }
    public string FavoriteQuotes { get; set; }
    public DateTime Created { get; set; }
    public UserState State { get; set; }
    public Picture ProfilePicture { get; set; }

    ... 
    //Other properties, including 1:n ones.
}

There are several pages that show parts of the user info, but never everything. For example, I have a page that let's the user change his basic info, like name, email, etc, but not the profile picture.

I created a data repository IUserRepository that has a method like this one:

public interface IUserRepository
{
    User GetUserById(int id);
    User GetUserByEmail(string email);
    void SaveUser(User user);

    ...
    //Other methods.
}

And, for example, for that page that let's the user change his basic info, I have created a view model class like this one:

public class UserBasicInfoViewModel
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public string AboutMe { get; set; }

    ... 
    //Other properties.
}

But when in the action method I do:

var userInfo = new UserBasicInfoViewModel();
var user = userRepository.GetUserById(id);
userInfo.Id = user.Id;
userInfo.UserName = user.UserName;
userInfo.Password = user.Password;
userInfo.Email = user.Email;
userInfo.AboutMe = user.AboutMe;
...
return View(userInfo);

I recovered all the info about the user and I'm only using a little part of it. For me it would make more sense if the repository had a method that returns only the necessary info, something like:

UserBasicInfoViewModel GetBasicInfoById(int id);

Would be it correct if I do that? Is there a better way?

Thank you very much for your help.

Related posts

Recent Viewed