Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doubts in MVVM pattern?

Tags:

c#

mvvm

wpf

c#-4.0

I am creating a WPF application and following the MVVM pattern. But while doing things I Worry about that, Is it according to MVVM or not? Please guide with these doubts.

  1. Is is necessary to have a new ViewModel for each and every View? If not, then can creating a single MasterViewModel is violation of MVVM?

  2. How will ViewModels communicate with each other?

  3. MainWindow.xaml.cs where I'm integrating all the Views, should have only the initialization of viewmodel and assigning the DataContext will there or I can put other codes also?

  4. I am having my defined EventHandlers. Should I use them in ViewModel or outside of model-view-viewmodel?

like image 462
PawanS Avatar asked Mar 29 '11 10:03

PawanS


People also ask

When should MVVM be used?

MVVM separates your view (i.e. Activity s and Fragment s) from your business logic. MVVM is enough for small projects, but when your codebase becomes huge, your ViewModel s start bloating. Separating responsibilities becomes hard. MVVM with Clean Architecture is pretty good in such cases.

What framework for MVVM should I use?

The Windows Presentation Framework (WPF) takes full advantage of the Model-View-ViewModel (MVVM) pattern.

What is purpose of using MVVM pattern?

Summary. The Model-View-ViewModel (MVVM) pattern helps to cleanly separate the business and presentation logic of an application from its user interface (UI).

Is MVVM faster?

Execution time in MVVM applications is faster with an average difference of 126.21 ms, while memory usage in MVP applications is lower with an average difference of 0.92 Mb.


1 Answers

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

Good examples of MVVM Template
Good Silverlight-MVVM Practice Example
MVVM Light Toolkit samples

For your questions:

  1. Some people follow this rule. : One-ViewModel-Per-View See Rule #2 on this article.

    It isn't absolutely necessary to follow this, but creating a MasterViewModel as using it everywhere, means you haven't understood MVVM yet.

    If you are referring to encapsulating common bits, yet, maybe, the MVVM Light Toolkit Has a ViewModelBase class for encapsulating a few things there.

  2. ViewModels won't communicate with each other, ViewModels will communicate with Views, and Views will communicate with other Views (and possibly instantiate ViewModels for them) some frameworks even have a loosely coupled way of doing this (ReactiveUI comes to mind)

  3. When a View is called, you could instantiate its corresponding ViewModel and then set it to the DataContext. But, there are many different patterns to doing this. @Euphporic mentions in the comments the ViewModel-first where ViewModels create Views through Ioc. See Which came first- The View or ViewModel.

    MVVM Light has a ViewModel locator which allows you to define a Views ViewModel statically inside XAML. It will automatically set when you create your view.

  4. Not completely clear here, (a) If you have events from Buttons, Menu, (anything deriving from ButtonBase) you should implement these using the Command Pattern. MVVM Light has a nice RelayCommand<T> that will help you here.

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


You've asked a couple of questions all over the place. The problem maybe you do not understand the WHY about MVVM.

In plain terms MVVM allows you keep the right thing at the right place, your application logic/control in the ViewModels, and then with the power of Silverlight/WPF databinding you hook your ViewModels to your views.

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

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

like image 148
gideon Avatar answered Sep 24 '22 03:09

gideon