Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing MVVMC and Dependency Injection

I have just read this article about MVVMC pattern. Now I have a question. Should Controller be injected to ViewModel, or ViewModel should be injected into Controller?

like image 329
Borysław Bobulski Avatar asked Oct 10 '12 12:10

Borysław Bobulski


People also ask

What is MVVM dependency injection?

Dependency Injection, or DI, is a programming technique that allows separation of concerns. A typical benefit of DI, in an MVVM application, is to make the developer able to instantiate a view model in the desired context.

What is MVVM Architecture pattern?

MVVM stands for Model, View, ViewModel. Model: This holds the data of the application. It cannot directly talk to the View. Generally, it's recommended to expose the data to the ViewModel through Observables. View: It represents the UI of the application devoid of any Application Logic.

Why use dependency injection?

Dependency injection helps to develop testable code, allowing developers to write unit tests easily. You can use mock databases with dependency injection, and test your application without affecting the actual database.

What is dependency injection in WPF?

As you know, dependency injection is a form of “inversion of the control” (IoC) programming principle. This means that classes don't create the objects they rely on. DI frameworks have containers responsible for revealing and resolving dependencies.


2 Answers

The MVVMC is simply a MVC where the View is replaced by a ViewModel pair.

  • The View interacts ONLY with the ViewModel taking advantage of the powerful data binding mechanisms in XAML based technologies.
  • The ViewModel can notify the Controller but SHOULD NEVER inject a controller.

I have put together a simply sample based on the well know sample of Josh Smith on MSDN... where I have introduced a Controller.

like image 148
Konstantinos Bakopanos Avatar answered Sep 19 '22 12:09

Konstantinos Bakopanos


It depends on what you are doing. I'm going to guess that most of the time the Controller would not need to be injected into either, but if it is needed, it is more likely to be needed in the ViewModel. Let me explain.

What are you doing with the controller? You must be doing something.. If that "something" is solely related to "what the data looks like", then it belongs in the View. If it is related to "what is be being shown to the user" then it belongs in the ViewModel.

I'm injecting a controller into one of my ViewModels. My ViewModel represents data which is then graphed in the View. I have a command which moves a data item from the current graph to a new graph. Since this changes "what is being displayed in the graph window" I implemented the command in my ViewModel. The ViewModel removes the data item from its own collection of items, and then uses the Controller to request a new view be created for that new data (it already had this functionality).

Looking at the article, I don't see arrows between the controller and the view From the article you linked

like image 23
Alan Avatar answered Sep 17 '22 12:09

Alan