Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controllers in MVVM, How to get info from viewmodel to it's controller?

Tags:

mvvm

We are building an app using the MVVM pattern, we have controllers that wire up all the views and viewmodels using DI. All examples of MVVM I've seen are really simplistic and have 1 view. How do/should viewmodels talk back to the controller? The controller knows about the models and views, should the viewmodel send events back to the controller? Where should a save happen? Model? Controller?

like image 351
nportelli Avatar asked Sep 24 '09 14:09

nportelli


People also ask

How do you communicate between view and ViewModel?

What is a proper way to communicate between the ViewModel and the View , Google architecture components give use LiveData in which the view subscribes to the changes and update itself accordingly, but this communication not suitable for single events, for example show message, show progress, hide progress etc.

Is a ViewModel the same as a controller?

While the ViewModel is an optional pattern the Controller is a must, if you are going the MVC way. The ViewModel encapsulates presentation logic and state, The Controller orchestrates all the Application Flow.

Is there a controller in MVVM?

Both MVC and MVVM architectures contain a Controller piece. In fact, every software project has a Controller piece, since every software project manipulates data.


1 Answers

Could your ViewModel not take a dependency on an IController or some other interface, so they can talk back to it? I try to keep as much application logic out of the ViewModel as possible, as these classes can easily become bloated.

 MyViewModel(IController controller)
 {
     this.controller = controller;
 }

 void Save()
 {
     this.controller.Save();
 }

I do agree that the MVVM frameworks tend to be too simplistic with their samples. In particular, moving between views/screens in your application is something I would like to see more examples of. I create an IViewManager interface, to allow my ViewModels to request that we move to another view.

like image 54
Mark Heath Avatar answered Oct 07 '22 00:10

Mark Heath