Message Box in MVVM - which is the best practice? RRS feed

  • Question

  • Hi,

    the question is quite easy: Which is the best practice for managing message boxes using MVVM pattern? In other words:which is the best layer for putting the code that displays the message box and how do you handle it?

    My analysis is:
    - message box is something related to UI so message boxes hall be managed into View. On the other hand the ViewModel shall ask (in some way) the View for displaying the message box without broking MVVM pattern (so without referencing the View into ViewModel or vice versa...).
    - the easy way consists in managing message boxes into ViewModel but in that case you loose the Unit Test benefits of MVVM...

    So, which is the best practice?

    Wednesday, March 23, 2011 9:58 AM


  • The best practice is using a ViewModelBase that supports services. This way, you can inject the services and decide what messagebox service to use. For example, during unit tests, you can manipulate the result of a message box, but during runtime, you want to show an actual message box.

    In this article, I explain the services that ship with Catel (which includes a message service). It also has a service to handle model or non-model dialog windows. That's really clean and separates the UI (View) from the logic (View model).

    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a free open-source MVVM framework for WPF and Silverlight? Check out Catel!
    Wednesday, March 23, 2011 11:14 AM
  • Hello Marzio,

    Check out this design pattern: http://stackoverflow.com/questions/454868/handling-dialogs-in-wpf-with-mvvm.

    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"


    Wednesday, March 23, 2011 10:35 AM

All replies