So I have a view model (asp.net mcv 3 app) that when instantiated with a particular parameter, populates it's properties via a couple of calls to a web service in it's constructor.
public class QuizPreviewViewModel
{
private IQuizServiceWrapper _service;
public string QuizName { get; set; }
public List<QuizQuestion> QuizQuestions { get; set; }
public List<AnswerChoice> AnswerChoices { get; set; }
public int QuizId { get; set; }
public QuizPreviewViewModel()
{
}
public QuizPreviewViewModel(int quizId )
{
_service = new QuizServiceWrapper();
var quiz = _service.GetQuizHeader(quizId);
var questions = _service.GetQuizQuestionsByQuiz(quizId).OrderBy(x => x.Order).ToList();
QuizId = quizId;
QuizName = quiz.QuizName;
QuizQuestions = questions;
}
}
I know I could just as easily perform the population in the controller, or possibly extract the code to populate the properties into a separate method or helper class.
I have read in various books/articles that both view models and controllers should be kept as clean as possible. So I'm wondering what you all think of populating the view model in it's constructor. Is this a decent practice, or a horrible idea? Where is the best place to populate the view model?