2

I'm writing my first "real" MVVM application, and the first step the user will need to take is to provide login credentials. In the past, I would have shown the login dialog after the main window has been laid out and made visible for the first time.

What is the preferred way of doing this in the MVVM world, and why?

I see many options, one of which is to continue doing it the way I've done it before, as it's a one-time step and won't interfere with the rest of the "MVVM-ness" of the application. Once the user has logged in, I could initialize the MainWindow's ModelView with their credentials and then continue on my way. Another option is to let the ModelView cause the login dialog to be shown (somehow). Do MVVM gurus prefer either of these?

(There is one other option, which is showing the login dialog first, and then when it is dismissed, create the main window. This would technically solve my dilemma, but it wouldn't really educate me, nor do I like the aesthetics of having a "naked" login dialog.)

moswald
  • 11,491
  • 7
  • 52
  • 78
  • My answer at http://stackoverflow.com/questions/3791677/mvvm-foundation-how-to-close-application-window-from-viewmodel/3793937#3793937 might help. – Sean Fausett Sep 25 '10 at 22:12

2 Answers2

2

The Model-View-ViewModel Pattern doesn't tell us how to define the UI workflow. We are free to choose an appropriate workflow ourself. I would prefer your first approach because it is simple and straight forward to implement. The workflow:

  1. Show empty Main Window
  2. Show Login Dialog
  3. Initialize Main Window with User Credentials

would be in the responsibility of a Controller (e.g. ApplicationController). How this might look like is shown in the ViewModel sample application of the WPF Application Framework (WAF).

jbe
  • 6,976
  • 1
  • 43
  • 34
0

I think the best(cleaner, testable...) option would be for the ViewModels to initiate the dialog show-up as the logic should belong to the VM and not the view...This 'show-up' is usually implemented by creating something like a DialogService that should take care of just that - Create an abstraction for your modal window (for example by creating an interface that has a method like ShowDialog() and a property DialogResult and perhaps more - that depends on your concrete need) that your dialogs implement; then create your DialogService that has a dependency on your abstraction of the dialog and in this service you can call ShowDialog, check for the DialogResult's value etc; In your VM you'll only use this dialog service without having a reference on some view related stuff...I won't get into the details as there are some implementations out there on displaying/working with modal dialogs from your VM that are based on this (and more)...Choosing this approach also gets you UI framework platform independence. HTH.

Stefan Szasz
  • 1,247
  • 11
  • 27