Is it possible to reuse VM MVVM?

I just need a simple explanation:

I have an example application with a ball model and two views (say, one shows the ball and allows you to resize it, if you use the mouse to change the color with a click, the second one has a control with two child controls (text box size and color selection) )

Does MVVM say that I have two virtual machines here, one for each particular view, or am I allowed to reuse the virtual machine without breaking the template?

As a follow-up question, if I need to have two virtual machines, is it legal, according to the pattern, to have one as a derived type of the other, or both as derived types of a base class or based on compound classes on common parts? Basically, you need to do two types, but improve reuse?

My example is contrived; I tried to emphasize that both representations affect the same two properties of the model. Maximum size, colors available, etc. The same between both representations.

thanks

+4
source share
5 answers

Use the same encoding methods in MVVM that you use elsewhere. In particular, remain DRY. Therefore, if you can use the same viewing model, do it. I would say that reusing the view model is extremely rare. If you later need to reorganize two different view models, but they can be obtained from a common base class to reuse common parts, etc., then do it.

+2
source

Yes, VM is very presentation specific, and the likelihood of reuse is very subtle.

In fact, I would go further if you can reuse a virtual machine, then I would say that this is most likely a duplication of the view.

One exception that I can think of is a child view in a nested structure of a hierarchical object model.

+2
source

In this situation, it is advisable to exchange ViewModels. ViewModel is the glue that allows a view to create a model view. If you have two views that represent the same aspects of the Model, it is logical to use the same ViewModel.

If over time you find that the representations are different representations of the model, you need to get back to design.

+2
source

Without answering your question directly, check out this insightful post by Josh Smith regarding reusing ViewModels and the "friction" caused by sharing: http://groups.google.com/group/wpf-disciples/msg/c29b3935ec9d3c4e

It basically offers MVVM enhancement (calling it MMVVVM, phew!) - an optional MV for ModelView. As others have noted, ViewModel is specific for viewing and reuse, which is unlikely. Rather, create a wrapper around the Model (ModelView) that can be reused with any ViewModel.

+2
source

We had a case some time ago when we needed to duplicate a view in several places, but the main types of views and viewmodels were different. In this case, we created a general representation model and passed it to the base types as parameters when building viewmodels and were able to avoid duplication of code in this way. As others say, this situation is rare, so in most cases you will create new view models (although all view models are likely to inherit from a common base class, for example, to notify about properties.)

We also tend to create POCOs from our database objects so that our database context does not change until we really want to apply the changes (i.e. the MMVVVM (!) Pattern described by Chris)

+2
source

All Articles