tags:

views:

65

answers:

3

If I want to open a new Window, without needing any immediate information, is it ok to open it this way? I know with Dialogs I should use a service, but if I don't need any information back, it is quite troublesome and messy to add a new function into the service just to open a window?

// in ShellViewModel
public ICommand AboutCommand
{
    get
    {
        if (_aboutCommand == null) {
            _aboutCommand = new RelayCommand(delegate
            {
                var aboutView = new QuickImageUpload.Views.AboutView();
                aboutView.Show();
            });
        }
        return _aboutCommand;
    }
}
+2  A: 

You "could" do this, but you will be defeating the purpose of the MVVM pattern. The view models' purpose is to support unit testing (non STA thread execution) so when you start showing UI windows, your unit test will not work.

So to correct your understanding of using the service mediator for dialog boxes is not that it requires a return response of some kind, but to still allow view models to be executed in non STA threads, hence happy unit testers.

Hope that clears it up for you.

Tri Q
Oh because I was thinking if I don't require any return inputs, if I open something like this, how will I break unit tests? If I were to do it the "MVVM" way, I would add a `OpenAboutWindow()` to the `IWindowsService` for example?
jiewmeng
As i've explained, and i'm not sure if you have done unit testing yourself, but basically if you try to instantiate a new WPF window during unit testing it will complain that you require to run the code under STA mode (default is MTA).
Tri Q
Oh I haven't did WPF Unit Testing
jiewmeng
+2  A: 

To expand on what Tri Q said, once you do this, you've coupled the view model to WPF, and you won't be able to test it outside of the framework.

And even if this didn't actually keep NUnit (say) from functioning, it would still be a problem: How do you write a test to demonstrate that your view model is actually opening the window at the right time?

This is why you either want to implement a service, or even just have the view model raise an event. That gives you a way of writing a unit test that can verify that the window-opening logic is being triggered under the right circumstances.

Robert Rossney
A: 

In your case, I would instantiate the command to a real implementation like yours in the real world app or to a stub when the application is unit tested.

If you are not expecting anything from your dialog, I would not even bother as the only thing you are probably going to unit test is whether the command can be executed or not and that does not require any dialog opening.

vc 74