Is it considered bad practice to have ViewModel objects hold the Dispatcher?

Posted by stiank81 on Stack Overflow See other posts from Stack Overflow or by stiank81
Published on 2010-03-12T11:01:20Z Indexed on 2010/03/12 13:07 UTC
Read the original article Hit count: 200

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#