Why implement an interface on viewmodel and view in mvvm

I am new to MVVM template, so please bear with me. I have seen incarnations in wpf + mvvm + prism, where all views tend to have IView as the topmost interface. Then the views in the respective modules have a view-specific interface, for example IViewA, IViewB, etc., which implement the IView interface. Even the viewmodel has the largest IViewModel interface, and subsequent modules have IViewAViewModel, IViewBViewModel, etc., which inherit from IViewmodel. IViewmodel has a link to Iview, and Iview has a link to IViewModel.

namespace xxx.xxx.infrastructure { public interface IView { IViewModel ViewModel {get;set;} } public interface IViewModel { IView View {get;set;} } public abstract class ViewModelBase : IViewModel, INotifyPropertyChanged { public IView View {get;set;} public ViewModelBase(IView view) { View = view; View.ViewModel = this; } //INotifyPropertyChanged left out } } namespace xxx.xxx.Modules.Customer { public interface ICustomerDetailsView : IView { } public partial Class CustomerDetailsView : UserControl, ICustomerDetailsView { public CustomerDetailsView () { InitializeComponent(); } //Is this implementation acceptable?The view is supposed to have zero code in the code behind..... public IViewModel ViewModel { get { return (ICustomerDetailsViewViewModel)DataContext; } set { DataContext = value; } } } public interface ICustomerDetailsViewViewModel : IViewModel { string Message {get;set;} } public class CustomerDetailsViewViewModel : ViewModelBase, ICustomerDetailsViewViewModel { //Will be injected by unity as i have set up mappings in module initilize. public CustomerDetailsViewViewModel(ICustomerDetailsView view) :base(view) { } public string Message { //INotifyPropertyChanged left out for brevity get;set; } } 

I have a few questions.

1.) Is this a violation of MVVM, since the code behind the file must have zero code?

2.) In the view model, the MVVM should not worry about the view or its contract? Does this implementation violate the above?

3.) I do not understand what the use of this implementation is. This actually borders on MVP and requires a lot of code.

4.) If this is an acceptable implementation method, I need to have interfaces for all types and viewing modes in all my modules.?

+6
source share
2 answers

Basically, we only need interfaces for injection. There are two ways:

  • Introduce ViewModels into Views

This means that the ViewModel no longer has a link back to the view. This means that with unit testing of ViewModel you don't need a layout. In addition, it makes the code cleaner, in that in the View constructor, it simply sets the DataContext in the ViewModel that was introduced.

  • Introduce views into ViewModels.

This avoids the workflow logic in the Presentation layer. This means that the application layer is responsible for the application workflow. Thus, the logic of the workflow is closely related to the views, so it is impossible to write unit test for this.

+6
source

first a very good comment from Rachel regarding the viewmodel first:

Remember that with MVVM, your ViewModels applications are your application. View is just a nice interface that allows users to interact with your ViewModels.

1) IView is a violation of MVVM for me, but codebehind is of course allowed for ui. The viewmodel should simply not have a link to the view. see Hasif's 1st comment

2) see my block list

3) I'm with you - I never use something like that in my projects

4) PLS makes MVVM in a simple way - there is no connection, use di, ioc, commanding, behavior and for me the most important thing: viewmodel first :)

+11
source

Source: https://habr.com/ru/post/920383/


All Articles