When we decide to develop an application, such as for WP7, using the MVVM pattern but without adopting an available framework (see MVVMLight, Caliburn Micro, ...) we can expect to have to solve some problems for guarantee decoupling between the UI and presentation and business logic. One of these problems can be for example the need to display a MessageBox, a component of the IU, starting from a method of ViewModel corresponding to the page in which we stand. This problem can be solved by implementing a service that will be "injected" into the ViewModel and that task will be to display the MessageBox on request. Because we must ensure that there isn’t strict dependence between the ViewModel and the implementation of the service, we must think in terms of interfaces. We can define an interface for that service in order to give the possibility to implement it from time to time in a different way, in our case we realize that the implementation will make use of the MessageBox from which we take a starting point for defining the methods of the interface.

 

   1: public interface IDialogService
   2: {
   3:     DialogResult Show(string messageText);
   4:  
   5:     DialogResult Show(string messageText, string caption, DialogButton button);
   6: }

The IDialogService interface exposes the same methods of MessageBox class while DialogResult and DialogButton, respectively define the possible values ​​returned by the dialog box by interaction with the user and the buttons that we want to see inside . For simplicity, they have been defined with two enumeration the same as those corresponding to the class MessageBox (MessageBoxResult and MessageBoxButton).

 

   1: public class DialogService : IDialogService
   2: {
   3:     #region IDialogService...
   4:  
   5:     public DialogResult Show(string messageText)
   6:     {
   7:         MessageBox.Show(messageText);
   8:         return DialogResult.OK;
   9:     }
  10:  
  11:     public DialogResult Show(string messageText, string caption, DialogButton button)
  12:     {
  13:         return this.MessageBoxToDialogResult(MessageBox.Show(messageText, caption, this.DialogToMessageBoxButton(button)));
  14:     }
  15:  
  16:     private DialogResult MessageBoxToDialogResult(MessageBoxResult messageBoxResult)
  17:     {
  18:         return (DialogResult)messageBoxResult;
  19:     }
  20:  
  21:     private MessageBoxButton DialogToMessageBoxButton(DialogButton dialogButton)
  22:     {
  23:         return (MessageBoxButton)dialogButton;
  24:     }
  25:  
  26:     #endregion
  27: }

The DialogService implementation not only implements the methods exposed by the interface but uses two methods to map the private DialogResult and DialogButton to the corresponding MessageBoxResult and MessageBoxButton. In this case, the mapping is trivially a cast as we have defined the enumeration, in other cases it may be more complex. Use of this service will be injecting the ViewModel through the constructor.

 

   1: private IDialogService dialogService;
   2:  
   3: public ViewModel(IDialogService dialogService)
   4: {
   5:     this.dialogService = dialogService;
   6: }

Finally, it will be usable directly in any method of ViewModel.

   1: this.dialogService.Show("Messaggio da visualizzare");