0

That is, let's say I have a model that's used in several views. Should I create a single view model to represent it across all those views, or should I create a separate view model for each view that uses the model?

ekolis
  • 521

2 Answers2

-1

With classic OOP find the things that are common across all these views and create a base model that these use and extend.

But composition is a better deign pattern in my opinion. Create modules (ViewModel and view) then create combinations of these to create your variations.

Anders
  • 671
-1

If you created separate view models for each view, would they end up containing exactly the same code? In that case use a single class. Would they contain different code? Then use separate classes. More generally, if you find yourself copy-pasting the same code into multiple files, you are probably doing something wrong.

A view model should not be coupled to the view, so there is nothing wrong with having multiple views using the same view model.

JacquesB
  • 59,334