开发者

Can the VM of MVVM be reused?

开发者 https://www.devze.com 2023-01-30 12:57 出处:网络
I just need a simple clarification: I have an example application with a Model of a ball, and two views (lets say one shows the ball and lets you resize if with the mouse, change it\'s colour with a

I just need a simple clarification:

I have an example application with a Model of a ball, and two views (lets say one shows the ball and lets you resize if with the mouse, change it's colour with a click, the second has a control with two child controls (size textbox and colour picker)).

Does MVVM say I must have two VM here, one for each specific View, or am I allowed to reuse the VM without breaking the pattern?

As a follow up question, if I am required to have two VM's is it legitimate, according to the pattern to have one as a derived type of the other, or both as derived types of a base class, or composite classes based on common parts? Basically fulfilling the need for two types but improving r开发者_如何学Pythoneuse?

My example is contrived, I've tried to make the point that both Views affect the same two properties of the model. Max size, available colours, etc are the same between both views.

Thanks


Use the same coding practices in MVVM that you use anywhere else. In particular, stay DRY. So if you can use the same view model, then do so. I would say reuse of a view model is extremely rare though. If you later have to refactor into two separate view models but can derive from a common base class to reuse common parts, etc. then do so.


Yes, VM is very specific to a view and likelihood of re-using is very slim.

In fact I would go further, if you can re-use a VM then I would say it is most probably duplication of the view.

The only exception I can think of is the child view in a nested hierarchical object model structure.


It is entirely appropriate to share ViewModels in this situation. A ViewModel is the glue that allows a View to create a representation of a Model. If you have two Views that represent the same aspects of the Model, it is logical to use the same ViewModel.

If you find over time that the Views are different representations of the Model, then you need to revisit the design.


Although not directly answering your question, check out this insightful post from Josh Smith regarding the re-use of ViewModels and the "friction" caused from sharing: http://groups.google.com/group/wpf-disciples/msg/c29b3935ec9d3c4e

He is basically proposing an improvement to MVVM (calling it MMVVVM; phew!) - the extra MV is for ModelView. As the others have mentioned, the ViewModel is specific to a View and re-using that is not very likely. But rather, create a wrapper around the Model (the ModelView), which can be re-used with any ViewModel.


We had a case some time ago where we needed a view duplicated in several places, but the underlying types of the views and viewmodels were different. In that case, we created a generic viewmodel and passed in the underlying types as parameters when constructing the viewmodels and were able to avoid code duplication that way. As the others say, this situation is rare so most times you'll be creating new viewmodels (although all viewmodels will probably inherit from a common base class for e.g. property notification.)

We also tend to create POCOs from our database entities so that our database context isn't changed until we really want to apply changes (i.e. the MMVVVM(!) pattern that Chris described)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号