Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I still don't get MVVM!

Perhaps I have been doing Flex development with Frameworks like Cairngorm too long but I still don’t get MVVM. I am aware that Cairngorm is a framework and MVVM is a design pattern, but what I am comparing here is Cairngorms implementations of design patterns, mainly the model view controller and the command pattern. Don’t get me wrong, I think the idea of binding a view to a view model is great and the advantages in testability and designer-programmer workflow are great. But there are two things that bother me: one is programming all my actions with Commands, which by the way also bordered me from Cairngorm. Only in Cairngorm the way they implemented the command pattern gave you the benefit of having a centralized controller for all your commands, which you don’t seem to get with MVVM, unless I am missing something. And if I thought implementing the commands in Cairngorm was convoluted in MVVM is much worst, I mean having to create private classes that implement ICommand for everything I do seems like too much. And then you have the problem that not all controls implement commands so for example if you are using a ListBox, which I use a lot, you are out of luck; there are workarounds but all kind of convoluted.

The other thing that bothers me is the communication between View Models. In a standard Model View Controller you collect all you information on a centralized model which is observed by the views, but this doesn’t seem to be the case with MVVM, at least not in the examples that I have seen. So, for example, if you have a control with a list that you use to select an item which is then used as source for different views and consequent actions it is not clear to me how you notify everybody of the changes without a centralized model.

I am aware of MVVMFoundation and the work of Tom Ershamam about WPF Commands Everywhere. Called me old fashioned but I think that in order to really understand a pattern you have to build an application that uses it from scratch. Which is what I am doing, but the whole time I keep thinking I must be missing something essential because I don’t seem to be able to quiet this little voice in my head that keeps telling me there must be a better way.

like image 742
Julio Garcia Avatar asked Feb 24 '10 10:02

Julio Garcia


People also ask

Is MVVM an overkill?

MVVM creator John Gossman points out that implementing MVVM is "overkill" for simple UI operations, and that for larger applications, generalizing the ViewModel becomes more difficult. There is considerable memory consumption with data binding in very large applications.

Do I have to use MVVM?

The Windows Presentation Framework (WPF) takes full advantage of the Model-View-ViewModel (MVVM) pattern. Though it is possible to create WPF applications without using the MVVM pattern, a little investment in learning can make building WPF applications much simpler.

What does MVVM stand for?

Model-View-ViewModel (MVVM) is a software design pattern that is structured to separate program logic and user interface controls. MVVM is also known as model-view-binder and was created by Microsoft architects Ken Cooper and John Gossman.


1 Answers

Whatever the framework/architecture/pattern, you will always need something that responds to a button click, on a toolbar/menu or plain form. And you need something that says if the button/menu should be enabled. So the ICommand interface is nice for this. I agree with Petoj, you don't need a new class. I wrote a simple implementation that takes 1 or 2 delegates, one for the actual response to the click (the Execute method) and an optional one for the "enabled" status of the command. This way, the ViewModel is not cluttered.

But I agree that this is not a centralized repository of commands. But do you really want one ? I prefer to have commands specific to one part of an app to be in the corresponding view model, with appropriate events raised when the rest of the app should be notified.

For the listbox, I bind the SelectedItem property to a property on the ViewModel. With INotifyPropertyChanged, any part of your code can react to the change.

Communication between ViewModels is a good question. If you need different views on the same screen, you can have a "super" view model that contains the view model of each view. There are quite a few MVVM frameworks out there. I used parts of Mark Smith's MVVM helpers, which is quite lightweight and useful.

like image 191
Timores Avatar answered Oct 22 '22 13:10

Timores