Search code examples
wpfsilverlightdesign-patternsmvvmmessagebox

Clean Implementation showing popup Error messages in MVVM ViewModel


I want to be able to show popups with errors and warnings. These originate from my ViewModel and other utilities in my application.
I have a static class MessageBoxHelper that provides this functionality for me.

I now do encounter a problem with my testability. In cases where i want to verify failing loging my test trigger the MessageBox and i get a UI thead exception in my Test.

I want to achieve a loose coupling system that's clean with a little overhead to provide this functionality. I allready came up with 2 idea's but for me there is still to much downside:

  1. Implementing the Interface IMessageBoxHelper registered in my DI Container. This required me to add another parameter to my Constructor. And i want to avoid having a gigantic constructor.
  2. Instead of calling the MessageBoxHelper directly sending a message with my EventAggregator and implementing a MessageBoxListener as singleton catching these Messages and showing them. But this seems a bit over-engeneerded to me.

Is any of these idea's right or is there another way?


Solution

  • If your view model has a dependency on being able to show a message box (e.g. IMessageBoxHelper) why not make that obvious by injecting the dependency? Unit testing will be easy etc. I don't see a problem in choosing solution 1.