0

I'm not sure if softwareengineering exchange is right for the question.

I need to populate view model with some values I query from the database.

I do it like:

var db = new NPoco.Database(connection);
// Get some values from database
//...
var viewModel = new MyViewModel(db) 
{
 //assign values from database
};

// show view and assign view model

I get all POCOs I need and I assign them to my view model before passing it to the view. And I pass database instance to my view model.

And it got me thinking. Should the creator of view model be responsible for populating it, or viewmodel could populate it in the constructor?

Am I doing it right currently?

  • Can ViewModel exist without this data? Would it make sense to create this ViewModel with the data being set to "default"? – Euphoric Apr 3 '18 at 12:02
  • I don't know. I think not. – Konrad Apr 3 '18 at 12:58
  • I need the data. – Konrad Apr 3 '18 at 12:59
  • 1
    Your ViewModel can pull the data from the db instance. That's kind of the whole point of a View Model: to coordination communication between the View and the Model. – Robert Harvey Apr 3 '18 at 15:40
  • @RobertHarvey what if I have some ids that are only known outside view model and I want to fetch those ids. – Konrad Apr 3 '18 at 18:48

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.