views:

371

answers:

2

My WPF application is structured using the MVVM pattern. The ViewModels will communicate asynchronously with a server, and when the requested data is returned a callback in the ViewModel is triggered, and it will do something with this data. This will run on a thread which is not the UI Thread. Sometimes these callbacks involve work that needs to be done on the UI thread, so I need the Dispatcher. This might be things such as:

  • Adding data to an ObservableCollection
  • Trigger Prism commands that will set something to be displayed in the GUI
  • Creating WPF objects of some kind.

I try to avoid the latter, but the two first points here I find to be reasonable things for ViewModels to do. So; is it okay to have ViewModels hold the Dispatcher to be able to Invoke commands for the UI thread? Or is this considered bad practice? And why?

+3  A: 

Since an ObservableCollection must be updated on the thread it belongs to (assuming a GUI app), and ObservableCollections should be part of the ViewModel, then there's a clear case for the ViewModel having a Dispatcher.

I can't see it being part of the Model.

kyoryu
It should definitly not be part of the model. The workaround you can do is to create a new ObservableCollection and fill the result with this, but that really sounds like a hack to me..
stiank81
I have a slight variation of MVVM:My Model is unmanaged code running on worker threads.The Model calls back into my .Net code, which then dispatches onto the GUI thread.The dispatched function running in the GUI thread then updates the ViewModel. In this way, my VM doesn't need to know about dispatching.
Surfbutler
+1  A: 

Ideally, a ViewModel should be totally independent from the UI technology used. We should theoretically be able to reuse it for Windows Forms (if we pimp up the Windows Forms controls a little bit to support better binding), for Web pages (I envision some kind of fancy mechanism here that would compile the ViewModel also into Javascript), and for any future technologies. Not all of these technologies will use the Dispatcher model.

That said, i consider it as a pragmatic compromise to include the Dispatcher in the ViewModel nowadays. In my ViewModel base class, I check for the current Dispatcher:

    protected override void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (Deployment.Current.Dispatcher == null || Deployment.Current.Dispatcher.CheckAccess())
        {
            base.OnPropertyChanged(sender, e);
        }
        else
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => base.OnPropertyChanged(sender, e));
        }
    }

I still have the dependency on System.Windows of course, but oh well. :->

herzmeister der welten
It sounds good to be able to reuse your ViewModels, but I don't really agree that this is the goal. Just consider a simple thing such as command bindings. You really need command bindings when doing MVVM, but does Winforms have ICommand? Actually I'm not sure, but earlier I wanted to make my ViewModels build on Mono, and at least Mono doesn't have ICommand..
stiank81
Still - valuable feedback! +1
stiank81
@heartmaster, if it (really) is a goal to reuse your VM, you should hide the dispatcher away behind some interface (that can also be re-implemented by WinForms InvokeRequired etc).
Henk Holterman
@both, yes, the out of the box Windows Forms controls of course lack support for MVVM, as I alluded. It was just an example. And the day I srsly consider making a ViewModel reusable for other technologies, I'll make an appropriate design. But for now, the Dispatcher is out of my sight in my inherited ViewModels, and that's all that matters for me now.
herzmeister der welten
Agree with sbank81 on reuse of ViewModels. It's not really the point. If it falls out that way, fine, but if you go into it trying to reuse your ViewModel, you're not really creating a ViewModel, you're creating a Model. Be wary of early generalization, it's often a sign of 'false laziness' - the costs and continued pain of the generalization is often more expensive than just writing a second ViewModel.
kyoryu