MVC vs MVVM – Should MVVM Models Have Behavior?

modelmvcmvvmsetters

I am trying to wrap my head around MVVM and "models should not have behaviour".

If my getter depends on an environment variable,

  • should it be in the model or viewmodel?
  • Does it make a difference if that getter is specific to one viewmodel vs many?

Also, I have complex setters that might have side-effects and logic. Does that automatically mean they belong into a viewmodel?

Sources on this:

Based on these I'm looking for some guidance for how to separate logic between model and viewmodel.

Best Answer

Your model classes should be simple and only store the data you wish to model for your application. You should avoid having any complicated methods or business logic in your model classes. I usually have constructors and basic properties (i.e. public getters, private setters) in my model classes and that tends to suffice.

Your view model classes should contain business logic and anything else required to facilitate presenting the data in your model classes in the corresponding views. View model classes can also access other required dependencies, such as environment variables. If you find that several view model classes require access to environment variables, you may want to introduce a separate class that accesses environment variables and then include this class as a dependency in your view model classes, preferably using dependency injection.

Related Topic