Doubts about the MVVM pattern?

I am building a WPF application and following the MVVM pattern. But, while doing something, I worry about it, is it according to MVVM or not? Please be guided by these doubts.

  • Do I need to have a new ViewModel for each view? If not, can a single MasterViewModel create an MVVM violation?

  • How will ViewModels interact with each other?

  • MainWindow.xaml.cs , where I integrate all the views, should there only be a viewmodel initialization and a DataContext destination there, or can I also put other codes?

  • I have my specific EventHandlers. Should I use them in the ViewModel or outside the model-view-viewmodel?

+7
source share
4 answers

You need some reading to do this on MVVM. See the following questions:

Good examples of MVVM patterns
Good example of using Silverlight-MVVM
MVVM Light Toolkit Samples

For all questions:

  • Some people follow this rule: One-ViewModel-Per-View See Rule No. 2 in this article .

    It is not necessary to follow this, but creating a MasterViewModel using it everywhere means that you have not understood MVVM yet.

    If you mean encapsulating common bits, perhaps the MVVM Light Toolkit has a ViewModelBase class to encapsulate several things there.

  • ViewModels will not communicate with each other, ViewModels will interact with Views, and Views will interact with other views (and possibly create instances of ViewModels for them), some structures even have a loosely coupled way to perform this action ( ReactiveUI comes to mind)

  • When the View is called, you can instantiate the corresponding ViewModel and then set it to the DataContext. But there are many different templates for this. @Euphporic mentions in the comments ViewModel-first, where ViewModels create views through Ioc. See What came first - View or ViewModel .

    MVVM Light has a ViewModel locator that allows you to define a View ViewModel statically inside XAML. It will be automatically installed when you create your view.

  • It is not completely clear here, (a) If you have events from buttons, menus (everything that comes from ButtonBase), you should implement them using the command template. MVVM Light has a nice RelayCommand<T> that will help you here.

    (b) Other events, MVVM Light has EventToCommand behavior, but Laurent (author) warns about this, turning it into an Anti Pattern. I think sometimes you can just use simple events in your code behind and then call your ViewModels from there (but, hey, I'm not an expert here)


You asked a couple of questions everywhere. The problem, maybe you do not understand WHY about MVVM.

In simple terms, MVVM allows you to maintain the right work in the right place, the logic / control of applications in ViewModels, and then by binding Silverlight / WPF to your views, you bind ViewModels to your views.

As Laurent explains, sometimes it doesn't have to be that hard. You don’t even need a framework all the time.

I highly recommend watching his MIX video titled “Understanding the Model-View-ViewModel Template” from here:
http://live.visitmix.com/Archive#VideoList

+3
source

Do I need to have a new ViewModel for each view? If not, can a single MasterViewModel create an MVVM violation?

No, you can have several views on the same view model, although it is typical to have one view model for one view relationship. If you are thinking of one basic model for all species, then it can quickly become unmanageable.

How will all ViewModel interact with each other?

There are several ways, including direct links between view models, standard .NET events, or an event aggregator pattern.

MainWindow.xaml.cs, where am I integrating all the Views, should only have the viewmodel initialized and the destination of the DataContext will be there or can I put other codes?

As a rule, in MVVM you will not have (or very little) code behind and use the XAML binding mechanism in the WPF application, and not for the viewmodel / view link.

I have my specific EventHandlers. Should I use them in the ViewModel or outside the model-view-viewmodel?

Not quite sure what you mean by that, but you can use standard events to link the view model if necessary.

I would seriously consider using the MVVM framework for your application, and my personal preference was Caliburn.Micro .

+2
source

[1] Do I need to have a new ViewModel for each view? If not, then you can create a single MasterViewModel is a violation of MVVM?

Typically, you need both a new ViewModel instance for each View instance, and a different ViewModel class for each View class. Sometimes you need several views for the same ViewModel, which is in order. If you use the first ViewModel approach, you may not always need the View class for each ViewModel class, but it also does not hurt to have one.

[2] How will ViewModels interact with each other?

If ViewModels are directly related (for example, parent / child relationships), then one possibility is for one to have a direct link to another or to subscribe to events to another.

If the ViewModels are logically independent, you should use a different mechanism, such as an Event Aggregator (in Prism) or a Messenger (MVVM Light) or equivalent.

[3] MainWindow.xaml.cs, where I integrate all the Views, should only have the viewmodel initialized and the purpose of the DataContext will be there or can I put other codes?

You should not initialize ViewModels in the code of the View code. Models must be injected into representations using the dependency injection container (DI).

[4] I have my specific EventHandlers. Should I use them in the ViewModel or outside the model-view-ViewModel?

I could not understand what you are asking here.

+2
source

1. Do I need to have a new ViewModel for each view? If not, can a single MasterViewModel create an MVVM violation?

Not really. You can have specific ViewModels that correspond to a large number of views, each of which shows the same data in a different format. In fact, this is the whole rationale for MVVM in the first place - the segregation of displayed and business rules, so that the display format can be changed by loading different views.

You can also have one view that matches several other ViewModels. This is code reuse in the display user interface.

2. How will ViewModels interact with each other?

Typically, ViewModels interact with views through a WPF binding. This is why it is called MVVM, not MVC.

ViewModels can interact with each other through a number of standard .NET tools.

3. MainWindow.xaml.cs, where I integrate all the views, should only have a viewmodel initialization and a DataContext destination there, or can I put other codes too?

You usually split each view into a separate XAML file. This makes it easy to substitute a different view for a different format with the same data.

It is generally recommended that you separate your code from stand-alone modules; that is, one view of one file, one view mode of one file.

4. I have my specific EventHandlers. Should I use them in the ViewModel or outside the model-view-viewmodel?

Events should be processed in views if they are purely UI-driven (i.e. have nothing to do with data).

If the event should affect some changes in the underlying data (or do some actions in the business rules), you can in turn raise the event on the ViewModel. Please note that this event in the ViewModel may be different from the event in the view / interface.

+1
source

All Articles