ViewModel Binding Model (WPF)
I am doing the transition from MVP to MVVM and am a little confused as to how best to bind ViewModel
to Model
. I understand how we can use the WPF data binding framework to route events between View
and ViewModel
using an interface ICommand
and INotifyPropertyChanged
for example View
:
public class MyView
{
public MyView()
{
InitializeComponent();
DataContext = new MyViewModel();
}
}
and ViewModel
:
public class MyViewModel : INotifyPropertyChanged
{
public MyViewModel(){}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
public ICommand MyCommand ...
}
This works great!
Now, generally with MVP, I would bind the Presenter
link to Model
via constructor injection and increment events to Model
from Presenter
to update the data in Model
. I've tried the same approach with MVVM, but it requires ViewModel
taking Model
as a dependency in my constructor, which seems to make some things useless with MVVM when using it straight out of the box without any form of IOC (at least with WPF) ...
So my two questions are:
- Is this introducing
Model
into theViewModel
right approach or should I implement the interfaceINotifyPropertyChanged
onModel
and using the WPF binding framework? - To take advantage of MVVM, should you almost always implement it with an IOC and a DI container, or better yet, Prism?
source to share
-
This is a "pure" MVVM approach: it
View
should only depend onViewModel
. ItselfViewModel
is a bridge betweenView
andModel
.Motivation - definition and responsibilities
Model
,View
andViewModel
as well as their relationship:- A model that provides a view-independent view of your business objects. The design of the model is optimized for logical relationships and operations between your business objects, regardless of how the data is presented in the user interface.
- The View class, which is the user interface . It displays information to the user and fires events in response to user interaction.
- The ViewModel that is the bridge between the view and the model . Each View class has a corresponding ViewModel class. The ViewModel retrieves data from the Model and processes it in the format required by the View . It notifies the View if the underlying data in the Model has changed, and it updates the data in the Model in response to UI events from the View.
Conclusion. Typing
Model
inViewModel
seems to be the correct approach. Also, instead of injection, itModel
may be helpful to type:-
Model Factory
to createModel
- lazy initialization; -
Service
(Service Facade
) to getModel
- lazy loading.
-
As shown in the book "MVVM Unleashed" by Michael Brown , the potential benefits of MVVM can be exploited: maintainability, testability, "mixability", portability. At the very least, dependency injection (in the described case using a dependency injection container) allows the design to follow the principle of dependency inversion:
Dependency Inversion is at the heart of many of the claimed benefits of object-oriented technology. Its correct application is essential to create reusable frameworks. It is also critical for building code that can be changed. And, because abstractions and details are isolated from each other, the code is much easier to maintain.
As a result, these are the benefits of MVVM, as maintainability and testability seem to be improved by following the Dependency Inversion Principle. The dependency injection container is just a tool to follow the principle.
source to share