0

Assume a rather simplistic model:

class Person{   
   static List<Person> LivingPersons

   String Name
   List<Person> Dependents 
}

From this it can (hopefully) be seen that a single Person object can be both within the LivingPersons list and within the Dependents list of another Person object. Similarly, if there were a couple of ViewModels to represent this model:

class ViewModel_LivingPersons{
   static List<ViewModel_Person> LivingPersons
}

class ViewModel_Person{
   String Name
   List<ViewModel_Person> Dependents   
}

If I had a single Person object that resided in both the LivingPersons list and within the Dependents list of another Person, my gut feeling tells me that as it is the same model object being represented there should only ever be one instance of a ViewModel_Person to represent this model object. However, I do not know why, nor if this is best practice.

I'm not certain of the best practices here. I've done a bit of searching, but cannot find anything on this particular issue.

3
  • 2
    Focus less on what's considered "best practices" and more on what your specific needs are. The term "best practices" is useless without some criteria to specify what you want to achieve. Mar 2, 2021 at 19:15
  • 1
    Oh now that in itself is quite helpful and a bit of an eye-opener. I wasn't aware there would be some flexibility there.
    – Harry Will
    Mar 2, 2021 at 19:20
  • 1
    Software is fiction written for a computer. It can be whatever you want it to be, but most importantly you want it to tell the story of the computer doing what you need. So focus on the needs and how to tell them to the computer. Best practice emerges as you learn what goes down well with the computer in that situation. And Best Practice here, is not necessarily best practice there. Take a look at how many ways there are to sort a list.
    – Kain0_0
    Mar 3, 2021 at 7:20

1 Answer 1

0

There is no reason why the same model objects cannot or should not be used by multiple view models. View models contain your application logic and if your application sometimes only need to deal with concrete persons and their dependents and sometimes with all living people, yet both of these parts implement different logic/behavior, it would not be good to mix both up into a single view model as this violates the single-responsibility principle:

The single-responsibility principle (SRP) is a computer-programming principle that states that every class in a computer program should have responsibility over a single part of that program's functionality, which it should encapsulate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.